diff options
author | siddharth <s@ricketyspace.net> | 2021-10-10 11:18:25 -0400 |
---|---|---|
committer | siddharth <s@ricketyspace.net> | 2021-10-10 11:18:25 -0400 |
commit | 244d10ac25d1adc0f10e089f8d457269b109897d (patch) | |
tree | e4a94f6967a973f8cd2e1edc258a7958be05e185 | |
parent | f9c878cbad59a98490e832703f19abf49bd6738f (diff) |
lib: move sha arithmetic functions to hash.go
-rw-r--r-- | lib/hash.go | 19 | ||||
-rw-r--r-- | lib/sha1.go | 35 |
2 files changed, 27 insertions, 27 deletions
diff --git a/lib/hash.go b/lib/hash.go index f0f4625..363d3d6 100644 --- a/lib/hash.go +++ b/lib/hash.go @@ -28,6 +28,25 @@ func shaMessageBlocks(pm []byte) [][]uint32 { return mbs } +// (a + b + ...) mod 2^32 +func shaAdd(n ...uint32) uint32 { + sum := uint64(0) + for _, v := range n { + sum += uint64(v) + } + return uint32(sum & 0xFFFFFFFF) +} + +// Circular Right Shift +func shaRotr(x uint32, n uint) uint32 { + return (x >> n) | (x << (32 - n)) +} + +// Circular Left Shift +func shaRotl(x uint32, n uint) uint32 { + return (x << n) | (x >> (32 - n)) +} + // Returns Merkle–Damgård padding in bytes for length of mesage `l` // bytes. func MDPadding(l int) []byte { diff --git a/lib/sha1.go b/lib/sha1.go index 5010bfb..e4ab369 100644 --- a/lib/sha1.go +++ b/lib/sha1.go @@ -21,25 +21,6 @@ var sha1IHashValues []uint32 = []uint32{ 0xc3d2e1f0, } -// (a + b + ...) mod 2^32 -func sha1Add(n ...uint32) uint32 { - sum := uint64(0) - for _, v := range n { - sum += uint64(v) - } - return uint32(sum & 0xFFFFFFFF) -} - -// Circular Right Shift -func sha1Rotr(x uint32, n uint) uint32 { - return (x >> n) | (x << (32 - n)) -} - -// Circular Left Shift -func sha1Rotl(x uint32, n uint) uint32 { - return (x << n) | (x >> (32 - n)) -} - // SHA-1 - Function f_t(x, y, z) func sha1FT(t int, x, y, z uint32) uint32 { switch { @@ -83,7 +64,7 @@ func sha1MessageSchedule(mb []uint32) []uint32 { if t <= 15 { w = append(w, mb[t]) } else { - w = append(w, sha1Rotl(w[t-3]^w[t-8]^w[t-14]^w[t-16], 1)) + w = append(w, shaRotl(w[t-3]^w[t-8]^w[t-14]^w[t-16], 1)) } } return w @@ -142,21 +123,21 @@ func (s *Sha1) Hash() []byte { e := h[4] for t := 0; t <= 79; t++ { - tmp := sha1Add(sha1Rotl(a, 5), sha1FT(t, b, c, d), + tmp := shaAdd(shaRotl(a, 5), sha1FT(t, b, c, d), e, sha1KT(t), w[t]) e = d d = c - c = sha1Rotl(b, 30) + c = shaRotl(b, 30) b = a a = tmp } // Compute intermediate hash values. - h[0] = sha1Add(a, h[0]) - h[1] = sha1Add(b, h[1]) - h[2] = sha1Add(c, h[2]) - h[3] = sha1Add(d, h[3]) - h[4] = sha1Add(e, h[4]) + h[0] = shaAdd(a, h[0]) + h[1] = shaAdd(b, h[1]) + h[2] = shaAdd(c, h[2]) + h[3] = shaAdd(d, h[3]) + h[4] = shaAdd(e, h[4]) } // Slurp sha1 digest from hash values. |