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