author | Claus Gittinger <cg@exept.de> |
Thu, 08 Jan 2004 10:47:52 +0100 | |
changeset 7824 | 7361f26d98cb |
parent 7045 | f970d69665b7 |
child 9369 | b7f43a51c4ed |
permissions | -rw-r--r-- |
4426 | 1 |
" |
2 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
5056 | 3 |
All Rights Reserved |
4426 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
7042 | 13 |
|
14 |
||
15 |
||
6789 | 16 |
"{ Package: 'exept:libcrypt' }" |
4051 | 17 |
|
18 |
HashStream subclass:#SHA1Stream |
|
19 |
instanceVariableNames:'hashContext' |
|
20 |
classVariableNames:'HashSize ContextSize' |
|
21 |
poolDictionaries:'' |
|
4797 | 22 |
category:'System-Crypt-Streams' |
4051 | 23 |
! |
24 |
||
25 |
!SHA1Stream primitiveDefinitions! |
|
26 |
%{ |
|
27 |
||
28 |
#if defined(__LSBFIRST) |
|
7824 | 29 |
# ifndef LITTLE_ENDIAN |
30 |
# define LITTLE_ENDIAN /* This should be #define'd if true. */ |
|
31 |
# endif |
|
4051 | 32 |
#endif |
33 |
||
5056 | 34 |
#define SHA1HANDSOFF /* Copies data before messing with it. */ |
4051 | 35 |
|
36 |
#include <stdio.h> |
|
37 |
#include <string.h> |
|
38 |
||
39 |
typedef struct { |
|
40 |
unsigned long state[5]; |
|
41 |
unsigned long count[2]; |
|
42 |
unsigned char buffer[64]; |
|
43 |
} SHA1_CTX; |
|
44 |
||
7824 | 45 |
#if USE_ANSI_C |
46 |
void SHA1Transform(unsigned long state[5], unsigned char buffer[64]); |
|
47 |
void SHA1Init(SHA1_CTX* context); |
|
48 |
void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len); |
|
49 |
void SHA1Final(unsigned char digest[20], SHA1_CTX* context); |
|
50 |
#else /* general: compiles everywhere */ |
|
51 |
# define SHA1Init __SHA1Init |
|
52 |
# define SHA1Update __SHA1Update |
|
53 |
# define SHA1Final __SHA2Final |
|
4051 | 54 |
|
7824 | 55 |
void SHA1Init(); |
56 |
void SHA1Update(); |
|
57 |
void SHA1Final(); |
|
58 |
#endif /* USE_ANSI_C */ |
|
4051 | 59 |
%} |
60 |
! ! |
|
61 |
||
62 |
!SHA1Stream primitiveFunctions! |
|
63 |
%{ |
|
64 |
||
65 |
/* |
|
7824 | 66 |
* SHA-1 in C |
67 |
* By Steve Reid <steve@edmweb.com> |
|
68 |
* 100% Public Domain |
|
69 |
*/ |
|
4051 | 70 |
|
71 |
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) |
|
72 |
||
73 |
/* blk0() and blk() perform the initial expand. */ |
|
74 |
/* I got the idea of expanding during the round function from SSLeay */ |
|
75 |
#ifdef LITTLE_ENDIAN |
|
7824 | 76 |
# define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ |
4051 | 77 |
|(rol(block->l[i],8)&0x00FF00FF)) |
78 |
#else |
|
7824 | 79 |
# define blk0(i) block->l[i] |
4051 | 80 |
#endif |
7824 | 81 |
|
4051 | 82 |
#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ |
83 |
^block->l[(i+2)&15]^block->l[i&15],1)) |
|
84 |
||
85 |
/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ |
|
86 |
#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); |
|
87 |
#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); |
|
88 |
#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); |
|
89 |
#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); |
|
90 |
#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); |
|
91 |
||
92 |
||
93 |
/* Hash a single 512-bit block. This is the core of the algorithm. */ |
|
94 |
||
7824 | 95 |
static void |
96 |
#if USE_ANSI_C |
|
4051 | 97 |
SHA1Transform (unsigned long state[5], unsigned char buffer[64]) |
98 |
#else |
|
99 |
SHA1Transform (state, buffer) |
|
100 |
unsigned long state[5]; |
|
101 |
unsigned char buffer[64]; |
|
102 |
#endif |
|
103 |
{ |
|
7824 | 104 |
unsigned long a, b, c, d, e; |
105 |
typedef union { |
|
106 |
unsigned char c[64]; |
|
107 |
unsigned long l[16]; |
|
108 |
} CHAR64LONG16; |
|
109 |
CHAR64LONG16* block; |
|
4051 | 110 |
#ifdef SHA1HANDSOFF |
7824 | 111 |
static unsigned char workspace[64]; |
4051 | 112 |
block = (CHAR64LONG16*)workspace; |
113 |
memcpy(block, buffer, 64); |
|
114 |
#else |
|
115 |
block = (CHAR64LONG16*)buffer; |
|
116 |
#endif |
|
117 |
/* Copy context->state[] to working vars */ |
|
118 |
a = state[0]; |
|
119 |
b = state[1]; |
|
120 |
c = state[2]; |
|
121 |
d = state[3]; |
|
122 |
e = state[4]; |
|
123 |
/* 4 rounds of 20 operations each. Loop unrolled. */ |
|
124 |
R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); |
|
125 |
R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); |
|
126 |
R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); |
|
127 |
R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); |
|
128 |
R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); |
|
129 |
R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); |
|
130 |
R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); |
|
131 |
R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); |
|
132 |
R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); |
|
133 |
R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); |
|
134 |
R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); |
|
135 |
R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); |
|
136 |
R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); |
|
137 |
R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); |
|
138 |
R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); |
|
139 |
R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); |
|
140 |
R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); |
|
141 |
R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); |
|
142 |
R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); |
|
143 |
R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); |
|
144 |
/* Add the working vars back into context.state[] */ |
|
145 |
state[0] += a; |
|
146 |
state[1] += b; |
|
147 |
state[2] += c; |
|
148 |
state[3] += d; |
|
149 |
state[4] += e; |
|
150 |
/* Wipe variables */ |
|
151 |
a = b = c = d = e = 0; |
|
152 |
} |
|
153 |
||
154 |
||
155 |
/* SHA1Init - Initialize new context */ |
|
156 |
||
157 |
void |
|
7824 | 158 |
#if USE_ANSI_C |
4051 | 159 |
SHA1Init(SHA1_CTX* context) |
160 |
#else |
|
161 |
SHA1Init(context) |
|
162 |
SHA1_CTX* context; |
|
163 |
#endif |
|
164 |
{ |
|
165 |
/* SHA1 initialization constants */ |
|
166 |
context->state[0] = 0x67452301; |
|
167 |
context->state[1] = 0xEFCDAB89; |
|
168 |
context->state[2] = 0x98BADCFE; |
|
169 |
context->state[3] = 0x10325476; |
|
170 |
context->state[4] = 0xC3D2E1F0; |
|
171 |
context->count[0] = context->count[1] = 0; |
|
172 |
} |
|
173 |
||
174 |
||
175 |
/* Run your data through this. */ |
|
176 |
||
177 |
void |
|
7824 | 178 |
#if USE_ANSI_C |
4051 | 179 |
SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len) |
180 |
#else |
|
181 |
SHA1Update(context, data, len) |
|
182 |
SHA1_CTX* context; |
|
183 |
unsigned char* data; |
|
184 |
unsigned int len; |
|
185 |
#endif |
|
186 |
{ |
|
7824 | 187 |
unsigned int i, j; |
4051 | 188 |
|
189 |
j = (context->count[0] >> 3) & 63; |
|
190 |
if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++; |
|
191 |
context->count[1] += (len >> 29); |
|
192 |
if ((j + len) > 63) { |
|
5056 | 193 |
memcpy(&context->buffer[j], data, (i = 64-j)); |
194 |
SHA1Transform(context->state, context->buffer); |
|
195 |
for ( ; i + 63 < len; i += 64) { |
|
196 |
SHA1Transform(context->state, &data[i]); |
|
197 |
} |
|
198 |
j = 0; |
|
4051 | 199 |
} |
200 |
else i = 0; |
|
201 |
memcpy(&context->buffer[j], &data[i], len - i); |
|
202 |
} |
|
203 |
||
204 |
||
205 |
/* Add padding and return the message digest. */ |
|
206 |
||
207 |
void |
|
7824 | 208 |
#if USE_ANSI_C |
4051 | 209 |
SHA1Final(unsigned char digest[20], SHA1_CTX* context) |
210 |
#else |
|
211 |
SHA1Final(digest, context) |
|
212 |
unsigned char digest[20]; |
|
213 |
SHA1_CTX* context; |
|
214 |
#endif |
|
215 |
{ |
|
7824 | 216 |
unsigned long i, j; |
217 |
unsigned char finalcount[8]; |
|
4051 | 218 |
|
219 |
for (i = 0; i < 8; i++) { |
|
5056 | 220 |
finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] |
221 |
>> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ |
|
4051 | 222 |
} |
223 |
SHA1Update(context, (unsigned char *)"\200", 1); |
|
224 |
while ((context->count[0] & 504) != 448) { |
|
5056 | 225 |
SHA1Update(context, (unsigned char *)"\0", 1); |
4051 | 226 |
} |
227 |
SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ |
|
228 |
for (i = 0; i < 20; i++) { |
|
5056 | 229 |
digest[i] = (unsigned char) |
230 |
((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); |
|
4051 | 231 |
} |
232 |
/* Wipe variables */ |
|
233 |
i = j = 0; |
|
234 |
memset(context->buffer, 0, 64); |
|
235 |
memset(context->state, 0, 20); |
|
236 |
memset(context->count, 0, 8); |
|
237 |
memset(&finalcount, 0, 8); |
|
238 |
#ifdef SHA1HANDSOFF /* make SHA1Transform overwrite it's own static vars */ |
|
239 |
SHA1Transform(context->state, context->buffer); |
|
240 |
#endif |
|
241 |
} |
|
242 |
||
243 |
#if 0 |
|
244 |
/*************************************************************/ |
|
245 |
||
7824 | 246 |
/* |
247 |
* SHA1 test program |
|
248 |
*/ |
|
4051 | 249 |
|
250 |
int main(int argc, char** argv) |
|
251 |
{ |
|
7824 | 252 |
int i, j; |
253 |
SHA1_CTX context; |
|
254 |
unsigned char digest[20], buffer[16384]; |
|
255 |
FILE* file; |
|
4051 | 256 |
|
257 |
if (argc > 2) { |
|
5056 | 258 |
puts("Public domain SHA-1 implementation - by Steve Reid <steve@edmweb.com>"); |
259 |
puts("Produces the SHA-1 hash of a file, or stdin if no file is specified."); |
|
260 |
exit(0); |
|
4051 | 261 |
} |
262 |
if (argc < 2) { |
|
5056 | 263 |
file = stdin; |
4051 | 264 |
} |
265 |
else { |
|
5056 | 266 |
if (!(file = fopen(argv[1], "rb"))) { |
267 |
fputs("Unable to open file.", stderr); |
|
268 |
exit(-1); |
|
269 |
} |
|
4051 | 270 |
} |
271 |
SHA1Init(&context); |
|
272 |
while (!feof(file)) { /* note: what if ferror(file) */ |
|
5056 | 273 |
i = fread(buffer, 1, 16384, file); |
274 |
SHA1Update(&context, buffer, i); |
|
4051 | 275 |
} |
276 |
SHA1Final(digest, &context); |
|
277 |
fclose(file); |
|
278 |
for (i = 0; i < 5; i++) { |
|
5056 | 279 |
for (j = 0; j < 4; j++) { |
280 |
printf("%02X", digest[i*4+j]); |
|
281 |
} |
|
282 |
putchar(' '); |
|
4051 | 283 |
} |
284 |
putchar('\n'); |
|
285 |
exit(0); |
|
286 |
} |
|
287 |
#endif |
|
288 |
||
289 |
%} |
|
290 |
! ! |
|
291 |
||
292 |
!SHA1Stream class methodsFor:'documentation'! |
|
293 |
||
4426 | 294 |
copyright |
295 |
" |
|
296 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
5056 | 297 |
All Rights Reserved |
4426 | 298 |
|
299 |
This software is furnished under a license and may be used |
|
300 |
only in accordance with the terms of that license and with the |
|
301 |
inclusion of the above copyright notice. This software may not |
|
302 |
be provided or otherwise made available to, or used by, any |
|
303 |
other person. No title to or ownership of the software is |
|
304 |
hereby transferred. |
|
305 |
" |
|
306 |
! |
|
307 |
||
4051 | 308 |
documentation |
309 |
" |
|
310 |
Generate a SHA-1 hash value as defined in |
|
311 |
NIST, FIPS PUB 180-1: Secure Hash Standard, April 1995. |
|
312 |
This may be used as checksum |
|
313 |
or for generating cryptographic signatures. |
|
314 |
||
4926 | 315 |
performance: roughly 9580 Kb/s on a 400Mhz PIII |
5056 | 316 |
3970 Kb/s on a 300Mhz Sparc |
4916
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
317 |
|
4051 | 318 |
[author:] |
5056 | 319 |
Stefan Vogel |
4051 | 320 |
|
321 |
[see also:] |
|
5056 | 322 |
MD5Stream |
4051 | 323 |
|
324 |
[class variables:] |
|
5056 | 325 |
HashSize size of returned hash value |
326 |
ContextSize (implementation) size of hash context |
|
4051 | 327 |
|
328 |
[instance variables:] |
|
5056 | 329 |
hashContext (implementation) |
330 |
internal buffer for computation of the hash value |
|
4051 | 331 |
" |
332 |
! |
|
333 |
||
334 |
examples |
|
335 |
" |
|
7824 | 336 |
Test Vectors (from FIPS PUB 180-1) |
337 |
||
338 |
'abc' |
|
339 |
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D |
|
4051 | 340 |
|
7824 | 341 |
'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' |
342 |
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 |
|
343 |
||
344 |
A million repetitions of 'a' |
|
345 |
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F |
|
346 |
||
347 |
[exBegin] |
|
4051 | 348 |
|hashStream| |
349 |
||
4798 | 350 |
hashStream := SHA1Stream new. |
4051 | 351 |
hashStream nextPut:'abc'. |
352 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
353 |
hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'. |
|
354 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
7824 | 355 |
[exEnd] |
7042 | 356 |
|
7824 | 357 |
[exBegin] |
7042 | 358 |
|hashValue| |
4051 | 359 |
|
7042 | 360 |
hahValue := SHA1Stream hashValueOf:'abc'. |
361 |
hashValue printOn:Transcript base:16. Transcript cr. |
|
7824 | 362 |
[exEnd] |
7042 | 363 |
|
7824 | 364 |
[exBegin] |
4051 | 365 |
|hashStream| |
366 |
||
4798 | 367 |
hashStream := SHA1Stream new. |
4051 | 368 |
hashStream nextPut:'abc' asByteArray. |
369 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
370 |
hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' asByteArray. |
|
371 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
372 |
||
7824 | 373 |
[exEnd] |
4051 | 374 |
|
7824 | 375 |
[exBegin] |
4051 | 376 |
|hashStream| |
377 |
||
4798 | 378 |
hashStream := SHA1Stream new. |
4051 | 379 |
1000000 timesRepeat:[ hashStream nextPut:$a ]. |
380 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
7824 | 381 |
[exEnd] |
4051 | 382 |
|
7824 | 383 |
[exBegin] |
4051 | 384 |
|hashStream| |
385 |
||
4798 | 386 |
hashStream := SHA1Stream new. |
4051 | 387 |
hashStream nextPut:'a'. |
388 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
7824 | 389 |
[exEnd] |
4051 | 390 |
|
7824 | 391 |
[exBegin] |
4051 | 392 |
|hashStream| |
393 |
||
4798 | 394 |
hashStream := SHA1Stream new. |
4051 | 395 |
hashStream nextPut:$a. |
396 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
7824 | 397 |
[exEnd] |
4916
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
398 |
|
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
399 |
timing throughput: |
7824 | 400 |
[exBegin] |
4916
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
401 |
|hashStream n t| |
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
402 |
|
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
403 |
hashStream := SHA1Stream new. |
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
404 |
n := 1000000. |
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
405 |
t := Time millisecondsToRun:[ |
7824 | 406 |
n timesRepeat:[ |
407 |
hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
|
408 |
]. |
|
409 |
]. |
|
4916
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
410 |
t := (t / 1000) asFloat. |
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
411 |
Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'. |
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
412 |
Transcript show:(n*50/1024 / t); showCR:' Kb/s' |
7824 | 413 |
[exEnd] |
4051 | 414 |
" |
415 |
! ! |
|
416 |
||
417 |
!SHA1Stream class methodsFor:'initialization'! |
|
418 |
||
419 |
initialize |
|
420 |
|ctxSize| |
|
421 |
||
422 |
%{ |
|
423 |
ctxSize = __MKSMALLINT(sizeof(SHA1_CTX)); |
|
424 |
%}. |
|
425 |
ContextSize := ctxSize. |
|
426 |
HashSize := 20. |
|
427 |
||
428 |
" |
|
5056 | 429 |
self initialize |
4051 | 430 |
" |
431 |
! ! |
|
432 |
||
433 |
!SHA1Stream class methodsFor:'queries'! |
|
434 |
||
435 |
blockSize |
|
436 |
"return the block size used internally by the compression function" |
|
437 |
||
438 |
^ 64 |
|
439 |
||
440 |
"Created: / 18.3.1999 / 08:37:10 / stefan" |
|
441 |
! |
|
442 |
||
443 |
hashSize |
|
444 |
"return the size of the hashvalue returned by instances of this class" |
|
445 |
||
446 |
^ HashSize |
|
447 |
||
448 |
"Modified: / 18.3.1999 / 07:54:22 / stefan" |
|
449 |
! ! |
|
450 |
||
7042 | 451 |
!SHA1Stream class methodsFor:'testing'! |
452 |
||
453 |
testVector |
|
454 |
"Test Vectors (from FIPS PUB 180-1)" |
|
455 |
||
456 |
^ #( |
|
7824 | 457 |
('abc' |
458 |
#[16rA9 16r99 16r3E 16r36 16r47 16r06 16r81 16r6A 16rBA 16r3E 16r25 16r71 |
|
459 |
16r78 16r50 16rC2 16r6C 16r9C 16rD0 16rD8 16r9D]) |
|
7042 | 460 |
|
7824 | 461 |
('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' |
462 |
#[16r84 16r98 16r3E 16r44 16r1C 16r3B 16rD2 16r6E 16rBA 16rAE 16r4A 16rA1 |
|
463 |
16rF9 16r51 16r29 16rE5 16rE5 16r46 16r70 16rF1]) |
|
7042 | 464 |
) copyWith: |
7824 | 465 |
(Array with:(String new:1000000 withAll:$a) |
466 |
with:#[16r34 16rAA 16r97 16r3C 16rD4 16rC4 16rDA 16rA4 16rF6 16r1E 16rEB 16r2B |
|
467 |
16rDB 16rAD 16r27 16r31 16r65 16r34 16r01 16r6F]) |
|
7042 | 468 |
|
469 |
" |
|
470 |
self test |
|
471 |
" |
|
472 |
! ! |
|
473 |
||
4051 | 474 |
!SHA1Stream methodsFor:'initialization'! |
475 |
||
476 |
initialize |
|
477 |
||
478 |
hashContext := ByteArray new:ContextSize. |
|
479 |
self reset |
|
480 |
||
481 |
"Created: / 17.3.1999 / 16:11:37 / stefan" |
|
482 |
"Modified: / 18.3.1999 / 07:56:46 / stefan" |
|
483 |
! ! |
|
484 |
||
485 |
!SHA1Stream methodsFor:'positioning'! |
|
486 |
||
487 |
reset |
|
488 |
"reset the stream in order to compute a new hash value" |
|
489 |
||
490 |
%{ |
|
491 |
if (__isNonNilObject(__INST(hashContext)) && |
|
492 |
__qClass(__INST(hashContext)) == @global(ByteArray) && |
|
493 |
__byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX) |
|
494 |
) { |
|
5056 | 495 |
SHA1_CTX *ctx = |
496 |
(SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
|
4051 | 497 |
|
5056 | 498 |
SHA1Init(ctx); |
499 |
RETURN(self); |
|
4051 | 500 |
} |
501 |
%}. |
|
502 |
^ self primitiveFailed |
|
503 |
||
504 |
"Created: / 18.3.1999 / 07:59:02 / stefan" |
|
505 |
! ! |
|
506 |
||
507 |
!SHA1Stream methodsFor:'queries'! |
|
508 |
||
509 |
hashValue |
|
510 |
"Get the value hashed so far. |
|
511 |
The context is kept, so that more objects may be hashed after |
|
512 |
retrieving a hash value" |
|
513 |
||
514 |
||
515 |
|digest| |
|
516 |
||
517 |
digest := ByteArray new:HashSize. |
|
518 |
||
519 |
%{ |
|
520 |
if (__isNonNilObject(__INST(hashContext)) && |
|
5056 | 521 |
__qClass(__INST(hashContext)) == @global(ByteArray) && |
522 |
__byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX) && |
|
523 |
__isNonNilObject(digest) && |
|
524 |
__qClass(digest) == @global(ByteArray) && |
|
525 |
__byteArraySize(digest) == 20 |
|
4051 | 526 |
) { |
5056 | 527 |
SHA1_CTX *ctx = |
528 |
(SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
|
529 |
SHA1_CTX copyContext; |
|
4051 | 530 |
|
5056 | 531 |
memcpy(©Context, ctx, sizeof(copyContext)); |
532 |
SHA1Final(__ByteArrayInstPtr(digest)->ba_element, ©Context); |
|
533 |
RETURN(digest); |
|
4051 | 534 |
} |
535 |
%}. |
|
536 |
||
537 |
^ self primitiveFailed |
|
538 |
||
539 |
"Created: / 17.3.1999 / 16:13:12 / stefan" |
|
540 |
"Modified: / 18.3.1999 / 08:00:54 / stefan" |
|
541 |
! ! |
|
542 |
||
543 |
!SHA1Stream methodsFor:'writing'! |
|
544 |
||
545 |
nextPut:anObject |
|
546 |
"update our hash value for anObject. |
|
547 |
anObject may be a String, a Character, a Smallinteger or an Array of primitive |
|
548 |
types like ByteArray. |
|
549 |
" |
|
550 |
||
551 |
|ret| |
|
552 |
||
553 |
%{ |
|
554 |
if (__isNonNilObject(__INST(hashContext)) && |
|
555 |
__qClass(__INST(hashContext)) == @global(ByteArray) && |
|
556 |
__byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX) |
|
557 |
) { |
|
558 |
SHA1_CTX *ctx = |
|
559 |
(SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
|
560 |
||
561 |
if (__isNonNilObject(anObject)) { |
|
562 |
OBJ cls =__qClass(anObject); |
|
563 |
INT mask = (INT)(__ClassInstPtr(cls)->c_flags) & __MASKSMALLINT(ARRAYMASK); |
|
564 |
||
565 |
if (cls == @global(String) || cls == @global(Symbol)) { |
|
566 |
/* String: omit leading '\0' */ |
|
567 |
||
568 |
SHA1Update(ctx, __StringInstPtr(anObject)->s_element, __stringSize(anObject)); |
|
569 |
} else if (mask != __MASKSMALLINT(POINTERARRAY) && |
|
570 |
mask != __MASKSMALLINT(WKPOINTERARRAY) && |
|
571 |
mask != __MASKSMALLINT(0) |
|
572 |
) { |
|
573 |
/* Byte|Integer|.... Array */ |
|
574 |
||
575 |
register int n; |
|
576 |
char *pFirst; |
|
577 |
||
578 |
n /* nInstVars */ = __intVal(__ClassInstPtr(cls)->c_ninstvars); |
|
579 |
n /* nInstBytes */ = OHDR_SIZE + __OBJS2BYTES__(n /* nInstVars */); |
|
580 |
pFirst = (char *)(__InstPtr(anObject)) + n /* nInstBytes */; |
|
581 |
n /* nbytes */ = __qSize(anObject) - n /* nInstBytes */; |
|
582 |
SHA1Update(ctx, pFirst, n); |
|
583 |
} else if (cls == @global(Character)) { |
|
584 |
/* Character */ |
|
585 |
||
586 |
INT val = __intVal(_characterVal(anObject)); |
|
587 |
if (val > 255) { |
|
588 |
/* Two byte character */ |
|
589 |
short s = val; |
|
7045 | 590 |
SHA1Update(ctx, (char *)&s, 2); |
4051 | 591 |
} else { |
592 |
char c = val; |
|
593 |
SHA1Update(ctx, &c, 1); |
|
594 |
} |
|
595 |
} else { |
|
596 |
ret = false; |
|
597 |
} |
|
598 |
} else { |
|
599 |
if (anObject == nil) { |
|
600 |
ret = false; |
|
601 |
} else { |
|
602 |
/* SmallInteger */ |
|
603 |
||
604 |
INT i = __intVal(anObject); |
|
7045 | 605 |
SHA1Update(ctx, (char *)&i, sizeof(INT)); |
4051 | 606 |
} |
607 |
} |
|
608 |
} |
|
609 |
%}. |
|
610 |
||
611 |
ret notNil ifTrue:[ |
|
612 |
^ self primitiveFailed |
|
613 |
]. |
|
614 |
||
615 |
"Created: / 17.3.1999 / 16:14:13 / stefan" |
|
7042 | 616 |
! |
617 |
||
618 |
nextPutBytes:count from:anObject startingAt: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 |
|
621 |
(i.e. be a ByteArray, String, Float- or DoubleArray), |
|
622 |
or an externalBytes object (with known size)" |
|
623 |
||
624 |
%{ |
|
625 |
int len, offs; |
|
626 |
int objSize, nInstVars, nInstBytes; |
|
627 |
char *extPtr; |
|
628 |
OBJ oClass; |
|
629 |
||
630 |
if (__isNonNilObject(__INST(hashContext)) |
|
631 |
&&__qClass(__INST(hashContext)) == @global(ByteArray) |
|
632 |
&& __byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX) |
|
633 |
&& __bothSmallInteger(count, start) |
|
634 |
) { |
|
7824 | 635 |
SHA1_CTX *ctx = |
636 |
(SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
|
7042 | 637 |
|
7824 | 638 |
len = __intVal(count); |
639 |
offs = __intVal(start) - 1; |
|
7042 | 640 |
|
7824 | 641 |
oClass = __Class(anObject); |
642 |
if (oClass == ExternalBytes) { |
|
643 |
OBJ sz; |
|
7042 | 644 |
|
7824 | 645 |
nInstBytes = 0; |
646 |
extPtr = (char *)__externalBytesAddress(anObject); |
|
647 |
sz = __externalBytesSize(anObject); |
|
648 |
if (__isSmallInteger(sz)) { |
|
649 |
objSize = __intVal(sz); |
|
650 |
} else { |
|
651 |
objSize = 0; /* unknown */ |
|
652 |
} |
|
653 |
} else { |
|
654 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
655 |
case BYTEARRAY: |
|
656 |
case WORDARRAY: |
|
657 |
case LONGARRAY: |
|
658 |
case SWORDARRAY: |
|
659 |
case SLONGARRAY: |
|
660 |
case FLOATARRAY: |
|
661 |
case DOUBLEARRAY: |
|
662 |
break; |
|
663 |
default: |
|
664 |
goto bad; |
|
665 |
} |
|
666 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
667 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
668 |
objSize = __Size(anObject) - nInstBytes; |
|
669 |
extPtr = (char *)__byteArrayVal(anObject); |
|
670 |
} |
|
671 |
if ((offs >= 0) && (len >= 0) && (objSize >= (len + offs))) { |
|
672 |
SHA1Update(ctx, extPtr+offs, len); |
|
673 |
RETURN (count); |
|
674 |
} |
|
7042 | 675 |
} |
676 |
bad: ; |
|
677 |
%}. |
|
678 |
||
679 |
^ self primitiveFailed |
|
4051 | 680 |
! ! |
681 |
||
682 |
!SHA1Stream class methodsFor:'documentation'! |
|
683 |
||
684 |
version |
|
7824 | 685 |
^ '$Header: /cvs/stx/stx/libbasic/SHA1Stream.st,v 1.11 2004-01-08 09:47:52 cg Exp $' |
4051 | 686 |
! ! |
7042 | 687 |
|
4051 | 688 |
SHA1Stream initialize! |