SHA1Stream.st
changeset 9369 b7f43a51c4ed
parent 7824 7361f26d98cb
child 10781 322a34a50825
equal deleted inserted replaced
9368:c5a70cfa0f18 9369:b7f43a51c4ed
    11 "
    11 "
    12 
    12 
    13 
    13 
    14 
    14 
    15 
    15 
    16 "{ Package: 'exept:libcrypt' }"
    16 "{ Package: 'stx:libbasic' }"
    17 
    17 
    18 HashStream subclass:#SHA1Stream
    18 HashStream subclass:#SHA1Stream
    19 	instanceVariableNames:'hashContext'
    19 	instanceVariableNames:'hashContext'
    20 	classVariableNames:'HashSize ContextSize'
    20 	classVariableNames:'HashSize ContextSize'
    21 	poolDictionaries:''
    21 	poolDictionaries:''
    95 static void
    95 static void
    96 #if USE_ANSI_C
    96 #if USE_ANSI_C
    97 SHA1Transform (unsigned long state[5], unsigned char buffer[64])
    97 SHA1Transform (unsigned long state[5], unsigned char buffer[64])
    98 #else
    98 #else
    99 SHA1Transform (state, buffer)
    99 SHA1Transform (state, buffer)
   100     unsigned long state[5]; 
   100     unsigned long state[5];
   101     unsigned char buffer[64];
   101     unsigned char buffer[64];
   102 #endif
   102 #endif
   103 {
   103 {
   104     unsigned long a, b, c, d, e;
   104     unsigned long a, b, c, d, e;
   105     typedef union {
   105     typedef union {
   152 }
   152 }
   153 
   153 
   154 
   154 
   155 /* SHA1Init - Initialize new context */
   155 /* SHA1Init - Initialize new context */
   156 
   156 
   157 void 
   157 void
   158 #if USE_ANSI_C
   158 #if USE_ANSI_C
   159 SHA1Init(SHA1_CTX* context)
   159 SHA1Init(SHA1_CTX* context)
   160 #else
   160 #else
   161 SHA1Init(context)
   161 SHA1Init(context)
   162     SHA1_CTX* context;
   162     SHA1_CTX* context;
   172 }
   172 }
   173 
   173 
   174 
   174 
   175 /* Run your data through this. */
   175 /* Run your data through this. */
   176 
   176 
   177 void 
   177 void
   178 #if USE_ANSI_C
   178 #if USE_ANSI_C
   179 SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len)
   179 SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len)
   180 #else
   180 #else
   181 SHA1Update(context, data, len)
   181 SHA1Update(context, data, len)
   182     SHA1_CTX* context;
   182     SHA1_CTX* context;
   265     else {
   265     else {
   266 	if (!(file = fopen(argv[1], "rb"))) {
   266 	if (!(file = fopen(argv[1], "rb"))) {
   267 	    fputs("Unable to open file.", stderr);
   267 	    fputs("Unable to open file.", stderr);
   268 	    exit(-1);
   268 	    exit(-1);
   269 	}
   269 	}
   270     } 
   270     }
   271     SHA1Init(&context);
   271     SHA1Init(&context);
   272     while (!feof(file)) {  /* note: what if ferror(file) */
   272     while (!feof(file)) {  /* note: what if ferror(file) */
   273 	i = fread(buffer, 1, 16384, file);
   273 	i = fread(buffer, 1, 16384, file);
   274 	SHA1Update(&context, buffer, i);
   274 	SHA1Update(&context, buffer, i);
   275     }
   275     }
   324     [class variables:]
   324     [class variables:]
   325 	HashSize        size of returned hash value
   325 	HashSize        size of returned hash value
   326 	ContextSize     (implementation) size of hash context
   326 	ContextSize     (implementation) size of hash context
   327 
   327 
   328     [instance variables:]
   328     [instance variables:]
   329 	hashContext     (implementation) 
   329 	hashContext     (implementation)
   330 			internal buffer for computation of the hash value
   330 			internal buffer for computation of the hash value
   331 "
   331 "
   332 !
   332 !
   333 
   333 
   334 examples
   334 examples
   453 testVector
   453 testVector
   454     "Test Vectors (from FIPS PUB 180-1)"
   454     "Test Vectors (from FIPS PUB 180-1)"
   455 
   455 
   456     ^ #(
   456     ^ #(
   457 	('abc'
   457 	('abc'
   458 	 #[16rA9 16r99 16r3E 16r36  16r47 16r06 16r81 16r6A  16rBA 16r3E 16r25 16r71 
   458 	 #[16rA9 16r99 16r3E 16r36  16r47 16r06 16r81 16r6A  16rBA 16r3E 16r25 16r71
   459 	   16r78 16r50 16rC2 16r6C  16r9C 16rD0 16rD8 16r9D])
   459 	   16r78 16r50 16rC2 16r6C  16r9C 16rD0 16rD8 16r9D])
   460 
   460 
   461 	('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'
   461 	('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'
   462 	  #[16r84 16r98 16r3E 16r44  16r1C 16r3B 16rD2 16r6E  16rBA 16rAE 16r4A 16rA1 
   462 	  #[16r84 16r98 16r3E 16r44  16r1C 16r3B 16rD2 16r6E  16rBA 16rAE 16r4A 16rA1
   463 	    16rF9 16r51 16r29 16rE5  16rE5 16r46 16r70 16rF1])
   463 	    16rF9 16r51 16r29 16rE5  16rE5 16r46 16r70 16rF1])
   464        ) copyWith:
   464        ) copyWith:
   465 	(Array with:(String new:1000000 withAll:$a)
   465 	(Array with:(String new:1000000 withAll:$a)
   466 	       with:#[16r34 16rAA 16r97 16r3C  16rD4 16rC4 16rDA 16rA4  16rF6 16r1E 16rEB 16r2B  
   466 	       with:#[16r34 16rAA 16r97 16r3C  16rD4 16rC4 16rDA 16rA4  16rF6 16r1E 16rEB 16r2B
   467 		      16rDB 16rAD 16r27 16r31  16r65 16r34 16r01 16r6F])
   467 		      16rDB 16rAD 16r27 16r31  16r65 16r34 16r01 16r6F])
   468 
   468 
   469     "
   469     "
   470      self test
   470      self test
   471     "
   471     "
   490 %{
   490 %{
   491    if (__isNonNilObject(__INST(hashContext)) &&
   491    if (__isNonNilObject(__INST(hashContext)) &&
   492        __qClass(__INST(hashContext)) == @global(ByteArray) &&
   492        __qClass(__INST(hashContext)) == @global(ByteArray) &&
   493        __byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX)
   493        __byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX)
   494    ) {
   494    ) {
   495 	SHA1_CTX *ctx = 
   495 	SHA1_CTX *ctx =
   496 	    (SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element;
   496 	    (SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element;
   497 
   497 
   498 	SHA1Init(ctx);
   498 	SHA1Init(ctx);
   499 	RETURN(self);
   499 	RETURN(self);
   500    }
   500    }
   522 	__byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX) &&
   522 	__byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX) &&
   523 	__isNonNilObject(digest) &&
   523 	__isNonNilObject(digest) &&
   524 	__qClass(digest) == @global(ByteArray) &&
   524 	__qClass(digest) == @global(ByteArray) &&
   525 	__byteArraySize(digest) == 20
   525 	__byteArraySize(digest) == 20
   526     ) {
   526     ) {
   527 	SHA1_CTX *ctx = 
   527 	SHA1_CTX *ctx =
   528 	    (SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element;
   528 	    (SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element;
   529 	SHA1_CTX copyContext;
   529 	SHA1_CTX copyContext;
   530 
   530 
   531 	memcpy(&copyContext, ctx, sizeof(copyContext));
   531 	memcpy(&copyContext, ctx, sizeof(copyContext));
   532 	SHA1Final(__ByteArrayInstPtr(digest)->ba_element, &copyContext);
   532 	SHA1Final(__ByteArrayInstPtr(digest)->ba_element, &copyContext);
   553 %{
   553 %{
   554    if (__isNonNilObject(__INST(hashContext)) &&
   554    if (__isNonNilObject(__INST(hashContext)) &&
   555        __qClass(__INST(hashContext)) == @global(ByteArray) &&
   555        __qClass(__INST(hashContext)) == @global(ByteArray) &&
   556        __byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX)
   556        __byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX)
   557    ) {
   557    ) {
   558 	SHA1_CTX *ctx = 
   558 	SHA1_CTX *ctx =
   559 	    (SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element;
   559 	    (SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element;
   560 
   560 
   561 	if (__isNonNilObject(anObject)) {
   561 	if (__isNonNilObject(anObject)) {
   562 	    OBJ cls =__qClass(anObject);
   562 	    OBJ cls =__qClass(anObject);
   563 	    INT mask = (INT)(__ClassInstPtr(cls)->c_flags) & __MASKSMALLINT(ARRAYMASK);
   563 	    INT mask = (INT)(__ClassInstPtr(cls)->c_flags) & __MASKSMALLINT(ARRAYMASK);
   615     "Created: / 17.3.1999 / 16:14:13 / stefan"
   615     "Created: / 17.3.1999 / 16:14:13 / stefan"
   616 !
   616 !
   617 
   617 
   618 nextPutBytes:count from:anObject startingAt:start
   618 nextPutBytes:count from:anObject startingAt:start
   619     "update the hash value with count bytes from an object starting at index start.
   619     "update the hash value with count bytes from an object starting at index start.
   620      The object must have non-pointer indexed instvars 
   620      The object must have non-pointer indexed instvars
   621      (i.e. be a ByteArray, String, Float- or DoubleArray),
   621      (i.e. be a ByteArray, String, Float- or DoubleArray),
   622      or an externalBytes object (with known size)"
   622      or an externalBytes object (with known size)"
   623 
   623 
   624 %{
   624 %{
   625     int len, offs;
   625     int len, offs;
   630    if (__isNonNilObject(__INST(hashContext))
   630    if (__isNonNilObject(__INST(hashContext))
   631        &&__qClass(__INST(hashContext)) == @global(ByteArray)
   631        &&__qClass(__INST(hashContext)) == @global(ByteArray)
   632        && __byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX)
   632        && __byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX)
   633        && __bothSmallInteger(count, start)
   633        && __bothSmallInteger(count, start)
   634    ) {
   634    ) {
   635 	SHA1_CTX *ctx = 
   635 	SHA1_CTX *ctx =
   636 	    (SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element;
   636 	    (SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element;
   637 
   637 
   638 	len = __intVal(count);
   638 	len = __intVal(count);
   639 	offs = __intVal(start) - 1;
   639 	offs = __intVal(start) - 1;
   640 
   640 
   680 ! !
   680 ! !
   681 
   681 
   682 !SHA1Stream class methodsFor:'documentation'!
   682 !SHA1Stream class methodsFor:'documentation'!
   683 
   683 
   684 version
   684 version
   685     ^ '$Header: /cvs/stx/stx/libbasic/SHA1Stream.st,v 1.11 2004-01-08 09:47:52 cg Exp $'
   685     ^ '$Header: /cvs/stx/stx/libbasic/SHA1Stream.st,v 1.12 2006-06-14 15:53:56 stefan Exp $'
   686 ! !
   686 ! !
   687 
   687 
   688 SHA1Stream initialize!
   688 SHA1Stream initialize!