Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
gjvanderheiden committed May 17, 2023
1 parent 71ad8e6 commit 6a118d1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
43 changes: 16 additions & 27 deletions src/System-Hashing-SHA512/SHA512.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ Class {
'totalE',
'totalF',
'totalG',
'totalH',
'totals'
'totalH'
],
#category : #'System-Hashing-SHA512-SHA512'
}
Expand Down Expand Up @@ -155,14 +154,14 @@ SHA512 >> finalHash [
"Concatenate the final totals to build the 512-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."

^ (totalA asInteger bitShift: 448) +
(totalB asInteger bitShift: 384) +
(totalC asInteger bitShift: 320) +
(totalD asInteger bitShift: 256) +
(totalE asInteger bitShift: 192) +
(totalF asInteger bitShift: 128) +
(totalG asInteger bitShift: 64) +
(totalH asInteger).
^ (totalA copy asInteger bitShift: 448) +
(totalB copy asInteger bitShift: 384) +
(totalC copy asInteger bitShift: 320) +
(totalD copy asInteger bitShift: 256) +
(totalE copy asInteger bitShift: 192) +
(totalF copy asInteger bitShift: 128) +
(totalG copy asInteger bitShift: 64) +
(totalH copy asInteger).
]

{ #category : #accessing }
Expand All @@ -171,7 +170,7 @@ SHA512 >> hashStream: aPositionableStream [
"SHA512 new hashStream: (ReadStream on: 'foo')"

| startPosition buf bitLength |
self initializeTotals.


aPositionableStream atEnd ifTrue: [ self processFinalBuffer: #() bitLength: 0 ].

Expand All @@ -187,6 +186,12 @@ SHA512 >> hashStream: aPositionableStream [
^self finalHash asByteArrayOfSize: 64
]

{ #category : #private }
SHA512 >> initialize [

self initializeTotals.
]

{ #category : #private }
SHA512 >> initializeTotals [
"Initialize totalA through totalE to their seed values."
Expand All @@ -200,30 +205,14 @@ SHA512 >> initializeTotals [
totalF := SixtyFourBitRegister new loadFrom: self class h5.
totalG := SixtyFourBitRegister new loadFrom: self class h6.
totalH := SixtyFourBitRegister new loadFrom: self class h7.
self initializeTotalsArray.
]

{ #category : #private }
SHA512 >> initializeTotalsArray [
"Initialize the totals array from the registers for use with the primitives."

totals := DoubleWordArray new: 8.
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.
totals at: 6 put: totalF asInteger.
totals at: 7 put: totalG asInteger.
totals at: 8 put: totalH asInteger.
]

{ #category : #private }
SHA512 >> processBuffer: aByteArray [
"Process given 64-byte buffer, accumulating the results in totalA through totalE."

| a b c d e f g h w |
totals := nil.

"initialize registers a through e from the current totals"
a := totalA copy.
Expand Down
15 changes: 15 additions & 0 deletions src/System-Hashing-SHA512/SHA512Test.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,18 @@ SHA512Test >> testHashStream [
'8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909'.
self assert: result equals: expected
]

{ #category : #test }
SHA512Test >> testHashStreamTwice [

| result expected |
result := SHA512 new hashStream: (ReadStream on: 'aa' utf8Encoded).

expected := 'f6c5600ed1dbdcfdf829081f5417dccbbd2b9288e0b427e65c8cf67e274b69009cd142475e15304f599f429f260a661b5df4de26746459a3cef7f32006e5d1c1'.

self assert: result hex equals: expected.




]

0 comments on commit 6a118d1

Please sign in to comment.