/
security.go
59 lines (49 loc) · 1.18 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
package web
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"golang.org/x/crypto/bcrypt"
)
// NewSecurity new security
func NewSecurity(key []byte) (*Security, error) {
cip, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
return &Security{cip: cip}, nil
}
// Security security helper
type Security struct {
cip cipher.Block
}
// Hash ont-way encrypt
func (p *Security) Hash(plain []byte) ([]byte, error) {
return bcrypt.GenerateFromPassword(plain, 16)
}
// Check check hash
func (p *Security) Check(encode, plain []byte) bool {
return bcrypt.CompareHashAndPassword(encode, plain) == nil
}
// Encrypt encrypt
func (p *Security) Encrypt(buf []byte) ([]byte, error) {
iv := make([]byte, aes.BlockSize)
if _, err := rand.Read(iv); err != nil {
return nil, err
}
cfb := cipher.NewCFBEncrypter(p.cip, iv)
val := make([]byte, len(buf))
cfb.XORKeyStream(val, buf)
return append(val, iv...), nil
}
// Decrypt decrypt
func (p *Security) Decrypt(buf []byte) ([]byte, error) {
bln := len(buf)
cln := bln - aes.BlockSize
ct := buf[0:cln]
iv := buf[cln:bln]
cfb := cipher.NewCFBDecrypter(p.cip, iv)
val := make([]byte, cln)
cfb.XORKeyStream(val, ct)
return val, nil
}