4426
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1999 by eXept Software AG
|
|
3 |
All Rights Reserved
|
|
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 |
|
|
30 |
/* #define SHA1HANDSOFF * Copies data before messing with it. */
|
|
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) {
|
|
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;
|
|
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++) {
|
|
226 |
finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
|
|
227 |
>> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
|
|
228 |
}
|
|
229 |
SHA1Update(context, (unsigned char *)"\200", 1);
|
|
230 |
while ((context->count[0] & 504) != 448) {
|
|
231 |
SHA1Update(context, (unsigned char *)"\0", 1);
|
|
232 |
}
|
|
233 |
SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
|
|
234 |
for (i = 0; i < 20; i++) {
|
|
235 |
digest[i] = (unsigned char)
|
|
236 |
((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
|
|
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) {
|
|
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);
|
|
264 |
}
|
|
265 |
if (argc < 2) {
|
|
266 |
file = stdin;
|
|
267 |
}
|
|
268 |
else {
|
|
269 |
if (!(file = fopen(argv[1], "rb"))) {
|
|
270 |
fputs("Unable to open file.", stderr);
|
|
271 |
exit(-1);
|
|
272 |
}
|
|
273 |
}
|
|
274 |
SHA1Init(&context);
|
|
275 |
while (!feof(file)) { /* note: what if ferror(file) */
|
|
276 |
i = fread(buffer, 1, 16384, file);
|
|
277 |
SHA1Update(&context, buffer, i);
|
|
278 |
}
|
|
279 |
SHA1Final(digest, &context);
|
|
280 |
fclose(file);
|
|
281 |
for (i = 0; i < 5; i++) {
|
|
282 |
for (j = 0; j < 4; j++) {
|
|
283 |
printf("%02X", digest[i*4+j]);
|
|
284 |
}
|
|
285 |
putchar(' ');
|
|
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
|
|
300 |
All Rights Reserved
|
|
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 |
|
|
321 |
[author:]
|
|
322 |
Stefan Vogel
|
|
323 |
|
|
324 |
[see also:]
|
|
325 |
MD5Stream
|
|
326 |
|
|
327 |
[class variables:]
|
|
328 |
HashSize size of returned hash value
|
|
329 |
ContextSize (implementation) size of hash context
|
|
330 |
|
|
331 |
[instance variables:]
|
|
332 |
hashContext (implementation)
|
|
333 |
internal buffer for computation of the hash value
|
|
334 |
"
|
|
335 |
|
|
336 |
|
|
337 |
|
|
338 |
!
|
|
339 |
|
|
340 |
examples
|
|
341 |
"
|
|
342 |
Test Vectors (from FIPS PUB 180-1)
|
|
343 |
'abc'
|
|
344 |
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
|
|
345 |
'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'
|
|
346 |
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
|
|
347 |
A million repetitions of 'a'
|
|
348 |
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
|
|
349 |
|
|
350 |
[exBegin]
|
|
351 |
|hashStream|
|
|
352 |
|
4798
|
353 |
hashStream := SHA1Stream new.
|
4051
|
354 |
hashStream nextPut:'abc'.
|
|
355 |
hashStream hashValue printOn:Transcript base:16. Transcript cr.
|
|
356 |
hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'.
|
|
357 |
hashStream hashValue printOn:Transcript base:16. Transcript cr.
|
|
358 |
[exEnd]
|
|
359 |
|
|
360 |
[exBegin]
|
|
361 |
|hashStream|
|
|
362 |
|
4798
|
363 |
hashStream := SHA1Stream new.
|
4051
|
364 |
hashStream nextPut:'abc' asByteArray.
|
|
365 |
hashStream hashValue printOn:Transcript base:16. Transcript cr.
|
|
366 |
hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' asByteArray.
|
|
367 |
hashStream hashValue printOn:Transcript base:16. Transcript cr.
|
|
368 |
|
|
369 |
[exEnd]
|
|
370 |
|
|
371 |
[exBegin]
|
|
372 |
|hashStream|
|
|
373 |
|
4798
|
374 |
hashStream := SHA1Stream new.
|
4051
|
375 |
1000000 timesRepeat:[ hashStream nextPut:$a ].
|
|
376 |
hashStream hashValue printOn:Transcript base:16. Transcript cr.
|
|
377 |
[exEnd]
|
|
378 |
|
|
379 |
[exBegin]
|
|
380 |
|hashStream|
|
|
381 |
|
4798
|
382 |
hashStream := SHA1Stream new.
|
4051
|
383 |
hashStream nextPut:'a'.
|
|
384 |
hashStream hashValue printOn:Transcript base:16. Transcript cr.
|
|
385 |
[exEnd]
|
|
386 |
|
|
387 |
[exBegin]
|
|
388 |
|hashStream|
|
|
389 |
|
4798
|
390 |
hashStream := SHA1Stream new.
|
4051
|
391 |
hashStream nextPut:$a.
|
|
392 |
hashStream hashValue printOn:Transcript base:16. Transcript cr.
|
|
393 |
[exEnd]
|
|
394 |
"
|
|
395 |
! !
|
|
396 |
|
|
397 |
!SHA1Stream class methodsFor:'initialization'!
|
|
398 |
|
|
399 |
initialize
|
|
400 |
|ctxSize|
|
|
401 |
|
|
402 |
%{
|
|
403 |
ctxSize = __MKSMALLINT(sizeof(SHA1_CTX));
|
|
404 |
%}.
|
|
405 |
ContextSize := ctxSize.
|
|
406 |
HashSize := 20.
|
|
407 |
|
|
408 |
"
|
|
409 |
self initialize
|
|
410 |
"
|
|
411 |
! !
|
|
412 |
|
|
413 |
!SHA1Stream class methodsFor:'queries'!
|
|
414 |
|
|
415 |
blockSize
|
|
416 |
"return the block size used internally by the compression function"
|
|
417 |
|
|
418 |
^ 64
|
|
419 |
|
|
420 |
"Created: / 18.3.1999 / 08:37:10 / stefan"
|
|
421 |
!
|
|
422 |
|
|
423 |
hashSize
|
|
424 |
"return the size of the hashvalue returned by instances of this class"
|
|
425 |
|
|
426 |
^ HashSize
|
|
427 |
|
|
428 |
"Modified: / 18.3.1999 / 07:54:22 / stefan"
|
|
429 |
! !
|
|
430 |
|
|
431 |
!SHA1Stream methodsFor:'initialization'!
|
|
432 |
|
|
433 |
initialize
|
|
434 |
|
|
435 |
hashContext := ByteArray new:ContextSize.
|
|
436 |
self reset
|
|
437 |
|
|
438 |
"Created: / 17.3.1999 / 16:11:37 / stefan"
|
|
439 |
"Modified: / 18.3.1999 / 07:56:46 / stefan"
|
|
440 |
! !
|
|
441 |
|
|
442 |
!SHA1Stream methodsFor:'positioning'!
|
|
443 |
|
|
444 |
reset
|
|
445 |
"reset the stream in order to compute a new hash value"
|
|
446 |
|
|
447 |
%{
|
|
448 |
if (__isNonNilObject(__INST(hashContext)) &&
|
|
449 |
__qClass(__INST(hashContext)) == @global(ByteArray) &&
|
|
450 |
__byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX)
|
|
451 |
) {
|
|
452 |
SHA1_CTX *ctx =
|
|
453 |
(SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element;
|
|
454 |
|
|
455 |
SHA1Init(ctx);
|
|
456 |
RETURN(self);
|
|
457 |
}
|
|
458 |
%}.
|
|
459 |
^ self primitiveFailed
|
|
460 |
|
|
461 |
"Created: / 18.3.1999 / 07:59:02 / stefan"
|
|
462 |
! !
|
|
463 |
|
|
464 |
!SHA1Stream methodsFor:'queries'!
|
|
465 |
|
|
466 |
hashValue
|
|
467 |
"Get the value hashed so far.
|
|
468 |
The context is kept, so that more objects may be hashed after
|
|
469 |
retrieving a hash value"
|
|
470 |
|
|
471 |
|
|
472 |
|digest|
|
|
473 |
|
|
474 |
digest := ByteArray new:HashSize.
|
|
475 |
|
|
476 |
%{
|
|
477 |
if (__isNonNilObject(__INST(hashContext)) &&
|
|
478 |
__qClass(__INST(hashContext)) == @global(ByteArray) &&
|
|
479 |
__byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX) &&
|
|
480 |
__isNonNilObject(digest) &&
|
|
481 |
__qClass(digest) == @global(ByteArray) &&
|
|
482 |
__byteArraySize(digest) == 20
|
|
483 |
) {
|
|
484 |
SHA1_CTX *ctx =
|
|
485 |
(SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element;
|
|
486 |
SHA1_CTX copyContext;
|
|
487 |
|
|
488 |
memcpy(©Context, ctx, sizeof(copyContext));
|
|
489 |
SHA1Final(__ByteArrayInstPtr(digest)->ba_element, ©Context);
|
|
490 |
RETURN(digest);
|
|
491 |
}
|
|
492 |
%}.
|
|
493 |
|
|
494 |
^ self primitiveFailed
|
|
495 |
|
|
496 |
"Created: / 17.3.1999 / 16:13:12 / stefan"
|
|
497 |
"Modified: / 18.3.1999 / 08:00:54 / stefan"
|
|
498 |
! !
|
|
499 |
|
|
500 |
!SHA1Stream methodsFor:'writing'!
|
|
501 |
|
|
502 |
nextPut:anObject
|
|
503 |
"update our hash value for anObject.
|
|
504 |
anObject may be a String, a Character, a Smallinteger or an Array of primitive
|
|
505 |
types like ByteArray.
|
|
506 |
"
|
|
507 |
|
|
508 |
|ret|
|
|
509 |
|
|
510 |
%{
|
|
511 |
if (__isNonNilObject(__INST(hashContext)) &&
|
|
512 |
__qClass(__INST(hashContext)) == @global(ByteArray) &&
|
|
513 |
__byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX)
|
|
514 |
) {
|
|
515 |
SHA1_CTX *ctx =
|
|
516 |
(SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element;
|
|
517 |
|
|
518 |
if (__isNonNilObject(anObject)) {
|
|
519 |
OBJ cls =__qClass(anObject);
|
|
520 |
INT mask = (INT)(__ClassInstPtr(cls)->c_flags) & __MASKSMALLINT(ARRAYMASK);
|
|
521 |
|
|
522 |
if (cls == @global(String) || cls == @global(Symbol)) {
|
|
523 |
/* String: omit leading '\0' */
|
|
524 |
|
|
525 |
SHA1Update(ctx, __StringInstPtr(anObject)->s_element, __stringSize(anObject));
|
|
526 |
} else if (mask != __MASKSMALLINT(POINTERARRAY) &&
|
|
527 |
mask != __MASKSMALLINT(WKPOINTERARRAY) &&
|
|
528 |
mask != __MASKSMALLINT(0)
|
|
529 |
) {
|
|
530 |
/* Byte|Integer|.... Array */
|
|
531 |
|
|
532 |
register int n;
|
|
533 |
char *pFirst;
|
|
534 |
|
|
535 |
n /* nInstVars */ = __intVal(__ClassInstPtr(cls)->c_ninstvars);
|
|
536 |
n /* nInstBytes */ = OHDR_SIZE + __OBJS2BYTES__(n /* nInstVars */);
|
|
537 |
pFirst = (char *)(__InstPtr(anObject)) + n /* nInstBytes */;
|
|
538 |
n /* nbytes */ = __qSize(anObject) - n /* nInstBytes */;
|
|
539 |
SHA1Update(ctx, pFirst, n);
|
|
540 |
} else if (cls == @global(Character)) {
|
|
541 |
/* Character */
|
|
542 |
|
|
543 |
INT val = __intVal(_characterVal(anObject));
|
|
544 |
if (val > 255) {
|
|
545 |
/* Two byte character */
|
|
546 |
short s = val;
|
|
547 |
SHA1Update(ctx, &s, 2);
|
|
548 |
} else {
|
|
549 |
char c = val;
|
|
550 |
SHA1Update(ctx, &c, 1);
|
|
551 |
}
|
|
552 |
} else {
|
|
553 |
ret = false;
|
|
554 |
}
|
|
555 |
} else {
|
|
556 |
if (anObject == nil) {
|
|
557 |
ret = false;
|
|
558 |
} else {
|
|
559 |
/* SmallInteger */
|
|
560 |
|
|
561 |
INT i = __intVal(anObject);
|
|
562 |
SHA1Update(ctx, &i, sizeof(INT));
|
|
563 |
}
|
|
564 |
}
|
|
565 |
}
|
|
566 |
%}.
|
|
567 |
|
|
568 |
ret notNil ifTrue:[
|
|
569 |
^ self primitiveFailed
|
|
570 |
].
|
|
571 |
|
|
572 |
"Created: / 17.3.1999 / 16:14:13 / stefan"
|
|
573 |
! !
|
|
574 |
|
|
575 |
!SHA1Stream class methodsFor:'documentation'!
|
|
576 |
|
|
577 |
version
|
4798
|
578 |
^ '$Header: /cvs/stx/stx/libbasic/SHA1Stream.st,v 1.4 1999-09-23 18:33:47 cg Exp $'
|
4051
|
579 |
! !
|
|
580 |
SHA1Stream initialize!
|