author | Claus Gittinger <cg@exept.de> |
Fri, 22 Mar 2019 13:53:23 +0100 | |
changeset 23953 | 43aef89d0166 |
parent 23952 | 59e081dd12dc |
child 23956 | 22c01d2b1186 |
permissions | -rw-r--r-- |
22563 | 1 |
"{ Encoding: utf8 }" |
2 |
||
4426 | 3 |
" |
15038 | 4 |
COPYRIGHT (c) 1999-2013 by eXept Software AG |
15907
b6a05f7d463f
use int32 instead of long (for 64bit machines)
Claus Gittinger <cg@exept.de>
parents:
15623
diff
changeset
|
5 |
All Rights Reserved |
4426 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
9369
b7f43a51c4ed
Moved SHA1Stream from exept:libcrypt to stx:libbasic
Stefan Vogel <sv@exept.de>
parents:
7824
diff
changeset
|
14 |
"{ Package: 'stx:libbasic' }" |
4051 | 15 |
|
19160 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
4051 | 18 |
HashStream subclass:#SHA1Stream |
19 |
instanceVariableNames:'hashContext' |
|
20 |
classVariableNames:'HashSize ContextSize' |
|
21 |
poolDictionaries:'' |
|
12420 | 22 |
category:'System-Crypt-Hashing' |
4051 | 23 |
! |
24 |
||
25 |
!SHA1Stream primitiveDefinitions! |
|
26 |
%{ |
|
27 |
||
23953 | 28 |
#if defined(__LSBFIRST) || defined(__LSBFIRST__) |
7824 | 29 |
# ifndef LITTLE_ENDIAN |
30 |
# define LITTLE_ENDIAN /* This should be #define'd if true. */ |
|
31 |
# endif |
|
4051 | 32 |
#endif |
33 |
||
5056 | 34 |
#define SHA1HANDSOFF /* Copies data before messing with it. */ |
4051 | 35 |
|
22491 | 36 |
#ifndef _STDIO_H_INCLUDED_ |
37 |
# include <stdio.h> |
|
38 |
# define _STDIO_H_INCLUDED_ |
|
39 |
#endif |
|
40 |
||
41 |
#ifndef _STRING_H_INCLUDED_ |
|
42 |
# include <string.h> |
|
43 |
# define _STRING_H_INCLUDED_ |
|
44 |
#endif |
|
4051 | 45 |
|
46 |
typedef struct { |
|
15025
552103f2ae6e
64bit (mac) fix: long is not an int32 here
Claus Gittinger <cg@exept.de>
parents:
14791
diff
changeset
|
47 |
unsigned int32 state[5]; |
552103f2ae6e
64bit (mac) fix: long is not an int32 here
Claus Gittinger <cg@exept.de>
parents:
14791
diff
changeset
|
48 |
unsigned int32 count[2]; |
4051 | 49 |
unsigned char buffer[64]; |
50 |
} SHA1_CTX; |
|
51 |
||
23952 | 52 |
#if defined(__GNUC__) || defined(__CLANG__) |
53 |
# define STATIC_INLINE static inline |
|
54 |
#endif |
|
55 |
||
7824 | 56 |
#if USE_ANSI_C |
23952 | 57 |
|
15907
b6a05f7d463f
use int32 instead of long (for 64bit machines)
Claus Gittinger <cg@exept.de>
parents:
15623
diff
changeset
|
58 |
void SHA1Transform(unsigned int32 /* long */ state[5], unsigned char buffer[64]); |
23952 | 59 |
STATIC_INLINE void SHA1Init(SHA1_CTX* context); |
7824 | 60 |
void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len); |
61 |
void SHA1Final(unsigned char digest[20], SHA1_CTX* context); |
|
23952 | 62 |
|
7824 | 63 |
#else /* general: compiles everywhere */ |
23952 | 64 |
|
7824 | 65 |
# define SHA1Init __SHA1Init |
66 |
# define SHA1Update __SHA1Update |
|
67 |
# define SHA1Final __SHA2Final |
|
4051 | 68 |
|
23952 | 69 |
STATIC_INLINE void SHA1Init(); |
7824 | 70 |
void SHA1Update(); |
71 |
void SHA1Final(); |
|
23952 | 72 |
|
7824 | 73 |
#endif /* USE_ANSI_C */ |
23952 | 74 |
|
4051 | 75 |
%} |
76 |
! ! |
|
77 |
||
78 |
!SHA1Stream primitiveFunctions! |
|
23953 | 79 |
|
80 |
%{ |
|
81 |
||
82 |
/* |
|
83 |
* SHA-1 in C |
|
84 |
* By Steve Reid <steve@edmweb.com> |
|
85 |
* 100% Public Domain |
|
86 |
*/ |
|
87 |
||
88 |
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) |
|
89 |
||
90 |
/* blk0() and blk() perform the initial expand. */ |
|
91 |
/* I got the idea of expanding during the round function from SSLeay */ |
|
92 |
#ifdef LITTLE_ENDIAN |
|
93 |
# define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ |
|
94 |
|(rol(block->l[i],8)&0x00FF00FF)) |
|
95 |
#else |
|
96 |
# define blk0(i) block->l[i] |
|
97 |
#endif |
|
98 |
||
99 |
#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ |
|
100 |
^block->l[(i+2)&15]^block->l[i&15],1)) |
|
101 |
||
102 |
/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ |
|
103 |
#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); |
|
104 |
#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); |
|
105 |
#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); |
|
106 |
#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); |
|
107 |
#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); |
|
108 |
||
109 |
/* |
|
110 |
* Hash a single 512-bit block. This is the core of the algorithm. |
|
111 |
*/ |
|
112 |
static void |
|
113 |
#if USE_ANSI_C |
|
114 |
SHA1Transform (unsigned int32 state[5], unsigned char buffer[64]) |
|
115 |
#else |
|
116 |
SHA1Transform (state, buffer) |
|
117 |
unsigned int32 state[5]; |
|
118 |
unsigned char buffer[64]; |
|
119 |
#endif |
|
120 |
{ |
|
121 |
unsigned int32 a, b, c, d, e; |
|
122 |
typedef union { |
|
123 |
unsigned char c[64]; |
|
124 |
unsigned int32 /* long */ l[16]; |
|
125 |
} CHAR64LONG16; |
|
126 |
CHAR64LONG16* block; |
|
127 |
#ifdef SHA1HANDSOFF |
|
128 |
static unsigned char workspace[64]; |
|
129 |
block = (CHAR64LONG16*)workspace; |
|
130 |
memcpy(block, buffer, 64); |
|
131 |
#else |
|
132 |
block = (CHAR64LONG16*)buffer; |
|
133 |
#endif |
|
134 |
/* Copy context->state[] to working vars */ |
|
135 |
a = state[0]; |
|
136 |
b = state[1]; |
|
137 |
c = state[2]; |
|
138 |
d = state[3]; |
|
139 |
e = state[4]; |
|
140 |
/* 4 rounds of 20 operations each. Loop unrolled. */ |
|
141 |
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); |
|
142 |
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); |
|
143 |
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); |
|
144 |
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); |
|
145 |
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); |
|
146 |
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); |
|
147 |
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); |
|
148 |
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); |
|
149 |
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); |
|
150 |
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); |
|
151 |
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); |
|
152 |
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); |
|
153 |
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); |
|
154 |
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); |
|
155 |
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); |
|
156 |
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); |
|
157 |
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); |
|
158 |
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); |
|
159 |
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); |
|
160 |
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); |
|
161 |
/* Add the working vars back into context.state[] */ |
|
162 |
state[0] += a; |
|
163 |
state[1] += b; |
|
164 |
state[2] += c; |
|
165 |
state[3] += d; |
|
166 |
state[4] += e; |
|
167 |
/* Wipe variables */ |
|
168 |
a = b = c = d = e = 0; |
|
169 |
} |
|
170 |
||
171 |
/* |
|
172 |
* SHA1Init - Initialize new context |
|
173 |
*/ |
|
174 |
STATIC_INLINE void |
|
175 |
#if USE_ANSI_C |
|
176 |
SHA1Init(SHA1_CTX* context) |
|
177 |
#else |
|
178 |
SHA1Init(context) |
|
179 |
SHA1_CTX* context; |
|
180 |
#endif |
|
181 |
{ |
|
182 |
/* SHA1 initialization constants */ |
|
183 |
context->state[0] = 0x67452301; |
|
184 |
context->state[1] = 0xEFCDAB89; |
|
185 |
context->state[2] = 0x98BADCFE; |
|
186 |
context->state[3] = 0x10325476; |
|
187 |
context->state[4] = 0xC3D2E1F0; |
|
188 |
context->count[0] = context->count[1] = 0; |
|
189 |
} |
|
190 |
||
4051 | 191 |
|
23953 | 192 |
/* |
193 |
* Run your data through this. |
|
194 |
*/ |
|
195 |
void |
|
196 |
#if USE_ANSI_C |
|
197 |
SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len) |
|
198 |
#else |
|
199 |
SHA1Update(context, data, len) |
|
200 |
SHA1_CTX* context; |
|
201 |
unsigned char* data; |
|
202 |
unsigned int len; |
|
203 |
#endif |
|
204 |
{ |
|
205 |
unsigned int i, j; |
|
206 |
||
207 |
j = (context->count[0] >> 3) & 63; |
|
208 |
if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++; |
|
209 |
context->count[1] += (len >> 29); |
|
210 |
if ((j + len) > 63) { |
|
211 |
memcpy(&context->buffer[j], data, (i = 64-j)); |
|
212 |
SHA1Transform(context->state, context->buffer); |
|
213 |
for ( ; i + 63 < len; i += 64) { |
|
214 |
SHA1Transform(context->state, &data[i]); |
|
215 |
} |
|
216 |
j = 0; |
|
217 |
} |
|
218 |
else i = 0; |
|
219 |
memcpy(&context->buffer[j], &data[i], len - i); |
|
220 |
} |
|
221 |
||
222 |
||
223 |
/* |
|
224 |
* Add padding and return the message digest. |
|
225 |
*/ |
|
226 |
void |
|
227 |
#if USE_ANSI_C |
|
228 |
SHA1Final(unsigned char digest[20], SHA1_CTX* context) |
|
229 |
#else |
|
230 |
SHA1Final(digest, context) |
|
231 |
unsigned char digest[20]; |
|
232 |
SHA1_CTX* context; |
|
233 |
#endif |
|
234 |
{ |
|
235 |
unsigned int32 i, j; |
|
236 |
unsigned char finalcount[8]; |
|
237 |
||
238 |
for (i = 0; i < 8; i++) { |
|
239 |
finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] |
|
240 |
>> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ |
|
241 |
} |
|
242 |
SHA1Update(context, (unsigned char *)"\200", 1); |
|
243 |
while ((context->count[0] & 504) != 448) { |
|
244 |
SHA1Update(context, (unsigned char *)"\0", 1); |
|
245 |
} |
|
246 |
SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ |
|
247 |
for (i = 0; i < 20; i++) { |
|
248 |
digest[i] = (unsigned char) |
|
249 |
((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); |
|
250 |
} |
|
251 |
/* Wipe variables */ |
|
252 |
i = j = 0; |
|
253 |
memset(context->buffer, 0, 64); |
|
254 |
memset(context->state, 0, 20); |
|
255 |
memset(context->count, 0, 8); |
|
256 |
memset(&finalcount, 0, 8); |
|
257 |
#ifdef SHA1HANDSOFF /* make SHA1Transform overwrite it's own static vars */ |
|
258 |
SHA1Transform(context->state, context->buffer); |
|
259 |
#endif |
|
260 |
} |
|
261 |
||
262 |
#if 0 |
|
263 |
/*************************************************************/ |
|
264 |
||
265 |
/* |
|
266 |
* SHA1 test program |
|
267 |
*/ |
|
268 |
||
269 |
int main(int argc, char** argv) |
|
270 |
{ |
|
271 |
int i, j; |
|
272 |
SHA1_CTX context; |
|
273 |
unsigned char digest[20], buffer[16384]; |
|
274 |
FILE* file; |
|
275 |
||
276 |
if (argc > 2) { |
|
277 |
puts("Public domain SHA-1 implementation - by Steve Reid <steve@edmweb.com>"); |
|
278 |
puts("Produces the SHA-1 hash of a file, or stdin if no file is specified."); |
|
279 |
exit(0); |
|
280 |
} |
|
281 |
if (argc < 2) { |
|
282 |
file = stdin; |
|
283 |
} |
|
284 |
else { |
|
285 |
if (!(file = fopen(argv[1], "rb"))) { |
|
286 |
fputs("Unable to open file.", stderr); |
|
287 |
exit(-1); |
|
288 |
} |
|
289 |
} |
|
290 |
SHA1Init(&context); |
|
291 |
while (!feof(file)) { /* note: what if ferror(file) */ |
|
292 |
i = fread(buffer, 1, 16384, file); |
|
293 |
SHA1Update(&context, buffer, i); |
|
294 |
} |
|
295 |
SHA1Final(digest, &context); |
|
296 |
fclose(file); |
|
297 |
for (i = 0; i < 5; i++) { |
|
298 |
for (j = 0; j < 4; j++) { |
|
299 |
printf("%02X", digest[i*4+j]); |
|
300 |
} |
|
301 |
putchar(' '); |
|
302 |
} |
|
303 |
putchar('\n'); |
|
304 |
exit(0); |
|
305 |
} |
|
306 |
#endif |
|
4051 | 307 |
|
308 |
%} |
|
309 |
! ! |
|
310 |
||
311 |
!SHA1Stream class methodsFor:'documentation'! |
|
312 |
||
4426 | 313 |
copyright |
314 |
" |
|
15038 | 315 |
COPYRIGHT (c) 1999-2013 by eXept Software AG |
15907
b6a05f7d463f
use int32 instead of long (for 64bit machines)
Claus Gittinger <cg@exept.de>
parents:
15623
diff
changeset
|
316 |
All Rights Reserved |
4426 | 317 |
|
318 |
This software is furnished under a license and may be used |
|
319 |
only in accordance with the terms of that license and with the |
|
320 |
inclusion of the above copyright notice. This software may not |
|
321 |
be provided or otherwise made available to, or used by, any |
|
322 |
other person. No title to or ownership of the software is |
|
323 |
hereby transferred. |
|
324 |
" |
|
325 |
! |
|
326 |
||
4051 | 327 |
documentation |
328 |
" |
|
329 |
Generate a SHA-1 hash value as defined in |
|
330 |
NIST, FIPS PUB 180-1: Secure Hash Standard, April 1995. |
|
13933 | 331 |
|
13935 | 332 |
Notice (2005): |
23953 | 333 |
Be aware that SHA-1 is considered broken and may not be appropriate in some applications. |
334 |
Especially it should no longer be used for security stuff. |
|
4051 | 335 |
|
12265 | 336 |
performance: roughly |
23953 | 337 |
200 Mb/s on a 2012 MAC Powerbook (2.6Ghz I7) |
338 |
150 Mb/s on a 2007 MAC Powerbook (2.6Ghz Duo) |
|
339 |
120 Mb/s on a 2.5Ghz 64X2 Athlon 4800+ (64bit) |
|
340 |
47400 Kb/s on a 2Ghz Duo (old measure) |
|
341 |
9580 Kb/s on a 400Mhz PIII |
|
342 |
3970 Kb/s on a 300Mhz Sparc |
|
4916
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
343 |
|
4051 | 344 |
[author:] |
23953 | 345 |
Stefan Vogel |
4051 | 346 |
|
347 |
[see also:] |
|
23953 | 348 |
MD5Stream |
349 |
SHA256Stream SHA512Stream (in libcrypt) |
|
4051 | 350 |
|
351 |
[class variables:] |
|
23953 | 352 |
HashSize size of returned hash value |
353 |
ContextSize (implementation) size of hash context |
|
4051 | 354 |
|
355 |
[instance variables:] |
|
23953 | 356 |
hashContext (implementation) |
357 |
internal buffer for computation of the hash value |
|
4051 | 358 |
" |
359 |
! |
|
360 |
||
361 |
examples |
|
362 |
" |
|
10781 | 363 |
Test Vectors (from FIPS PUB 180-1); results are: |
7824 | 364 |
|
23953 | 365 |
[exBegin] |
4051 | 366 |
|hashStream| |
367 |
||
4798 | 368 |
hashStream := SHA1Stream new. |
4051 | 369 |
hashStream nextPut:'abc'. |
370 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
371 |
hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'. |
|
372 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
23953 | 373 |
[exEnd] |
19160 | 374 |
|
23953 | 375 |
[exBegin] |
19160 | 376 |
|hashValue| |
7042 | 377 |
|
19160 | 378 |
hashValue := SHA1Stream hashValueOf:'hello world'. |
379 |
self assert:(hashValue hexPrintString = '2AAE6C35C94FCFB415DBE95F408B9CE91EE846ED') |
|
23953 | 380 |
[exEnd] |
381 |
||
382 |
[exBegin] |
|
7042 | 383 |
|hashValue| |
4051 | 384 |
|
15038 | 385 |
hashValue := SHA1Stream hashValueOf:'abc'. |
7042 | 386 |
hashValue printOn:Transcript base:16. Transcript cr. |
23953 | 387 |
[exEnd] |
7042 | 388 |
|
23953 | 389 |
[exBegin] |
4051 | 390 |
|hashStream| |
391 |
||
4798 | 392 |
hashStream := SHA1Stream new. |
4051 | 393 |
hashStream nextPut:'abc' asByteArray. |
394 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
395 |
hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' asByteArray. |
|
396 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
397 |
||
23953 | 398 |
[exEnd] |
4051 | 399 |
|
23953 | 400 |
[exBegin] |
4051 | 401 |
|hashStream| |
402 |
||
4798 | 403 |
hashStream := SHA1Stream new. |
4051 | 404 |
1000000 timesRepeat:[ hashStream nextPut:$a ]. |
405 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
23953 | 406 |
[exEnd] |
4051 | 407 |
|
23953 | 408 |
[exBegin] |
4051 | 409 |
|hashStream| |
410 |
||
4798 | 411 |
hashStream := SHA1Stream new. |
4051 | 412 |
hashStream nextPut:'a'. |
413 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
23953 | 414 |
[exEnd] |
4051 | 415 |
|
23953 | 416 |
[exBegin] |
4051 | 417 |
|hashStream| |
418 |
||
4798 | 419 |
hashStream := SHA1Stream new. |
4051 | 420 |
hashStream nextPut:$a. |
421 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
23953 | 422 |
[exEnd] |
4916
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
423 |
|
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
424 |
timing throughput: |
23953 | 425 |
[exBegin] |
4916
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
426 |
|hashStream n t| |
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
427 |
|
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
428 |
hashStream := SHA1Stream new. |
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
429 |
n := 1000000. |
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
430 |
t := Time millisecondsToRun:[ |
23953 | 431 |
n timesRepeat:[ |
432 |
hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
|
433 |
]. |
|
434 |
]. |
|
4916
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
435 |
t := (t / 1000) asFloat. |
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
436 |
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
|
437 |
Transcript show:(n*50/1024 / t); showCR:' Kb/s' |
23953 | 438 |
[exEnd] |
4051 | 439 |
" |
19143 | 440 |
! |
441 |
||
442 |
performance |
|
443 |
" |
|
23952 | 444 |
CPU cc algo mb/sec |
19143 | 445 |
|
23952 | 446 |
MAC (2010 macbook; 2.7Ghz Duo) clang -O2 slow 128.5 |
23953 | 447 |
132 |
23952 | 448 |
MAC (2012 macbook; 2.6Ghz I7) clang -O2 203.5 |
449 |
||
19143 | 450 |
|
451 |
||
452 |
||
453 |
timing throughput: |
|
23953 | 454 |
[exBegin] |
19143 | 455 |
|hashStream n t| |
456 |
||
457 |
hashStream := SHA1Stream new. |
|
458 |
n := 1000000. |
|
459 |
t := Time millisecondsToRun:[ |
|
23953 | 460 |
n timesRepeat:[ |
461 |
hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
|
462 |
]. |
|
463 |
]. |
|
19143 | 464 |
t := (t / 1000) asFloat. |
465 |
Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'. |
|
466 |
Transcript show:(n*50/1024 / t); showCR:' Kb/s' |
|
23953 | 467 |
[exEnd] |
19143 | 468 |
" |
4051 | 469 |
! ! |
470 |
||
471 |
!SHA1Stream class methodsFor:'initialization'! |
|
472 |
||
473 |
initialize |
|
474 |
|ctxSize| |
|
475 |
||
476 |
%{ |
|
477 |
ctxSize = __MKSMALLINT(sizeof(SHA1_CTX)); |
|
478 |
%}. |
|
479 |
ContextSize := ctxSize. |
|
480 |
HashSize := 20. |
|
481 |
||
482 |
" |
|
5056 | 483 |
self initialize |
4051 | 484 |
" |
485 |
! ! |
|
486 |
||
487 |
!SHA1Stream class methodsFor:'queries'! |
|
488 |
||
15623
1c229b466d52
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15038
diff
changeset
|
489 |
hashBlockSize |
4051 | 490 |
"return the block size used internally by the compression function" |
15907
b6a05f7d463f
use int32 instead of long (for 64bit machines)
Claus Gittinger <cg@exept.de>
parents:
15623
diff
changeset
|
491 |
|
4051 | 492 |
^ 64 |
493 |
||
494 |
"Created: / 18.3.1999 / 08:37:10 / stefan" |
|
495 |
! |
|
496 |
||
497 |
hashSize |
|
498 |
"return the size of the hashvalue returned by instances of this class" |
|
499 |
||
500 |
^ HashSize |
|
501 |
||
502 |
"Modified: / 18.3.1999 / 07:54:22 / stefan" |
|
503 |
! ! |
|
504 |
||
505 |
!SHA1Stream methodsFor:'initialization'! |
|
506 |
||
507 |
initialize |
|
508 |
||
509 |
hashContext := ByteArray new:ContextSize. |
|
510 |
self reset |
|
511 |
||
512 |
"Created: / 17.3.1999 / 16:11:37 / stefan" |
|
513 |
"Modified: / 18.3.1999 / 07:56:46 / stefan" |
|
514 |
! ! |
|
515 |
||
516 |
!SHA1Stream methodsFor:'positioning'! |
|
517 |
||
518 |
reset |
|
519 |
"reset the stream in order to compute a new hash value" |
|
520 |
||
521 |
%{ |
|
14669 | 522 |
if (__isByteArray(__INST(hashContext)) && |
4051 | 523 |
__byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX) |
524 |
) { |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
13935
diff
changeset
|
525 |
SHA1_CTX *ctx = (SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
4051 | 526 |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
13935
diff
changeset
|
527 |
SHA1Init(ctx); |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
13935
diff
changeset
|
528 |
RETURN(self); |
4051 | 529 |
} |
530 |
%}. |
|
531 |
^ self primitiveFailed |
|
532 |
||
13930 | 533 |
"Created: / 18-03-1999 / 07:59:02 / stefan" |
534 |
"Modified: / 12-01-2012 / 12:21:23 / cg" |
|
22563 | 535 |
! |
536 |
||
537 |
seedWith:fiveWordVector |
|
538 |
"seed the stream with 5*16 (for testing)" |
|
539 |
||
540 |
|seedA seedB seedC seedD seedE| |
|
541 |
||
542 |
seedA := fiveWordVector at:1. |
|
543 |
seedB := fiveWordVector at:1. |
|
544 |
seedC := fiveWordVector at:1. |
|
545 |
seedD := fiveWordVector at:1. |
|
546 |
seedE := fiveWordVector at:1. |
|
547 |
self reset. |
|
548 |
%{ |
|
549 |
if (__isByteArray(__INST(hashContext)) |
|
550 |
&& (__byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX)) |
|
551 |
&& __isSmallInteger(seedA) |
|
552 |
&& __isSmallInteger(seedB) |
|
553 |
&& __isSmallInteger(seedC) |
|
554 |
&& __isSmallInteger(seedD) |
|
555 |
&& __isSmallInteger(seedE) |
|
556 |
) { |
|
23953 | 557 |
SHA1_CTX *ctx = (SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
22563 | 558 |
|
23953 | 559 |
ctx->state[0] = __intVal(seedA); |
560 |
ctx->state[1] = __intVal(seedB); |
|
561 |
ctx->state[2] = __intVal(seedC); |
|
562 |
ctx->state[3] = __intVal(seedD); |
|
563 |
ctx->state[4] = __intVal(seedE); |
|
564 |
RETURN(self); |
|
22563 | 565 |
} |
566 |
%}. |
|
567 |
^ self primitiveFailed |
|
4051 | 568 |
! ! |
569 |
||
570 |
!SHA1Stream methodsFor:'queries'! |
|
571 |
||
572 |
hashValue |
|
573 |
"Get the value hashed so far. |
|
574 |
The context is kept, so that more objects may be hashed after |
|
575 |
retrieving a hash value" |
|
576 |
||
577 |
||
578 |
|digest| |
|
579 |
||
580 |
digest := ByteArray new:HashSize. |
|
581 |
||
582 |
%{ |
|
14791 | 583 |
OBJ hcon; |
584 |
||
585 |
hcon = __INST(hashContext); |
|
586 |
if (__isByteArray(hcon) && |
|
587 |
__byteArraySize(hcon) == sizeof(SHA1_CTX) && |
|
14669 | 588 |
__isByteArray(digest) && |
5056 | 589 |
__byteArraySize(digest) == 20 |
4051 | 590 |
) { |
14791 | 591 |
SHA1_CTX *ctx = (SHA1_CTX *)(__ByteArrayInstPtr(hcon)->ba_element); |
5056 | 592 |
SHA1_CTX copyContext; |
14723 | 593 |
|
14791 | 594 |
memcpy(©Context, ctx, sizeof(SHA1_CTX)); |
5056 | 595 |
SHA1Final(__ByteArrayInstPtr(digest)->ba_element, ©Context); |
596 |
RETURN(digest); |
|
4051 | 597 |
} |
598 |
%}. |
|
599 |
||
600 |
^ self primitiveFailed |
|
601 |
||
602 |
"Created: / 17.3.1999 / 16:13:12 / stefan" |
|
603 |
"Modified: / 18.3.1999 / 08:00:54 / stefan" |
|
604 |
! ! |
|
605 |
||
606 |
!SHA1Stream methodsFor:'writing'! |
|
607 |
||
19400 | 608 |
nextPutByte:anInteger |
609 |
"update the hash value with anInteger <= 255." |
|
610 |
||
611 |
%{ |
|
612 |
if (__isSmallInteger(anInteger) && __intVal(anInteger) <= 255 |
|
613 |
&& __isByteArray(__INST(hashContext)) |
|
614 |
&& __byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX) |
|
615 |
) { |
|
23953 | 616 |
SHA1_CTX *ctx = (SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
617 |
unsigned char value = __intVal(anInteger); |
|
19400 | 618 |
|
23953 | 619 |
SHA1Update(ctx, &value, 1); |
620 |
RETURN(self); |
|
19400 | 621 |
} |
622 |
bad: ; |
|
623 |
%}. |
|
624 |
||
625 |
^ self primitiveFailed |
|
626 |
! |
|
627 |
||
7042 | 628 |
nextPutBytes:count from:anObject startingAt:start |
629 |
"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
|
630 |
The object must have non-pointer indexed instvars |
7042 | 631 |
(i.e. be a ByteArray, String, Float- or DoubleArray), |
632 |
or an externalBytes object (with known size)" |
|
633 |
||
634 |
%{ |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
13935
diff
changeset
|
635 |
INT len, offs; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
13935
diff
changeset
|
636 |
INT objSize; |
15989 | 637 |
int nInstBytes; |
7042 | 638 |
char *extPtr; |
639 |
||
14669 | 640 |
if (__isByteArray(__INST(hashContext)) |
7042 | 641 |
&& __byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX) |
642 |
&& __bothSmallInteger(count, start) |
|
643 |
) { |
|
19143 | 644 |
SHA1_CTX *ctx = (SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
7042 | 645 |
|
19143 | 646 |
len = __intVal(count); |
647 |
offs = __intVal(start) - 1; |
|
7042 | 648 |
|
19143 | 649 |
if (__isExternalBytesLike(anObject)) { |
650 |
OBJ sz; |
|
7042 | 651 |
|
19143 | 652 |
nInstBytes = 0; |
653 |
extPtr = (char *)__externalBytesAddress(anObject); |
|
654 |
if (extPtr == NULL) goto bad; |
|
655 |
sz = __externalBytesSize(anObject); |
|
656 |
if (__isSmallInteger(sz)) { |
|
657 |
objSize = __intVal(sz); |
|
658 |
} else { |
|
659 |
objSize = 0; /* unknown */ |
|
660 |
} |
|
661 |
} else { |
|
662 |
OBJ oClass = __Class(anObject); |
|
663 |
int nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
14723 | 664 |
|
19143 | 665 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
666 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
667 |
case BYTEARRAY: |
|
668 |
case WORDARRAY: |
|
669 |
case LONGARRAY: |
|
670 |
case SWORDARRAY: |
|
671 |
case SLONGARRAY: |
|
672 |
case FLOATARRAY: |
|
673 |
break; |
|
674 |
case DOUBLEARRAY: |
|
15989 | 675 |
#ifdef __NEED_DOUBLE_ALIGN |
19143 | 676 |
nInstBytes = (nInstBytes-1+__DOUBLE_ALIGN) &~ (__DOUBLE_ALIGN-1); |
15989 | 677 |
#endif |
19143 | 678 |
break; |
679 |
case LONGLONGARRAY: |
|
680 |
case SLONGLONGARRAY: |
|
15989 | 681 |
#ifdef __NEED_LONGLONG_ALIGN |
19143 | 682 |
nInstBytes = (nInstBytes-1+__LONGLONG_ALIGN) &~ (__LONGLONG_ALIGN-1); |
15989 | 683 |
#endif |
19143 | 684 |
break; |
685 |
default: |
|
686 |
goto bad; |
|
687 |
} |
|
688 |
// nInstBytes is the number of bytes occupied by pointer instance variables |
|
689 |
// subtract from size and add to byte-pointer |
|
690 |
objSize = __Size(anObject) - nInstBytes; |
|
691 |
extPtr = (char *)anObject + nInstBytes; |
|
692 |
} |
|
693 |
if ((offs >= 0) && (len >= 0) && (objSize >= (len + offs))) { |
|
694 |
SHA1Update(ctx, extPtr+offs, (unsigned int)len); |
|
695 |
RETURN (count); |
|
696 |
} |
|
7042 | 697 |
} |
698 |
bad: ; |
|
699 |
%}. |
|
700 |
||
701 |
^ self primitiveFailed |
|
4051 | 702 |
! ! |
703 |
||
704 |
!SHA1Stream class methodsFor:'documentation'! |
|
705 |
||
706 |
version |
|
19143 | 707 |
^ '$Header$' |
12205 | 708 |
! |
709 |
||
710 |
version_CVS |
|
19143 | 711 |
^ '$Header$' |
4051 | 712 |
! ! |
7042 | 713 |
|
14715 | 714 |
|
4051 | 715 |
SHA1Stream initialize! |