From c9c7ca1c6191844feaac28eaecdc684a2384537d Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 1 May 2017 01:55:17 +0200 Subject: swap --- bhash/bhash.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'bhash') diff --git a/bhash/bhash.go b/bhash/bhash.go index a9369ab..3dc1ced 100644 --- a/bhash/bhash.go +++ b/bhash/bhash.go @@ -8,6 +8,7 @@ import ( "bytes" "crypto/sha512" "encoding/binary" + "io" "golang.org/x/crypto/blowfish" ) @@ -16,9 +17,17 @@ var ( magic = []byte("OxychromaticBlowfishSwatDynamite") rounds = 64 words = blowfish.BlockSize - hashSize = words * 4 + hashSize = 4 * words + be = binary.BigEndian + le = binary.LittleEndian ) +func swap(w io.Writer, data []byte) { + var u [2]uint32 + binary.Read(bytes.NewReader(data), le, &u) + binary.Write(w, be, u) +} + // Hash computes bcrypt hash func Hash(pass, salt []byte) []byte { c, err := blowfish.NewSaltedCipher(pass, salt) @@ -39,9 +48,7 @@ func Hash(pass, salt []byte) []byte { c.Encrypt(v, v) } // swap bytes and copy out - var u [2]uint32 - binary.Read(bytes.NewReader(v), binary.BigEndian, &u) - binary.Write(buf, binary.LittleEndian, u) + swap(buf, v) } return buf.Bytes() } @@ -61,7 +68,7 @@ func Pbkdf(pass, salt []byte, iter, keyLen int) []byte { // first round, salt is salt h.Reset() h.Write(salt) - binary.Write(h, binary.BigEndian, uint32(n)) + binary.Write(h, be, uint32(n)) tmp := Hash(sha2pass, h.Sum(nil)) copy(out, tmp) -- cgit v1.2.3