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