# HG changeset patch # User Claus Gittinger # Date 1073555272 -3600 # Node ID 7361f26d98cb8794f9e820ac04c553c11226093f # Parent 4a31fa10c6eb3c81334abad2a2a8f47eb5ba102b code indentation diff -r 4a31fa10c6eb -r 7361f26d98cb SHA1Stream.st --- a/SHA1Stream.st Wed Jan 07 13:51:18 2004 +0100 +++ b/SHA1Stream.st Thu Jan 08 10:47:52 2004 +0100 @@ -26,7 +26,9 @@ %{ #if defined(__LSBFIRST) -# define LITTLE_ENDIAN /* This should be #define'd if true. */ +# ifndef LITTLE_ENDIAN +# define LITTLE_ENDIAN /* This should be #define'd if true. */ +# endif #endif #define SHA1HANDSOFF /* Copies data before messing with it. */ @@ -40,21 +42,20 @@ unsigned char buffer[64]; } SHA1_CTX; -#if ORIGINAL -void SHA1Transform(unsigned long state[5], unsigned char buffer[64]); -void SHA1Init(SHA1_CTX* context); -void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len); -void SHA1Final(unsigned char digest[20], SHA1_CTX* context); -#else +#if USE_ANSI_C + void SHA1Transform(unsigned long state[5], unsigned char buffer[64]); + void SHA1Init(SHA1_CTX* context); + void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len); + void SHA1Final(unsigned char digest[20], SHA1_CTX* context); +#else /* general: compiles everywhere */ +# define SHA1Init __SHA1Init +# define SHA1Update __SHA1Update +# define SHA1Final __SHA2Final -#define SHA1Init __SHA1Init -#define SHA1Update __SHA1Update -#define SHA1Final __SHA2Final - -void SHA1Init(); -void SHA1Update(); -void SHA1Final(); -#endif + void SHA1Init(); + void SHA1Update(); + void SHA1Final(); +#endif /* USE_ANSI_C */ %} ! ! @@ -62,30 +63,22 @@ %{ /* -SHA-1 in C -By Steve Reid -100% Public Domain - -Test Vectors (from FIPS PUB 180-1) -"abc" - A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D -"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" - 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 -A million repetitions of "a" - 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F -*/ - + * SHA-1 in C + * By Steve Reid + * 100% Public Domain + */ #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) /* blk0() and blk() perform the initial expand. */ /* I got the idea of expanding during the round function from SSLeay */ #ifdef LITTLE_ENDIAN -#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ +# define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ |(rol(block->l[i],8)&0x00FF00FF)) #else -#define blk0(i) block->l[i] +# define blk0(i) block->l[i] #endif + #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ ^block->l[(i+2)&15]^block->l[i&15],1)) @@ -99,9 +92,8 @@ /* Hash a single 512-bit block. This is the core of the algorithm. */ -static -void -#if ORIGINAL +static void +#if USE_ANSI_C SHA1Transform (unsigned long state[5], unsigned char buffer[64]) #else SHA1Transform (state, buffer) @@ -109,14 +101,14 @@ unsigned char buffer[64]; #endif { -unsigned long a, b, c, d, e; -typedef union { - unsigned char c[64]; - unsigned long l[16]; -} CHAR64LONG16; -CHAR64LONG16* block; + unsigned long a, b, c, d, e; + typedef union { + unsigned char c[64]; + unsigned long l[16]; + } CHAR64LONG16; + CHAR64LONG16* block; #ifdef SHA1HANDSOFF -static unsigned char workspace[64]; + static unsigned char workspace[64]; block = (CHAR64LONG16*)workspace; memcpy(block, buffer, 64); #else @@ -163,7 +155,7 @@ /* SHA1Init - Initialize new context */ void -#if ORIGINAL +#if USE_ANSI_C SHA1Init(SHA1_CTX* context) #else SHA1Init(context) @@ -183,7 +175,7 @@ /* Run your data through this. */ void -#if ORIGINAL +#if USE_ANSI_C SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len) #else SHA1Update(context, data, len) @@ -192,7 +184,7 @@ unsigned int len; #endif { -unsigned int i, j; + unsigned int i, j; j = (context->count[0] >> 3) & 63; if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++; @@ -213,7 +205,7 @@ /* Add padding and return the message digest. */ void -#if ORIGINAL +#if USE_ANSI_C SHA1Final(unsigned char digest[20], SHA1_CTX* context) #else SHA1Final(digest, context) @@ -221,8 +213,8 @@ SHA1_CTX* context; #endif { -unsigned long i, j; -unsigned char finalcount[8]; + unsigned long i, j; + unsigned char finalcount[8]; for (i = 0; i < 8; i++) { finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] @@ -251,13 +243,16 @@ #if 0 /*************************************************************/ +/* + * SHA1 test program + */ int main(int argc, char** argv) { -int i, j; -SHA1_CTX context; -unsigned char digest[20], buffer[16384]; -FILE* file; + int i, j; + SHA1_CTX context; + unsigned char digest[20], buffer[16384]; + FILE* file; if (argc > 2) { puts("Public domain SHA-1 implementation - by Steve Reid "); @@ -308,9 +303,6 @@ other person. No title to or ownership of the software is hereby transferred. " - - - ! documentation @@ -337,22 +329,22 @@ hashContext (implementation) internal buffer for computation of the hash value " - - - ! examples " -Test Vectors (from FIPS PUB 180-1) -'abc' - A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D -'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' - 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 -A million repetitions of 'a' - 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F + Test Vectors (from FIPS PUB 180-1) + + 'abc' + A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D - [exBegin] + 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' + 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 + + A million repetitions of 'a' + 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F + + [exBegin] |hashStream| hashStream := SHA1Stream new. @@ -360,16 +352,16 @@ hashStream hashValue printOn:Transcript base:16. Transcript cr. hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'. hashStream hashValue printOn:Transcript base:16. Transcript cr. - [exEnd] + [exEnd] - [exBegin] + [exBegin] |hashValue| hahValue := SHA1Stream hashValueOf:'abc'. hashValue printOn:Transcript base:16. Transcript cr. - [exEnd] + [exEnd] - [exBegin] + [exBegin] |hashStream| hashStream := SHA1Stream new. @@ -378,47 +370,47 @@ hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' asByteArray. hashStream hashValue printOn:Transcript base:16. Transcript cr. - [exEnd] + [exEnd] - [exBegin] + [exBegin] |hashStream| hashStream := SHA1Stream new. 1000000 timesRepeat:[ hashStream nextPut:$a ]. hashStream hashValue printOn:Transcript base:16. Transcript cr. - [exEnd] + [exEnd] - [exBegin] + [exBegin] |hashStream| hashStream := SHA1Stream new. hashStream nextPut:'a'. hashStream hashValue printOn:Transcript base:16. Transcript cr. - [exEnd] + [exEnd] - [exBegin] + [exBegin] |hashStream| hashStream := SHA1Stream new. hashStream nextPut:$a. hashStream hashValue printOn:Transcript base:16. Transcript cr. - [exEnd] + [exEnd] timing throughput: - [exBegin] + [exBegin] |hashStream n t| hashStream := SHA1Stream new. n := 1000000. t := Time millisecondsToRun:[ - n timesRepeat:[ - hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. - ]. - ]. + n timesRepeat:[ + hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. + ]. + ]. t := (t / 1000) asFloat. Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'. Transcript show:(n*50/1024 / t); showCR:' Kb/s' - [exEnd] + [exEnd] " ! ! @@ -462,17 +454,17 @@ "Test Vectors (from FIPS PUB 180-1)" ^ #( - ('abc' - #[16rA9 16r99 16r3E 16r36 16r47 16r06 16r81 16r6A 16rBA 16r3E 16r25 16r71 - 16r78 16r50 16rC2 16r6C 16r9C 16rD0 16rD8 16r9D]) + ('abc' + #[16rA9 16r99 16r3E 16r36 16r47 16r06 16r81 16r6A 16rBA 16r3E 16r25 16r71 + 16r78 16r50 16rC2 16r6C 16r9C 16rD0 16rD8 16r9D]) - ('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' - #[16r84 16r98 16r3E 16r44 16r1C 16r3B 16rD2 16r6E 16rBA 16rAE 16r4A 16rA1 - 16rF9 16r51 16r29 16rE5 16rE5 16r46 16r70 16rF1]) + ('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' + #[16r84 16r98 16r3E 16r44 16r1C 16r3B 16rD2 16r6E 16rBA 16rAE 16r4A 16rA1 + 16rF9 16r51 16r29 16rE5 16rE5 16r46 16r70 16rF1]) ) copyWith: - (Array with:(String new:1000000 withAll:$a) - with:#[16r34 16rAA 16r97 16r3C 16rD4 16rC4 16rDA 16rA4 16rF6 16r1E 16rEB 16r2B - 16rDB 16rAD 16r27 16r31 16r65 16r34 16r01 16r6F]) + (Array with:(String new:1000000 withAll:$a) + with:#[16r34 16rAA 16r97 16r3C 16rD4 16rC4 16rDA 16rA4 16rF6 16r1E 16rEB 16r2B + 16rDB 16rAD 16r27 16r31 16r65 16r34 16r01 16r6F]) " self test @@ -640,46 +632,46 @@ && __byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX) && __bothSmallInteger(count, start) ) { - SHA1_CTX *ctx = - (SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; + SHA1_CTX *ctx = + (SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; - len = __intVal(count); - offs = __intVal(start) - 1; + len = __intVal(count); + offs = __intVal(start) - 1; - oClass = __Class(anObject); - if (oClass == ExternalBytes) { - OBJ sz; + oClass = __Class(anObject); + if (oClass == ExternalBytes) { + OBJ sz; - nInstBytes = 0; - extPtr = (char *)__externalBytesAddress(anObject); - sz = __externalBytesSize(anObject); - if (__isSmallInteger(sz)) { - objSize = __intVal(sz); - } else { - objSize = 0; /* unknown */ - } - } else { - switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { - case BYTEARRAY: - case WORDARRAY: - case LONGARRAY: - case SWORDARRAY: - case SLONGARRAY: - case FLOATARRAY: - case DOUBLEARRAY: - break; - default: - goto bad; - } - nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); - nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); - objSize = __Size(anObject) - nInstBytes; - extPtr = (char *)__byteArrayVal(anObject); - } - if ((offs >= 0) && (len >= 0) && (objSize >= (len + offs))) { - SHA1Update(ctx, extPtr+offs, len); - RETURN (count); - } + nInstBytes = 0; + extPtr = (char *)__externalBytesAddress(anObject); + sz = __externalBytesSize(anObject); + if (__isSmallInteger(sz)) { + objSize = __intVal(sz); + } else { + objSize = 0; /* unknown */ + } + } else { + switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { + case BYTEARRAY: + case WORDARRAY: + case LONGARRAY: + case SWORDARRAY: + case SLONGARRAY: + case FLOATARRAY: + case DOUBLEARRAY: + break; + default: + goto bad; + } + nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); + nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); + objSize = __Size(anObject) - nInstBytes; + extPtr = (char *)__byteArrayVal(anObject); + } + if ((offs >= 0) && (len >= 0) && (objSize >= (len + offs))) { + SHA1Update(ctx, extPtr+offs, len); + RETURN (count); + } } bad: ; %}. @@ -690,7 +682,7 @@ !SHA1Stream class methodsFor:'documentation'! version - ^ '$Header: /cvs/stx/stx/libbasic/SHA1Stream.st,v 1.10 2003-02-20 15:13:09 stefan Exp $' + ^ '$Header: /cvs/stx/stx/libbasic/SHA1Stream.st,v 1.11 2004-01-08 09:47:52 cg Exp $' ! ! SHA1Stream initialize!