/
crypto.go
98 lines (81 loc) · 1.95 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
package util
import (
"crypto/md5"
"encoding/hex"
"errors"
"strings"
"github.com/andeya/goutil"
)
const key = "NYwxF4uj8NVvXxawnML5tyVeLmv6eCrA"
var ErrPassword = errors.New("密码错误")
var innerCrypto = NewCrypto(TrimKey(key)[:32])
// Crypto ...
type Crypto struct {
key string
}
// Decrypt ...
func (c *Crypto) Decrypt(ciphertext string) (string, error) {
v, err := goutil.AESDecrypt([]byte(c.Key()), []byte(ciphertext))
if err != nil {
return "", err
}
return string(v), nil
}
// DecryptCBC ...
func (c *Crypto) DecryptCBC(ciphertext string) (string, error) {
v, err := goutil.AESCBCDecrypt([]byte(c.Key()), []byte(ciphertext))
if err != nil {
return "", err
}
return string(v), nil
}
// Encrypt ...
func (c *Crypto) Encrypt(val string) string {
return string(goutil.AESEncrypt([]byte(c.Key()), []byte(val)))
}
// EncryptCBC ...
func (c *Crypto) EncryptCBC(val string) string {
return string(goutil.AESCBCEncrypt([]byte(c.Key()), []byte(val)))
}
// Key ...
func (c *Crypto) Key() string {
c.key = strings.TrimSpace(strings.Replace(c.key, "-", "", -1))
return c.key
}
// CheckPassword ...c cipher_password
// p plaintext
func CheckPassword(p, c string) error {
s, err := DecryptPassword(c)
if err != nil {
return err
}
if s != p {
return ErrPassword
}
return nil
}
// DecryptPassword ...
func DecryptPassword(p string) (s string, err error) {
return innerCrypto.Decrypt(strings.ToLower(p))
}
// EncryptPassword ...
func EncryptPassword(p string) string {
return strings.ToUpper(innerCrypto.Encrypt(p))
}
func MD5(s string) string {
md5New := md5.New()
md5New.Write([]byte(s))
b0 := md5New.Sum(nil)[:]
return hex.EncodeToString(b0)
}
// NewCrypto ...
func NewCrypto(key string) *Crypto {
return &Crypto{key: key}
}
// SetInnerCrypto ... must replace your own crypto
func SetInnerCrypto(k string) {
innerCrypto = NewCrypto(TrimKey(k)[:32])
}
func TrimKey(k string) string {
return strings.TrimSpace(strings.Replace(k, "-", "", -1))
}