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