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