forked from synapse-garden/mf-proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
38 lines (33 loc) · 1.01 KB
/
util.go
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
package util
import (
"crypto/sha1"
"encoding/hex"
"io"
)
type Key string
type Hash string
type Salt string
func CheckHashedPw(pw string, salt Salt, hash Hash) bool {
hashedPw := sha1.Sum([]byte(pw))
sha := sha1.New()
io.WriteString(sha, string(salt))
io.WriteString(sha, hex.EncodeToString(hashedPw[:]))
return hex.EncodeToString(sha.Sum(nil)[:]) == string(hash)
}
func HashedAndSalt(pw, saltSeed string) (Hash, Salt) {
hashedPw := sha1.Sum([]byte(pw))
sha := sha1.New()
hashedSalt := sha1.Sum([]byte(saltSeed))
io.WriteString(sha, hex.EncodeToString(hashedSalt[:]))
io.WriteString(sha, hex.EncodeToString(hashedPw[:]))
return Hash(hex.EncodeToString(sha.Sum(nil)[:])),
Salt(hex.EncodeToString(hashedSalt[:]))
}
func SaltedHash(pw, saltSeed string) Key {
hashedPw := sha1.Sum([]byte(pw))
sha := sha1.New()
hashedSalt := sha1.Sum([]byte(saltSeed))
io.WriteString(sha, hex.EncodeToString(hashedSalt[:]))
io.WriteString(sha, hex.EncodeToString(hashedPw[:]))
return Key(hex.EncodeToString(sha.Sum(nil)[:]))
}