aboutsummaryrefslogtreecommitdiff
path: root/bhash/bhash.go
diff options
context:
space:
mode:
Diffstat (limited to 'bhash/bhash.go')
-rw-r--r--bhash/bhash.go54
1 files changed, 21 insertions, 33 deletions
diff --git a/bhash/bhash.go b/bhash/bhash.go
index 422ab76..2f1b626 100644
--- a/bhash/bhash.go
+++ b/bhash/bhash.go
@@ -3,7 +3,7 @@ package bhash
import (
"bytes"
"crypto/sha512"
- "log"
+ "encoding/binary"
"golang.org/x/crypto/blowfish"
)
@@ -24,17 +24,17 @@ func Hash(pass, salt []byte) []byte {
}
// encryption
buf := new(bytes.Buffer)
- bSz := c.BlockSize()
- for n := 0; n < len(magic)/bSz; n++ {
- b := make([]byte, bSz)
- copy(b, magic[n*bSz:(n+1)*bSz])
+ blockSize := c.BlockSize()
+ for n := 0; n < len(magic)/blockSize; n++ {
+ b := make([]byte, blockSize)
+ copy(b, magic[n*blockSize:(n+1)*blockSize])
for i := 0; i < rounds; i++ {
c.Encrypt(b, b)
}
- // copy out
- b[0], b[1], b[2], b[3] = b[3], b[2], b[1], b[0]
- b[4], b[5], b[6], b[7] = b[7], b[6], b[5], b[4]
- buf.Write(b)
+ // swap bytes and copy out
+ var u [2]uint32
+ binary.Read(bytes.NewReader(b), binary.BigEndian, &u)
+ binary.Write(buf, binary.LittleEndian, u)
}
return buf.Bytes()
}
@@ -45,41 +45,29 @@ func Pbkdf(pass, salt []byte, iter, keyLen int) []byte {
h.Write(pass)
sha2pass := h.Sum(nil)
- out := make([]byte, 4*blowfish.BlockSize) // XXX
+ hashLen := 4 * blowfish.BlockSize
+ numBlocks := (keyLen + hashLen - 1) / hashLen
- for count := 1; keyLen > 0; count++ {
- countSalt := []byte{
- byte(count >> 24),
- byte(count >> 16),
- byte(count >> 8),
- byte(count),
- }
+ out := make([]byte, hashLen)
+ buf := new(bytes.Buffer)
+ for block := 1; block <= numBlocks; block++ {
// first round, salt is salt
h.Reset()
h.Write(salt)
- h.Write(countSalt)
- sha2salt := h.Sum(nil)
- tmp := Hash(sha2pass, sha2salt)
+ binary.Write(h, binary.BigEndian, uint32(block))
+ tmp := Hash(sha2pass, h.Sum(nil))
copy(out, tmp)
- log.Println(out)
for i := 1; i < iter; i++ {
h.Reset()
h.Write(tmp)
- sha2salt = h.Sum(nil)
- tmp = Hash(sha2pass, sha2salt)
- for i := range out {
- out[i] ^= tmp[i]
+ tmp = Hash(sha2pass, h.Sum(nil))
+ for x := range tmp {
+ out[x] ^= tmp[x]
}
- log.Println(out)
}
-
- // pbkdf2 deviation: output the key material non-linearly
- // TODO
-
- keyLen-- // XXX
+ buf.Write(out)
}
-
- return nil
+ return buf.Bytes()[:keyLen]
}