aboutsummaryrefslogtreecommitdiff
path: root/bhash/bhash.go
blob: 170855ecfb7ea7e7edd36fb932d7c18ebbd0ea34 (plain)
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
package bhash

import (
	"bytes"

	"golang.org/x/crypto/blowfish"
)

var magic = []byte("OxychromaticBlowfishSwatDynamite")

const rounds = 64

func Hash(pass, salt []byte) []byte {
	c, err := blowfish.NewSaltedCipher(pass, salt)
	if err != nil {
		panic(err)
	}
	// key expansion
	for i := 0; i < rounds; i++ {
		blowfish.ExpandKey(salt, c)
		blowfish.ExpandKey(pass, c)
	}
	// 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])
		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)
	}
	return buf.Bytes()
}

func Pbkdf() {
}