-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.go
206 lines (181 loc) · 4.47 KB
/
crypto.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package crypto
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/md5"
"crypto/rand"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"errors"
"io"
"math/big"
"time"
std "github.com/PKUJohnson/solar/std"
"golang.org/x/crypto/bcrypt"
)
const (
numberchars = "1234567890"
)
func GetMD5Content(content string) string {
md5Ctx := md5.New()
md5Ctx.Write([]byte(content))
cipherStr := md5Ctx.Sum(nil)
return hex.EncodeToString(cipherStr)
}
func GetMD5Content2(content []byte) string {
md5Ctx := md5.New()
md5Ctx.Write(content)
cipherStr := md5Ctx.Sum(nil)
return hex.EncodeToString(cipherStr)
}
func Base64Encode(src []byte) string {
return base64.StdEncoding.EncodeToString(src)
}
func Base64Decode(src string) ([]byte, error) {
return base64.StdEncoding.DecodeString(src)
}
func Base64URLEncode(src []byte) string {
return base64.URLEncoding.EncodeToString(src)
}
func Base64URLDecode(src string) ([]byte, error) {
return base64.URLEncoding.DecodeString(src)
}
func RandInt63(max int64) int64 {
var maxbi big.Int
maxbi.SetInt64(max)
value, _ := rand.Int(rand.Reader, &maxbi)
return value.Int64()
}
func RandNumStr(l int) string {
ret := make([]byte, 0, l)
for i := 0; i < l; i++ {
index := RandInt63(int64(len(numberchars)))
ret = append(ret, numberchars[index])
}
return string(ret)
}
func RandBytes(c int) ([]byte, error) {
b := make([]byte, c)
if _, err := rand.Read(b); err != nil {
return nil, err
} else {
return b, nil
}
}
func SimpleGuid() string {
b := make([]byte, 24)
binary.LittleEndian.PutUint64(b, uint64(time.Now().UnixNano()))
if _, err := rand.Read(b[8:]); err != nil {
return ""
} else {
return base64.RawURLEncoding.EncodeToString(b)
}
}
func SaltPassword(password string) []byte {
h, err := bcrypt.GenerateFromPassword([]byte(password), 10)
if err != nil {
std.LogErrorLn("Bcrypt salt password error: ", err)
return nil
}
return h
}
func VerifyPassword(stored []byte, password string) bool {
return bcrypt.CompareHashAndPassword(stored, []byte(password)) == nil
}
func SaltPassword2(password string) []byte {
if rb, err := RandBytes(64); err != nil {
return nil
} else {
pw := append(rb, []byte(password)...)
hash := sha512.Sum512(pw)
return append(rb, hash[:]...)
}
}
func VerifyPassword2(stored []byte, password string) bool {
if len(stored) != 128 {
return false
}
pw := append([]byte{}, stored[:64]...)
pw = append(pw, []byte(password)...)
hash := sha512.Sum512(pw)
return bytes.Equal(hash[:], stored[64:])
}
func Md5(data []byte) []byte {
sum := md5.Sum(data)
return sum[:]
}
func Md5Str(data []byte) string {
return base64.StdEncoding.EncodeToString(Md5(data))
}
func Encrypt(data, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
plaintext := padding(data)
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext[aes.BlockSize:], plaintext)
return appendHmac(ciphertext, key), nil
}
func Decrypt(data, key []byte) ([]byte, error) {
data, err := removeHmac(data, key)
if err != nil {
return nil, err
}
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
iv := data[:aes.BlockSize]
data = data[aes.BlockSize:]
// CBC mode always works in whole blocks.
if len(data)%aes.BlockSize != 0 {
errors.New("data is not a multiple of the block size")
}
mode := cipher.NewCBCDecrypter(block, iv)
plaintext := make([]byte, len(data))
mode.CryptBlocks(plaintext, data)
return unPadding(plaintext), nil
}
func padding(data []byte) []byte {
blockSize := aes.BlockSize
padding := blockSize - len(data)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(data, padtext...)
}
func unPadding(data []byte) []byte {
l := len(data)
unpadding := int(data[l-1])
return data[:(l - unpadding)]
}
func appendHmac(data, key []byte) []byte {
mac := hmac.New(sha256.New, key)
mac.Write(data)
hash := mac.Sum(nil)
return append(data, hash...)
}
func removeHmac(data, key []byte) ([]byte, error) {
if len(data) < sha256.Size {
return nil, errors.New("Invalid length")
}
p := len(data) - sha256.Size
mmac := data[p:]
mac := hmac.New(sha256.New, key)
mac.Write(data[:p])
exp := mac.Sum(nil)
if hmac.Equal(mmac, exp) {
return data[:p], nil
} else {
return nil, errors.New("MAC doesn't match")
}
}