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