/
Cuis-System-Hashing.pck.st
1441 lines (1219 loc) · 46 KB
/
Cuis-System-Hashing.pck.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'From Cuis 4.1 of 12 December 2012 [latest update: #1561] on 18 January 2013 at 8:58:31 am'!
'Description Please enter a description for this package '!
!classDefinition: #HMAC category: #'Cuis-System-Hashing'!
Object subclass: #HMAC
instanceVariableNames: 'hash key ipad epad'
classVariableNames: ''
poolDictionaries: ''
category: 'Cuis-System-Hashing'!
!classDefinition: 'HMAC class' category: #'Cuis-System-Hashing'!
HMAC class
instanceVariableNames: ''!
!classDefinition: #HashFunction category: #'Cuis-System-Hashing'!
Object subclass: #HashFunction
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Cuis-System-Hashing'!
!classDefinition: 'HashFunction class' category: #'Cuis-System-Hashing'!
HashFunction class
instanceVariableNames: ''!
!classDefinition: #MD5 category: #'Cuis-System-Hashing'!
HashFunction subclass: #MD5
instanceVariableNames: 'state'
classVariableNames: ''
poolDictionaries: ''
category: 'Cuis-System-Hashing'!
!classDefinition: 'MD5 class' category: #'Cuis-System-Hashing'!
MD5 class
instanceVariableNames: ''!
!classDefinition: #MD5NonPrimitive category: #'Cuis-System-Hashing'!
MD5 subclass: #MD5NonPrimitive
instanceVariableNames: ''
classVariableNames: 'ABCDTable IndexTable ShiftTable SinTable'
poolDictionaries: ''
category: 'Cuis-System-Hashing'!
!classDefinition: 'MD5NonPrimitive class' category: #'Cuis-System-Hashing'!
MD5NonPrimitive class
instanceVariableNames: ''!
!classDefinition: #SHA1 category: #'Cuis-System-Hashing'!
HashFunction subclass: #SHA1
instanceVariableNames: 'totalA totalB totalC totalD totalE totals'
classVariableNames: 'K1 K2 K3 K4'
poolDictionaries: ''
category: 'Cuis-System-Hashing'!
!classDefinition: 'SHA1 class' category: #'Cuis-System-Hashing'!
SHA1 class
instanceVariableNames: ''!
!classDefinition: #SecureHashAlgorithm category: #'Cuis-System-Hashing'!
Object subclass: #SecureHashAlgorithm
instanceVariableNames: 'totalA totalB totalC totalD totalE totals'
classVariableNames: 'K1 K2 K3 K4'
poolDictionaries: ''
category: 'Cuis-System-Hashing'!
!classDefinition: 'SecureHashAlgorithm class' category: #'Cuis-System-Hashing'!
SecureHashAlgorithm class
instanceVariableNames: ''!
!classDefinition: #SecureHashAlgorithmTest category: #'Cuis-System-Hashing'!
TestCase subclass: #SecureHashAlgorithmTest
instanceVariableNames: 'hash'
classVariableNames: ''
poolDictionaries: ''
category: 'Cuis-System-Hashing'!
!classDefinition: 'SecureHashAlgorithmTest class' category: #'Cuis-System-Hashing'!
SecureHashAlgorithmTest class
instanceVariableNames: ''!
!classDefinition: #ThirtyTwoBitRegister category: #'Cuis-System-Hashing'!
Object subclass: #ThirtyTwoBitRegister
instanceVariableNames: 'hi low'
classVariableNames: ''
poolDictionaries: ''
category: 'Cuis-System-Hashing'!
!classDefinition: 'ThirtyTwoBitRegister class' category: #'Cuis-System-Hashing'!
ThirtyTwoBitRegister class
instanceVariableNames: ''!
!HMAC commentStamp: '<historical>' prior: 0!
HMAC is a mechanism for message authentication using cryptographic hash functions. HMAC can be used with any iterative cryptographic hash function, e.g., MD5, SHA-1, in combination with a secret shared key. The cryptographic strength of HMAC depends on the properties of the underlying hash function.
See RFC 2114.!
!MD5 commentStamp: 'ul 3/3/2008 23:40' prior: 0!
This class implements the MD5 128-bit one-way hash function. It uses the MD5Plugin for better performance. Some methods are taken from the original version of MD5NonPrimitive.!
!MD5NonPrimitive commentStamp: '<historical>' prior: 0!
This class implements the MD5 128-bit one-way hash function. It relies on
the ThirtyTwoBitRegister class supplied as part of the "Digital Signatures"
functionality included in Squeak 2.7. As of this date (1/20/2000), the
U.S. Government has lifted many of the previous restrictions on the export
of encryption software, but you should check before exporting anything
including this code. MD5 is commonly used for some secure Internet
protocols, including authentication in HTTP, which is why I wrote it.
Submitted by Duane Maxwell
!
!SHA1 commentStamp: '<historical>' prior: 0!
This class implements the Secure Hash Algorithm (SHA) described in the U.S. government's Secure Hash Standard (SHS). This standard is described in FIPS PUB 180-1, "SECURE HASH STANDARD", April 17, 1995.
The Secure Hash Algorithm is also described on p. 442 of 'Applied Cryptography: Protocols, Algorithms, and Source Code in C' by Bruce Scheier, Wiley, 1996.
See the comment in class DigitalSignatureAlgorithm for details on its use.
Implementation notes:
The secure hash standard was created with 32-bit hardware in mind. All arithmetic in the hash computation must be done modulo 2^32. This implementation uses ThirtyTwoBitRegister objects to simulate hardware registers; this implementation is about six times faster than using LargePositiveIntegers (measured on a Macintosh G3 Powerbook). Implementing a primitive to process each 64-byte buffer would probably speed up the computation by a factor of 20 or more.
!
!SecureHashAlgorithm commentStamp: '<historical>' prior: 0!
This class implements the Secure Hash Algorithm (SHA) described in the U.S. government's Secure Hash Standard (SHS). This standard is described in FIPS PUB 180-1, "SECURE HASH STANDARD", April 17, 1995.
The Secure Hash Algorithm is also described on p. 442 of 'Applied Cryptography: Protocols, Algorithms, and Source Code in C' by Bruce Scheier, Wiley, 1996.
See the comment in class DigitalSignatureAlgorithm for details on its use.
Implementation notes:
The secure hash standard was created with 32-bit hardware in mind. All arithmetic in the hash computation must be done modulo 2^32. This implementation uses ThirtyTwoBitRegister objects to simulate hardware registers; this implementation is about six times faster than using LargePositiveIntegers (measured on a Macintosh G3 Powerbook). Implementing a primitive to process each 64-byte buffer would probably speed up the computation by a factor of 20 or more.
!
!SecureHashAlgorithmTest commentStamp: '<historical>' prior: 0!
This is the unit test for the class SecureHashAlgorithm. Unit tests are a good way to exercise the functionality of your system in a repeatable and automatic manner. They are therefore recommended if you plan to release anything. For more information, see:
- http://www.c2.com/cgi/wiki?UnitTest
- there is a chapter in the PharoByExample book (http://pharobyexample.org)
- the sunit class category!
!ThirtyTwoBitRegister commentStamp: '<historical>' prior: 0!
I represent a 32-bit register. An instance of me can hold any non-negative integer in the range [0..(2^32 - 1)]. Operations are performed on my contents in place, like a hardware register, and results are always modulo 2^32.
This class is primarily meant for use by the SecureHashAlgorithm class.
!
!ByteArray methodsFor: '*Cuis-System-Hashing' stamp: 'gsa 1/14/2013 15:07'!
asByteArrayOfSize: size
"
'34523' asByteArray asByteArrayOfSize: 100.
(((
| repeats bytes |
repeats := 1000000.
bytes := '123456789123456789123456789123456789123456789123456789' asByteArray.
[repeats timesRepeat: (bytes asByteArrayOfSize: 1024) ] timeToRun.
)))"
| bytes |
size < self size
ifTrue: [^ self error: 'bytearray bigger than ', size asString].
bytes := self asByteArray.
^ (ByteArray new: (size - bytes size)), bytes
! !
!HMAC methodsFor: 'accessing' stamp: 'cmm 12/2/2006 14:57'!
destroy
key destroy! !
!HMAC methodsFor: 'accessing' stamp: 'len 10/16/2002 16:43'!
digestMessage: aByteArray
^ hash hashMessage: (key bitXor: epad), (hash hashMessage: (key bitXor: ipad), aByteArray)! !
!HMAC methodsFor: 'accessing' stamp: 'len 8/3/2002 02:06'!
digestSize
^ hash hashSize! !
!HMAC methodsFor: 'accessing' stamp: 'StephaneDucasse 10/17/2009 17:15'!
key: aByteArray
key := aByteArray.
key size > hash blockSize ifTrue: [ key := hash hashMessage: key ].
key size < hash blockSize ifTrue: [ key := key , (ByteArray new: hash blockSize - key size) ]! !
!HMAC methodsFor: 'printing' stamp: 'len 8/3/2002 02:08'!
printOn: aStream
aStream nextPutAll: 'HMAC-'; print: hash! !
!HMAC methodsFor: 'initialization' stamp: 'StephaneDucasse 10/17/2009 17:15'!
setHash: aHash
hash := aHash.
ipad := ByteArray
new: aHash blockSize
withAll: 54.
epad := ByteArray
new: aHash blockSize
withAll: 92! !
!HMAC class methodsFor: 'instance creation' stamp: 'len 8/15/2002 01:42'!
on: aHashFunction
^ self new setHash: aHashFunction! !
!HashFunction methodsFor: 'accessing' stamp: 'len 8/15/2002 01:43'!
blockSize
^ self class blockSize! !
!HashFunction methodsFor: 'accessing' stamp: 'cmm 2/20/2006 23:22'!
doubleHashMessage: aStringOrByteArray
"SHA1 new doubleHashMessage: 'foo'"
^ self doubleHashStream: aStringOrByteArray asByteArray readStream! !
!HashFunction methodsFor: 'accessing' stamp: 'cmm 2/20/2006 23:21'!
doubleHashStream: aStream
^ self hashStream: ((self hashStream: aStream) asByteArray readStream)! !
!HashFunction methodsFor: 'accessing' stamp: 'len 8/7/2002 16:30'!
hashMessage: aStringOrByteArray
"MD5 new hashMessage: 'foo'"
^ self hashStream: aStringOrByteArray asByteArray readStream! !
!HashFunction methodsFor: 'accessing' stamp: 'len 8/9/2002 13:17'!
hashSize
^ self class hashSize! !
!HashFunction methodsFor: 'accessing' stamp: 'len 8/2/2002 02:21'!
hashStream: aStream
^ self subclassResponsibility! !
!HashFunction methodsFor: 'converting' stamp: 'len 8/3/2002 02:42'!
hmac
^ HMAC on: self! !
!HashFunction class methodsFor: 'accessing' stamp: 'len 8/15/2002 01:43'!
blockSize
^ self subclassResponsibility! !
!HashFunction class methodsFor: 'hashing' stamp: 'len 8/2/2002 02:20'!
hashMessage: aStringOrByteArray
^ self new hashMessage: aStringOrByteArray! !
!HashFunction class methodsFor: 'accessing' stamp: 'len 8/9/2002 13:17'!
hashSize
^ self subclassResponsibility! !
!HashFunction class methodsFor: 'hashing' stamp: 'len 8/2/2002 02:20'!
hashStream: aPositionableStream
^ self new hashStream: aPositionableStream! !
!Integer methodsFor: '*Cuis-System-Hashing' stamp: 'gsa 1/14/2013 15:08'!
asByteArrayOfSize: aSize
"Answer a ByteArray of aSize with my value, most-significant byte first."
| answer digitPos |
aSize < self digitLength ifTrue: [ self error: 'number to large for byte array' ].
answer := ByteArray new: aSize.
digitPos := 1.
aSize
to: aSize - self digitLength + 1
by: -1
do:
[ :pos |
answer
at: pos
put: (self digitAt: digitPos).
digitPos := digitPos + 1 ].
^ answer! !
!MD5 methodsFor: 'private-buffers' stamp: 'ul 3/3/2008 21:34'!
finalValue
^state! !
!MD5 methodsFor: 'accessing' stamp: 'ul 3/3/2008 22:00'!
hashStream: aPositionableStream
| startPosition buf bitLength |
self initialize.
aPositionableStream atEnd ifTrue: [
buf := ByteArray new: 64.
buf at: 1 put: 128.
self processBuffer: buf.
^self finalValue ].
startPosition := aPositionableStream position.
[aPositionableStream atEnd] whileFalse: [
buf := aPositionableStream next: 64.
(aPositionableStream atEnd not and: [buf size = 64])
ifTrue: [self processBuffer: buf]
ifFalse: [
bitLength := (aPositionableStream position - startPosition) * 8.
self processFinalBuffer: buf bitLength: bitLength]].
^ self finalValue! !
!MD5 methodsFor: 'initialization' stamp: 'StephaneDucasse 2/3/2010 22:14'!
initialize
"Some magic numbers to get the process started"
state := #[1 35 69 103 137 171 205 239 254 220 186 152 118 84 50 16]
! !
!MD5 methodsFor: 'private-buffers' stamp: 'ul 3/3/2008 22:28'!
primProcessBuffer: aByteArray withState: s
<primitive: 'primitiveProcessBufferWithState' module: 'MD5Plugin'>
self primitiveFailed! !
!MD5 methodsFor: 'private-buffers' stamp: 'ul 3/3/2008 19:17'!
processBuffer: aByteArray
self primProcessBuffer: aByteArray withState: state.
! !
!MD5 methodsFor: 'private-buffers' stamp: 'StephaneDucasse 10/17/2009 17:15'!
processFinalBuffer: aByteArray bitLength: bitLength
"Pad the buffer until we have an even 64 bytes, then transform"
| out |
out := ByteArray new: 64.
out
replaceFrom: 1
to: aByteArray size
with: aByteArray
startingAt: 1.
aByteArray size < 56 ifTrue:
[ out
at: aByteArray size + 1
put: 128. "trailing bit"
self
storeLength: bitLength
in: out.
self processBuffer: out.
^ self ].
"not enough room for the length, so just pad this one, then..."
aByteArray size < 64 ifTrue:
[ out
at: aByteArray size + 1
put: 128 ].
self processBuffer: out.
"process one additional block of padding ending with the length"
out := ByteArray new: 64. "filled with zeros"
aByteArray size = 64 ifTrue:
[ out
at: 1
put: 128 ].
self
storeLength: bitLength
in: out.
self processBuffer: out! !
!MD5 methodsFor: 'private-buffers' stamp: 'StephaneDucasse 10/17/2009 17:15'!
storeLength: bitLength in: aByteArray
"Fill in the final 8 bytes of the given ByteArray with a 64-bit
little-endian representation of the original message length in bits."
| n i |
n := bitLength.
i := aByteArray size - 8 + 1.
[ n > 0 ] whileTrue:
[ aByteArray
at: i
put: (n bitAnd: 255).
n := n bitShift: -8.
i := i + 1 ]! !
!MD5 class methodsFor: 'accessing' stamp: 'ul 3/3/2008 23:37'!
blockSize
^ 64! !
!MD5 class methodsFor: 'accessing' stamp: 'ul 3/3/2008 23:37'!
hashSize
^ 16! !
!MD5 class methodsFor: 'as yet unclassified' stamp: 'ul 3/3/2008 22:46'!
isPluginAvailable
<primitive: 'primitivePluginAvailable' module: 'MD5Plugin'>
^false! !
!MD5 class methodsFor: 'as yet unclassified' stamp: 'ul 3/3/2008 23:01'!
new
self isPluginAvailable
ifTrue: [ ^self basicNew ]
ifFalse: [ ^MD5NonPrimitive basicNew ]! !
!MD5NonPrimitive methodsFor: 'private-functions' stamp: 'DSM 1/20/2000 01:47'!
fX: x Y: y Z: z
" compute 'xy or (not x)z'"
^ x copy bitAnd: y; bitOr: (x copy bitInvert; bitAnd: z)
! !
!MD5NonPrimitive methodsFor: 'private-functions' stamp: 'DSM 1/20/2000 13:38'!
ffA: a B: b C: c D: d M: m S: s T: t
"compute a = b + ((a + f(b,c,d) + m + t) <<< s)"
^ a += (self fX: b Y: c Z: d); += m; += t; leftRotateBy: s; += b.
! !
!MD5NonPrimitive methodsFor: 'private-buffers' stamp: 'len 10/15/2002 19:58'!
finalValue
"Concatenate the state values to produce the 128-bite result"
^ (state at: 1) asByteArray, (state at: 2) asByteArray, (state at: 3) asByteArray, (state at: 4) asByteArray! !
!MD5NonPrimitive methodsFor: 'private-functions' stamp: 'DSM 1/20/2000 01:48'!
gX: x Y: y Z: z
" compute 'xz or y(not z)'"
^ x copy bitAnd: z; bitOr: (z copy bitInvert; bitAnd: y)
! !
!MD5NonPrimitive methodsFor: 'private-functions' stamp: 'DSM 1/20/2000 13:38'!
ggA: a B: b C: c D: d M: m S: s T: t
"compute a = b + ((a + g(b,c,d) + m + t) <<< s)"
^ a += (self gX: b Y: c Z: d); += m; += t; leftRotateBy: s; += b.
! !
!MD5NonPrimitive methodsFor: 'private-functions' stamp: 'DSM 1/20/2000 01:48'!
hX: x Y: y Z: z
" compute 'x xor y xor z'"
^ x copy bitXor: y; bitXor: z
! !
!MD5NonPrimitive methodsFor: 'private-functions' stamp: 'DSM 1/20/2000 13:38'!
hhA: a B: b C: c D: d M: m S: s T: t
"compute a = b + ((a + h(b,c,d) + m + t) <<< s)"
^ a += (self hX: b Y: c Z: d); += m; += t; leftRotateBy: s; += b.
! !
!MD5NonPrimitive methodsFor: 'private-functions' stamp: 'DSM 1/20/2000 01:48'!
iX: x Y: y Z: z
" compute 'y xor (x or (not z))'"
^ y copy bitXor: (z copy bitInvert; bitOr: x)
! !
!MD5NonPrimitive methodsFor: 'private-functions' stamp: 'DSM 1/20/2000 13:39'!
iiA: a B: b C: c D: d M: m S: s T: t
"compute a = b + ((a + i(b,c,d) + m + t) <<< s)"
^ a += (self iX: b Y: c Z: d); += m; += t; leftRotateBy: s; += b.
! !
!MD5NonPrimitive methodsFor: 'initialization' stamp: 'StephaneDucasse 10/17/2009 17:15'!
initialize
"Some magic numbers to get the process started"
state := OrderedCollection newFrom: {
(ThirtyTwoBitRegister new load: 1732584193).
(ThirtyTwoBitRegister new load: 4023233417).
(ThirtyTwoBitRegister new load: 2562383102).
(ThirtyTwoBitRegister new load: 271733878)
}! !
!MD5NonPrimitive methodsFor: 'private-buffers' stamp: 'StephaneDucasse 10/17/2009 17:15'!
processBuffer: aByteArray
"Process a 64-byte buffer"
| saveState data |
saveState := state collect: [ :item | item copy ].
data := Array new: 16.
1
to: 16
do:
[ :index |
data
at: index
put: (ThirtyTwoBitRegister new
reverseLoadFrom: aByteArray
at: index * 4 - 3) ].
self rounds: data.
1
to: 4
do: [ :index | (state at: index) += (saveState at: index) ]! !
!MD5NonPrimitive methodsFor: 'private-rounds' stamp: 'StephaneDucasse 10/17/2009 17:15'!
round: data selector: selector round: round
"Do one round with the given function"
| shiftIndex template abcd |
1
to: 16
do:
[ :i |
shiftIndex := (i - 1) \\ 4 + 1.
abcd := ABCDTable at: shiftIndex.
template := {
(abcd at: 1).
(abcd at: 2).
(abcd at: 3).
(abcd at: 4).
((IndexTable at: round) at: i).
((ShiftTable at: round) at: shiftIndex).
(SinTable at: (round - 1) * 16 + i)
}.
self
step: data
template: template
selector: selector ]! !
!MD5NonPrimitive methodsFor: 'private-rounds' stamp: 'DSM 1/20/2000 17:58'!
rounds: data
"Perform the four rounds with different functions"
#(
ffA:B:C:D:M:S:T:
ggA:B:C:D:M:S:T:
hhA:B:C:D:M:S:T:
iiA:B:C:D:M:S:T:
) doWithIndex: [ :selector :index |
self round: data selector: selector round: index.]
! !
!MD5NonPrimitive methodsFor: 'private-functions' stamp: 'StephaneDucasse 10/17/2009 17:15'!
step: data template: item selector: selector
"Perform one step in the round"
| args |
args := {
(state at: (item at: 1)).
(state at: (item at: 2)).
(state at: (item at: 3)).
(state at: (item at: 4)).
(data at: (item at: 5)).
(item at: 6).
(item at: 7)
}.
self
perform: selector
withArguments: args! !
!MD5NonPrimitive class methodsFor: 'class initialization' stamp: 'StephaneDucasse 10/17/2009 17:15'!
initialize
"MD5 initialize"
"Obscure fact: those magic hex numbers that are hard to type in correctly are
actually the result of a simple trigonometric function and are therefore
easier to compute than proofread. Laziness is sometimes a virtue."
| c |
c := 2 raisedTo: 32.
SinTable := Array new: 64.
1
to: 64
do:
[ :i |
SinTable
at: i
put: (ThirtyTwoBitRegister new load: (c * i sin abs) truncated) ].
ShiftTable := {
#(7 12 17 22 ).
#(5 9 14 20 ).
#(4 11 16 23 ).
#(6 10 15 21 )
}.
IndexTable := {
#(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
).
#(
2
7
12
1
6
11
16
5
10
15
4
9
14
3
8
13
).
#(
6
9
12
15
2
5
8
11
14
1
4
7
10
13
16
3
).
#(
1
8
15
6
13
4
11
2
9
16
7
14
5
12
3
10
)
}.
ABCDTable := {
#(1 2 3 4 ).
#(4 1 2 3 ).
#(3 4 1 2 ).
#(2 3 4 1 )
}! !
!SHA1 methodsFor: 'private' stamp: 'jm 12/7/1999 23:25'!
constantForStep: i
"Answer the constant for the i-th step of the block hash loop. We number our steps 1-80, versus the 0-79 of the standard."
i <= 20 ifTrue: [^ K1].
i <= 40 ifTrue: [^ K2].
i <= 60 ifTrue: [^ K3].
^ K4
! !
!SHA1 methodsFor: 'private' stamp: 'StephaneDucasse 10/17/2009 17:15'!
expandedBlock: aByteArray
"Convert the given 64 byte buffer into 80 32-bit registers and answer the result."
| out src v |
out := Array new: 80.
src := 1.
1
to: 16
do:
[ :i |
out
at: i
put: (ThirtyTwoBitRegister new
loadFrom: aByteArray
at: src).
src := src + 4 ].
17
to: 80
do:
[ :i |
v := (out at: i - 3) copy.
v
bitXor: (out at: i - 8);
bitXor: (out at: i - 14);
bitXor: (out at: i - 16);
leftRotateBy: 1.
out
at: i
put: v ].
^ out! !
!SHA1 methodsFor: 'private' stamp: 'StephaneDucasse 10/17/2009 17:15'!
finalHash
"Concatenate the final totals to build the 160-bit integer result."
"Details: If the primitives are supported, the results are in the totals array. Otherwise, they are in the instance variables totalA through totalE."
| r |
totals ifNil:
[ "compute final hash when not using primitives"
^ (totalA asInteger bitShift: 128) + (totalB asInteger bitShift: 96) + (totalC asInteger bitShift: 64) + (totalD asInteger bitShift: 32) + totalE asInteger ].
"compute final hash when using primitives"
r := 0.
1
to: 5
do: [ :i | r := r bitOr: ((totals at: i) bitShift: 32 * (5 - i)) ].
^ r! !
!SHA1 methodsFor: 'private' stamp: 'jm 12/7/1999 22:15'!
hashFunction: i of: x with: y with: z
"Compute the hash function for the i-th step of the block hash loop. We number our steps 1-80, versus the 0-79 of the standard."
"Details: There are four functions, one for each 20 iterations. The second and fourth are the same."
i <= 20 ifTrue: [^ x copy bitAnd: y; bitOr: (x copy bitInvert; bitAnd: z)].
i <= 40 ifTrue: [^ x copy bitXor: y; bitXor: z].
i <= 60 ifTrue: [^ x copy bitAnd: y; bitOr: (x copy bitAnd: z); bitOr: (y copy bitAnd: z)].
^ x copy bitXor: y; bitXor: z
! !
!SHA1 methodsFor: 'accessing' stamp: 'StephaneDucasse 10/17/2009 17:15'!
hashInteger: aPositiveInteger
"Hash the given positive integer. The integer to be hashed should have 512 or fewer bits. This entry point is used in key generation."
| buffer dstIndex |
self initializeTotals.
"pad integer with zeros"
aPositiveInteger highBit <= 512 ifFalse: [ self error: 'integer cannot exceed 512 bits' ].
buffer := ByteArray new: 64.
dstIndex := 0.
aPositiveInteger digitLength
to: 1
by: -1
do:
[ :i |
buffer
at: (dstIndex := dstIndex + 1)
put: (aPositiveInteger digitAt: i) ].
"process that one block"
self processBuffer: buffer.
^ self finalHash! !
!SHA1 methodsFor: 'accessing' stamp: 'StephaneDucasse 10/17/2009 17:15'!
hashInteger: aPositiveInteger seed: seedInteger
"Hash the given positive integer. The integer to be hashed should have 512 or fewer bits. This entry point is used in the production of random numbers"
"Initialize totalA through totalE to their seed values."
| buffer dstIndex |
totalA := ThirtyTwoBitRegister new load: ((seedInteger bitShift: -128) bitAnd: 4294967295).
totalB := ThirtyTwoBitRegister new load: ((seedInteger bitShift: -96) bitAnd: 4294967295).
totalC := ThirtyTwoBitRegister new load: ((seedInteger bitShift: -64) bitAnd: 4294967295).
totalD := ThirtyTwoBitRegister new load: ((seedInteger bitShift: -32) bitAnd: 4294967295).
totalE := ThirtyTwoBitRegister new load: (seedInteger bitAnd: 4294967295).
self initializeTotalsArray.
"pad integer with zeros"
buffer := ByteArray new: 64.
dstIndex := 0.
aPositiveInteger digitLength
to: 1
by: -1
do:
[ :i |
buffer
at: (dstIndex := dstIndex + 1)
put: (aPositiveInteger digitAt: i) ].
"process that one block"
self processBuffer: buffer.
^ self finalHash! !
!SHA1 methodsFor: 'accessing' stamp: 'StephaneDucasse 10/17/2009 17:15'!
hashStream: aPositionableStream
"Hash the contents of the given stream from the current position to the end using the Secure Hash Algorithm. The SHA algorithm is defined in FIPS PUB 180-1. It is also described on p. 442 of 'Applied Cryptography: Protocols, Algorithms, and Source Code in C' by Bruce Scheier, Wiley, 1996."
"SecureHashAlgorithm new hashStream: (ReadStream on: 'foo')"
| startPosition buf bitLength |
self initializeTotals.
aPositionableStream atEnd ifTrue: [ self error: 'empty stream' ].
startPosition := aPositionableStream position.
[ aPositionableStream atEnd ] whileFalse:
[ buf := aPositionableStream next: 64.
(aPositionableStream atEnd not and: [ buf size = 64 ])
ifTrue: [ self processBuffer: buf ]
ifFalse:
[ bitLength := (aPositionableStream position - startPosition) * 8.
self
processFinalBuffer: buf
bitLength: bitLength ] ].
^ self finalHash asByteArrayOfSize: 20! !
!SHA1 methodsFor: 'private' stamp: 'StephaneDucasse 10/17/2009 17:15'!
initializeTotals
"Initialize totalA through totalE to their seed values."
"total registers for use when primitives are absent"
totalA := ThirtyTwoBitRegister new load: 1732584193.
totalB := ThirtyTwoBitRegister new load: 4023233417.
totalC := ThirtyTwoBitRegister new load: 2562383102.
totalD := ThirtyTwoBitRegister new load: 271733878.
totalE := ThirtyTwoBitRegister new load: 3285377520.
self initializeTotalsArray! !
!SHA1 methodsFor: 'private' stamp: 'StephaneDucasse 10/17/2009 17:15'!
initializeTotalsArray
"Initialize the totals array from the registers for use with the primitives."
totals := Bitmap new: 5.
totals
at: 1
put: totalA asInteger.
totals
at: 2
put: totalB asInteger.
totals
at: 3
put: totalC asInteger.
totals
at: 4
put: totalD asInteger.
totals
at: 5
put: totalE asInteger! !
!SHA1 methodsFor: 'primitives' stamp: 'jm 12/21/1999 20:11'!
primExpandBlock: aByteArray into: wordBitmap
"Expand the given 64-byte buffer into the given Bitmap of length 80."
<primitive: 'primitiveExpandBlock' module: 'DSAPrims'>
^ self primitiveFailed
! !
!SHA1 methodsFor: 'primitives' stamp: 'jm 12/21/1999 22:58'!
primHasSecureHashPrimitive
"Answer true if this platform has primitive support for the Secure Hash Algorithm."
<primitive: 'primitiveHasSecureHashPrimitive' module: 'DSAPrims'>
^ false
! !
!SHA1 methodsFor: 'primitives' stamp: 'jm 12/21/1999 20:13'!
primHashBlock: blockBitmap using: workingTotalsBitmap
"Hash the given block (a Bitmap) of 80 32-bit words, using the given workingTotals."
<primitive: 'primitiveHashBlock' module: 'DSAPrims'>
^ self primitiveFailed
! !
!SHA1 methodsFor: 'private' stamp: 'StephaneDucasse 10/17/2009 17:15'!
processBuffer: aByteArray
"Process given 64-byte buffer, accumulating the results in totalA through totalE."
| a b c d e w tmp |
self primHasSecureHashPrimitive
ifTrue: [ ^ self processBufferUsingPrimitives: aByteArray ]
ifFalse: [ totals := nil ].
"initialize registers a through e from the current totals"
a := totalA copy.
b := totalB copy.
c := totalC copy.
d := totalD copy.
e := totalE copy.
"expand and process the buffer"
w := self expandedBlock: aByteArray.
1
to: 80
do:
[ :i |
tmp := (a copy leftRotateBy: 5)
+= (self
hashFunction: i
of: b
with: c
with: d);
+= e;
+= (w at: i);
+= (self constantForStep: i).
e := d.
d := c.
c := b copy leftRotateBy: 30.
b := a.
a := tmp ].
"add a through e into total accumulators"
totalA += a.
totalB += b.
totalC += c.
totalD += d.
totalE += e! !
!SHA1 methodsFor: 'private' stamp: 'StephaneDucasse 10/17/2009 17:15'!
processBufferUsingPrimitives: aByteArray
"Process given 64-byte buffer using the primitives, accumulating the results in totals."
"expand and process the buffer"
| w |
w := Bitmap new: 80.
self
primExpandBlock: aByteArray
into: w.
self
primHashBlock: w
using: totals! !
!SHA1 methodsFor: 'private' stamp: 'StephaneDucasse 10/17/2009 17:15'!
processFinalBuffer: buffer bitLength: bitLength
"Process given buffer, whose length may be <= 64 bytes, accumulating the results in totalA through totalE. Also process the final padding bits and length."
| out |
out := ByteArray new: 64.
out
replaceFrom: 1
to: buffer size
with: buffer
startingAt: 1.
buffer size < 56 ifTrue:
[ "padding and length fit in last data block"
out
at: buffer size + 1
put: 128. "trailing one bit"
self
storeLength: bitLength
in: out. "end with length"
self processBuffer: out.
^ self ].
"process the final data block"
buffer size < 64 ifTrue:
[ out
at: buffer size + 1
put: 128 ]. "trailing one bit"
self processBuffer: out.
"process one additional block of padding ending with the length"
out := ByteArray new: 64. "filled with zeros"
buffer size = 64 ifTrue:
[ "add trailing one bit that didn't fit in final data block"
out
at: 1
put: 128 ].
self
storeLength: bitLength
in: out.
self processBuffer: out! !
!SHA1 methodsFor: 'private' stamp: 'StephaneDucasse 10/17/2009 17:15'!
storeLength: bitLength in: aByteArray
"Fill in the final 8 bytes of the given ByteArray with a 64-bit big-endian representation of the original message length in bits."
| n i |
n := bitLength.
i := aByteArray size.
[ n > 0 ] whileTrue:
[ aByteArray
at: i
put: (n bitAnd: 255).
n := n bitShift: -8.
i := i - 1 ]! !
!SHA1 class methodsFor: 'accessing' stamp: 'rww 9/22/2006 19:25'!
blockSize
^ 64! !
!SHA1 class methodsFor: 'accessing' stamp: 'len 8/15/2002 01:45'!
hashSize
^ 20! !
!SHA1 class methodsFor: 'class initialization' stamp: 'StephaneDucasse 10/17/2009 17:15'!
initialize
"SecureHashAlgorithm initialize"
"For the curious, here's where these constants come from:
#(2 3 5 10) collect: [:x | ((x sqrt / 4.0) * (2.0 raisedTo: 32)) truncated hex]"
K1 := ThirtyTwoBitRegister new load: 1518500249.
K2 := ThirtyTwoBitRegister new load: 1859775393.
K3 := ThirtyTwoBitRegister new load: 2400959708.
K4 := ThirtyTwoBitRegister new load: 3395469782! !
!SecureHashAlgorithm methodsFor: 'private' stamp: 'jm 12/7/1999 23:25'!
constantForStep: i
"Answer the constant for the i-th step of the block hash loop. We number our steps 1-80, versus the 0-79 of the standard."
i <= 20 ifTrue: [^ K1].
i <= 40 ifTrue: [^ K2].
i <= 60 ifTrue: [^ K3].
^ K4
! !
!SecureHashAlgorithm methodsFor: 'private' stamp: 'jm 12/21/1999 20:06'!
expandedBlock: aByteArray
"Convert the given 64 byte buffer into 80 32-bit registers and answer the result."
| out src v |
out := Array new: 80.
src := 1.
1 to: 16 do: [:i |
out at: i put: (ThirtyTwoBitRegister new loadFrom: aByteArray at: src).
src := src + 4].
17 to: 80 do: [:i |
v := (out at: i - 3) copy.
v bitXor: (out at: i - 8);
bitXor: (out at: i - 14);
bitXor: (out at: i - 16);
leftRotateBy: 1.
out at: i put: v].
^ out
! !
!SecureHashAlgorithm methodsFor: 'private' stamp: 'jm 12/21/1999 20:02'!
finalHash
"Concatenate the final totals to build the 160-bit integer result."
"Details: If the primitives are supported, the results are in the totals array. Otherwise, they are in the instance variables totalA through totalE."
| r |
totals ifNil: [ "compute final hash when not using primitives"
^ (totalA asInteger bitShift: 128) +
(totalB asInteger bitShift: 96) +
(totalC asInteger bitShift: 64) +
(totalD asInteger bitShift: 32) +
(totalE asInteger)].
"compute final hash when using primitives"
r := 0.
1 to: 5 do: [:i |
r := r bitOr: ((totals at: i) bitShift: (32 * (5 - i)))].
^ r
! !
!SecureHashAlgorithm methodsFor: 'private' stamp: 'jm 12/7/1999 22:15'!
hashFunction: i of: x with: y with: z
"Compute the hash function for the i-th step of the block hash loop. We number our steps 1-80, versus the 0-79 of the standard."
"Details: There are four functions, one for each 20 iterations. The second and fourth are the same."
i <= 20 ifTrue: [^ x copy bitAnd: y; bitOr: (x copy bitInvert; bitAnd: z)].
i <= 40 ifTrue: [^ x copy bitXor: y; bitXor: z].
i <= 60 ifTrue: [^ x copy bitAnd: y; bitOr: (x copy bitAnd: z); bitOr: (y copy bitAnd: z)].
^ x copy bitXor: y; bitXor: z
! !