author | Claus Gittinger <cg@exept.de> |
Thu, 12 Jan 2012 17:36:53 +0100 | |
changeset 13934 | 4a07dd192fcf |
parent 13933 | b2c5cb0eebbd |
child 13935 | 0d7929973c93 |
permissions | -rw-r--r-- |
4426 | 1 |
" |
2 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
5056 | 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 { |
|
36 |
unsigned long state[5]; |
|
37 |
unsigned long count[2]; |
|
38 |
unsigned char buffer[64]; |
|
39 |
} SHA1_CTX; |
|
40 |
||
7824 | 41 |
#if USE_ANSI_C |
42 |
void SHA1Transform(unsigned long state[5], unsigned char buffer[64]); |
|
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 |
|
4051 | 93 |
SHA1Transform (unsigned long state[5], unsigned char buffer[64]) |
94 |
#else |
|
95 |
SHA1Transform (state, buffer) |
|
9369
b7f43a51c4ed
Moved SHA1Stream from exept:libcrypt to stx:libbasic
Stefan Vogel <sv@exept.de>
parents:
7824
diff
changeset
|
96 |
unsigned long state[5]; |
4051 | 97 |
unsigned char buffer[64]; |
98 |
#endif |
|
99 |
{ |
|
7824 | 100 |
unsigned long a, b, c, d, e; |
101 |
typedef union { |
|
102 |
unsigned char c[64]; |
|
103 |
unsigned long l[16]; |
|
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 |
{ |
|
7824 | 212 |
unsigned long i, j; |
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 |
" |
|
292 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
5056 | 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 |
||
310 |
Notice: |
|
311 |
Be aware that SHA-1 is considered broken and may not be appropriate in some applications. |
|
312 |
Especially it should no longer be used for security stuff. |
|
4051 | 313 |
|
12265 | 314 |
performance: roughly |
13933 | 315 |
47400 Kb/s on a 2Ghz Duo |
316 |
9580 Kb/s on a 400Mhz PIII |
|
317 |
3970 Kb/s on a 300Mhz Sparc |
|
4916
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
318 |
|
4051 | 319 |
[author:] |
13933 | 320 |
Stefan Vogel |
4051 | 321 |
|
322 |
[see also:] |
|
13934 | 323 |
MD5Stream SHA256Stream SHA512Stream |
4051 | 324 |
|
325 |
[class variables:] |
|
13933 | 326 |
HashSize size of returned hash value |
327 |
ContextSize (implementation) size of hash context |
|
4051 | 328 |
|
329 |
[instance variables:] |
|
13933 | 330 |
hashContext (implementation) |
331 |
internal buffer for computation of the hash value |
|
4051 | 332 |
" |
333 |
! |
|
334 |
||
335 |
examples |
|
336 |
" |
|
10781 | 337 |
Test Vectors (from FIPS PUB 180-1); results are: |
7824 | 338 |
|
12752 | 339 |
[exBegin] |
4051 | 340 |
|hashStream| |
341 |
||
4798 | 342 |
hashStream := SHA1Stream new. |
4051 | 343 |
hashStream nextPut:'abc'. |
344 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
345 |
hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'. |
|
346 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
12752 | 347 |
[exEnd] |
7042 | 348 |
|
12752 | 349 |
[exBegin] |
7042 | 350 |
|hashValue| |
4051 | 351 |
|
7042 | 352 |
hahValue := SHA1Stream hashValueOf:'abc'. |
353 |
hashValue printOn:Transcript base:16. Transcript cr. |
|
12752 | 354 |
[exEnd] |
7042 | 355 |
|
12752 | 356 |
[exBegin] |
4051 | 357 |
|hashStream| |
358 |
||
4798 | 359 |
hashStream := SHA1Stream new. |
4051 | 360 |
hashStream nextPut:'abc' asByteArray. |
361 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
362 |
hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' asByteArray. |
|
363 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
364 |
||
12752 | 365 |
[exEnd] |
4051 | 366 |
|
12752 | 367 |
[exBegin] |
4051 | 368 |
|hashStream| |
369 |
||
4798 | 370 |
hashStream := SHA1Stream new. |
4051 | 371 |
1000000 timesRepeat:[ hashStream nextPut:$a ]. |
372 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
12752 | 373 |
[exEnd] |
4051 | 374 |
|
12752 | 375 |
[exBegin] |
4051 | 376 |
|hashStream| |
377 |
||
4798 | 378 |
hashStream := SHA1Stream new. |
4051 | 379 |
hashStream nextPut:'a'. |
380 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
12752 | 381 |
[exEnd] |
4051 | 382 |
|
12752 | 383 |
[exBegin] |
4051 | 384 |
|hashStream| |
385 |
||
4798 | 386 |
hashStream := SHA1Stream new. |
4051 | 387 |
hashStream nextPut:$a. |
388 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
12752 | 389 |
[exEnd] |
4916
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
390 |
|
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
391 |
timing throughput: |
12752 | 392 |
[exBegin] |
4916
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
393 |
|hashStream n t| |
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
394 |
|
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
395 |
hashStream := SHA1Stream new. |
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
396 |
n := 1000000. |
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
397 |
t := Time millisecondsToRun:[ |
12752 | 398 |
n timesRepeat:[ |
399 |
hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
|
400 |
]. |
|
401 |
]. |
|
4916
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
402 |
t := (t / 1000) asFloat. |
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
403 |
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
|
404 |
Transcript show:(n*50/1024 / t); showCR:' Kb/s' |
12752 | 405 |
[exEnd] |
4051 | 406 |
" |
407 |
! ! |
|
408 |
||
409 |
!SHA1Stream class methodsFor:'initialization'! |
|
410 |
||
411 |
initialize |
|
412 |
|ctxSize| |
|
413 |
||
414 |
%{ |
|
415 |
ctxSize = __MKSMALLINT(sizeof(SHA1_CTX)); |
|
416 |
%}. |
|
417 |
ContextSize := ctxSize. |
|
418 |
HashSize := 20. |
|
419 |
||
420 |
" |
|
5056 | 421 |
self initialize |
4051 | 422 |
" |
423 |
! ! |
|
424 |
||
425 |
!SHA1Stream class methodsFor:'queries'! |
|
426 |
||
427 |
blockSize |
|
428 |
"return the block size used internally by the compression function" |
|
429 |
||
430 |
^ 64 |
|
431 |
||
432 |
"Created: / 18.3.1999 / 08:37:10 / stefan" |
|
433 |
! |
|
434 |
||
435 |
hashSize |
|
436 |
"return the size of the hashvalue returned by instances of this class" |
|
437 |
||
438 |
^ HashSize |
|
439 |
||
440 |
"Modified: / 18.3.1999 / 07:54:22 / stefan" |
|
441 |
! ! |
|
442 |
||
443 |
!SHA1Stream methodsFor:'initialization'! |
|
444 |
||
445 |
initialize |
|
446 |
||
447 |
hashContext := ByteArray new:ContextSize. |
|
448 |
self reset |
|
449 |
||
450 |
"Created: / 17.3.1999 / 16:11:37 / stefan" |
|
451 |
"Modified: / 18.3.1999 / 07:56:46 / stefan" |
|
452 |
! ! |
|
453 |
||
454 |
!SHA1Stream methodsFor:'positioning'! |
|
455 |
||
456 |
reset |
|
457 |
"reset the stream in order to compute a new hash value" |
|
458 |
||
459 |
%{ |
|
460 |
if (__isNonNilObject(__INST(hashContext)) && |
|
461 |
__qClass(__INST(hashContext)) == @global(ByteArray) && |
|
462 |
__byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX) |
|
463 |
) { |
|
13930 | 464 |
SHA1_CTX *ctx = (SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
4051 | 465 |
|
13930 | 466 |
SHA1Init(ctx); |
467 |
RETURN(self); |
|
4051 | 468 |
} |
469 |
%}. |
|
470 |
^ self primitiveFailed |
|
471 |
||
13930 | 472 |
"Created: / 18-03-1999 / 07:59:02 / stefan" |
473 |
"Modified: / 12-01-2012 / 12:21:23 / cg" |
|
4051 | 474 |
! ! |
475 |
||
476 |
!SHA1Stream methodsFor:'queries'! |
|
477 |
||
478 |
hashValue |
|
479 |
"Get the value hashed so far. |
|
480 |
The context is kept, so that more objects may be hashed after |
|
481 |
retrieving a hash value" |
|
482 |
||
483 |
||
484 |
|digest| |
|
485 |
||
486 |
digest := ByteArray new:HashSize. |
|
487 |
||
488 |
%{ |
|
489 |
if (__isNonNilObject(__INST(hashContext)) && |
|
5056 | 490 |
__qClass(__INST(hashContext)) == @global(ByteArray) && |
491 |
__byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX) && |
|
492 |
__isNonNilObject(digest) && |
|
493 |
__qClass(digest) == @global(ByteArray) && |
|
494 |
__byteArraySize(digest) == 20 |
|
4051 | 495 |
) { |
9369
b7f43a51c4ed
Moved SHA1Stream from exept:libcrypt to stx:libbasic
Stefan Vogel <sv@exept.de>
parents:
7824
diff
changeset
|
496 |
SHA1_CTX *ctx = |
5056 | 497 |
(SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
498 |
SHA1_CTX copyContext; |
|
4051 | 499 |
|
5056 | 500 |
memcpy(©Context, ctx, sizeof(copyContext)); |
501 |
SHA1Final(__ByteArrayInstPtr(digest)->ba_element, ©Context); |
|
502 |
RETURN(digest); |
|
4051 | 503 |
} |
504 |
%}. |
|
505 |
||
506 |
^ self primitiveFailed |
|
507 |
||
508 |
"Created: / 17.3.1999 / 16:13:12 / stefan" |
|
509 |
"Modified: / 18.3.1999 / 08:00:54 / stefan" |
|
510 |
! ! |
|
511 |
||
512 |
!SHA1Stream methodsFor:'writing'! |
|
513 |
||
7042 | 514 |
nextPutBytes:count from:anObject startingAt:start |
515 |
"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
|
516 |
The object must have non-pointer indexed instvars |
7042 | 517 |
(i.e. be a ByteArray, String, Float- or DoubleArray), |
518 |
or an externalBytes object (with known size)" |
|
519 |
||
520 |
%{ |
|
521 |
int len, offs; |
|
522 |
int objSize, nInstVars, nInstBytes; |
|
523 |
char *extPtr; |
|
524 |
OBJ oClass; |
|
525 |
||
526 |
if (__isNonNilObject(__INST(hashContext)) |
|
527 |
&&__qClass(__INST(hashContext)) == @global(ByteArray) |
|
528 |
&& __byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX) |
|
529 |
&& __bothSmallInteger(count, start) |
|
530 |
) { |
|
12265 | 531 |
SHA1_CTX *ctx = |
532 |
(SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
|
7042 | 533 |
|
12265 | 534 |
len = __intVal(count); |
535 |
offs = __intVal(start) - 1; |
|
7042 | 536 |
|
12265 | 537 |
oClass = __Class(anObject); |
538 |
if (oClass == ExternalBytes) { |
|
539 |
OBJ sz; |
|
7042 | 540 |
|
12265 | 541 |
nInstBytes = 0; |
542 |
extPtr = (char *)__externalBytesAddress(anObject); |
|
543 |
sz = __externalBytesSize(anObject); |
|
544 |
if (__isSmallInteger(sz)) { |
|
545 |
objSize = __intVal(sz); |
|
546 |
} else { |
|
547 |
objSize = 0; /* unknown */ |
|
548 |
} |
|
549 |
} else { |
|
550 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
551 |
case BYTEARRAY: |
|
552 |
case WORDARRAY: |
|
553 |
case LONGARRAY: |
|
554 |
case SWORDARRAY: |
|
555 |
case SLONGARRAY: |
|
556 |
case FLOATARRAY: |
|
557 |
case DOUBLEARRAY: |
|
558 |
break; |
|
559 |
default: |
|
560 |
goto bad; |
|
561 |
} |
|
562 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
563 |
nInstBytes = __OBJS2BYTES__(nInstVars); |
|
564 |
// nInstBytes is the number of bytes occupied by pointer instance variables |
|
565 |
// subtract from size and add to byte-pointer |
|
566 |
objSize = __Size(anObject) - OHDR_SIZE - nInstBytes; |
|
567 |
extPtr = (char *)__byteArrayVal(anObject)+nInstBytes; |
|
568 |
} |
|
569 |
if ((offs >= 0) && (len >= 0) && (objSize >= (len + offs))) { |
|
570 |
SHA1Update(ctx, extPtr+offs, len); |
|
571 |
RETURN (count); |
|
572 |
} |
|
7042 | 573 |
} |
574 |
bad: ; |
|
575 |
%}. |
|
576 |
||
577 |
^ self primitiveFailed |
|
4051 | 578 |
! ! |
579 |
||
580 |
!SHA1Stream class methodsFor:'documentation'! |
|
581 |
||
582 |
version |
|
13934 | 583 |
^ '$Header: /cvs/stx/stx/libbasic/SHA1Stream.st,v 1.27 2012-01-12 16:36:53 cg Exp $' |
12205 | 584 |
! |
585 |
||
586 |
version_CVS |
|
13934 | 587 |
^ '$Header: /cvs/stx/stx/libbasic/SHA1Stream.st,v 1.27 2012-01-12 16:36:53 cg Exp $' |
4051 | 588 |
! ! |
7042 | 589 |
|
4051 | 590 |
SHA1Stream initialize! |