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