/
security.go
147 lines (128 loc) · 3.34 KB
/
security.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package security
import (
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
cryptoRand "crypto/rand"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io"
"math/rand"
"time"
"crypto/md5"
"hash/fnv"
)
const (
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
var (
secretKey = []byte("vnnaEPK8CJbXGuSk2qa9Zh2VetP")
)
func RandomInt(min, max int) int {
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
return r1.Intn(max-min) + min
}
func Sha256(message []byte) string {
mac := hmac.New(sha256.New, secretKey)
mac.Write(message)
expectedMAC := mac.Sum(nil)
return hex.EncodeToString(expectedMAC)
}
func Sha256Hmac(message, key []byte) []byte {
mac := hmac.New(sha256.New, key)
mac.Write(message)
expectedMAC := mac.Sum(nil)
return expectedMAC
}
func MakeTrackSecret(message string) string {
msg := []byte(message)
hash := Sha256(msg)
return hash
}
func Base64Decode(str string) string {
sDec, _ := base64.StdEncoding.DecodeString(str)
return string(sDec)
}
func Base64Encode(data string) string {
enc := base64.StdEncoding.EncodeToString([]byte(data))
return enc
}
func Base64EncodeRaw(data []byte) string {
enc := base64.RawURLEncoding.EncodeToString(data)
return enc
}
func MD5(data []byte) string {
return fmt.Sprintf("%x", md5.Sum(data))
}
func RandomString(n int) string {
b := make([]byte, n)
// A rand.Int63() generates 63 random bits, enough for letterIdxMax letters!
for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = rand.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
// EncryptText method will encrypt the text using the key provided with
// AES algo. Ref: https://tutorialedge.net/golang/go-encrypt-decrypt-aes-tutorial/
func EncryptText(text, key []byte) ([]byte, error) {
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
_, err = io.ReadFull(cryptoRand.Reader, nonce)
if err != nil {
return nil, err
}
return gcm.Seal(nonce, nonce, text, nil), nil
}
// HashStr method will hash the string to int32 value
func HashStr(value string) uint32 {
h := fnv.New32a()
h.Write([]byte(value))
return h.Sum32()
}
// DecryptText method will decrypt the text with help of a key
// Ref: https://tutorialedge.net/golang/go-encrypt-decrypt-aes-tutorial/
func DecryptText(ciphertext, key []byte) ([]byte, error) {
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
nonceSize := gcm.NonceSize()
if len(ciphertext) < nonceSize {
return nil, errors.New("Invalid ciphertext")
}
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
return plaintext, err
}
func Sha512Hash(value string) string {
h := sha512.New()
h.Write([]byte(value))
return hex.EncodeToString(h.Sum(nil))
}