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