-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashUtil.go
71 lines (60 loc) · 1.79 KB
/
hashUtil.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package serverutils
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"golang.org/x/crypto/scrypt"
)
type hashUtil struct {
}
func (*hashUtil) CreateSHA256HashHex(data []byte) string {
hasher := sha256.New()
hasher.Write(data)
sha256 := hex.EncodeToString(hasher.Sum(nil))
return sha256
}
func (*hashUtil) CreateSHA256HashBase64(data []byte) string {
hasher := sha256.New()
hasher.Write(data)
sha256 := base64.StdEncoding.EncodeToString(hasher.Sum(nil))
return sha256
}
func (*hashUtil) CreateSHA1HashHex(data []byte) string {
hasher := sha1.New()
hasher.Write(data)
//sha := base64.StdEncoding.EncodeToString(hasher.Sum(nil))
sha := hex.EncodeToString(hasher.Sum(nil))
return sha
}
func (*hashUtil) CreateSHA1HashBase64(data []byte) string {
hasher := sha1.New()
hasher.Write(data)
//sha := base64.StdEncoding.EncodeToString(hasher.Sum(nil))
sha := base64.StdEncoding.EncodeToString(hasher.Sum(nil))
return sha
}
func (*hashUtil) CreateMD5Hash(data []byte) string {
hasher := md5.New()
hasher.Write(data)
return hex.EncodeToString(hasher.Sum(nil))
}
func (*hashUtil) CreateHMAC_SHA256(message string, key string) string {
mac := hmac.New(sha256.New, []byte(key))
mac.Write([]byte(message))
expectedMAC := mac.Sum(nil)
return base64.StdEncoding.EncodeToString(expectedMAC)
}
func (*hashUtil) CreateHMAC_SHA256FromBytes(message []byte, key string) string {
mac := hmac.New(sha256.New, []byte(key))
mac.Write(message)
expectedMAC := mac.Sum(nil)
return base64.StdEncoding.EncodeToString(expectedMAC)
}
func (*hashUtil) CreateScrypt(password string, salt string) (string, error) {
dk, err := scrypt.Key([]byte(password), []byte(salt), 1<<15, 8, 1, 32)
CreateScrypt := base64.StdEncoding.EncodeToString(dk)
return CreateScrypt, err
}