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