/
crypto.go
149 lines (124 loc) · 2.67 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
package util
import (
"crypto"
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"io"
"os"
)
var (
ErrUnavailable = errors.New("unavailable")
)
// sha1加密
func SHA1(str string) string {
sha1Ctx := sha1.New()
sha1Ctx.Write([]byte(str))
cipherStr := sha1Ctx.Sum(nil)
return hex.EncodeToString(cipherStr)
}
// sha1加密文件
func SHA1File(file string) string {
if f, e := os.Open(file); e == nil {
defer f.Close()
h := sha1.New()
if _, e := io.Copy(h, f); e == nil {
return hex.EncodeToString(h.Sum(nil))
}
}
return ""
}
func SHA1FileBase64(file string) string {
if f, e := os.Open(file); e == nil {
defer f.Close()
h := sha1.New()
if _, e := io.Copy(h, f); e == nil {
return base64.URLEncoding.EncodeToString(h.Sum(nil))
}
}
return ""
}
// SHA256
func SHA256(str string) string {
sha256Ctx := sha256.New()
sha256Ctx.Write([]byte(str))
cipherStr := sha256Ctx.Sum(nil)
return hex.EncodeToString(cipherStr)
}
// sha256加密文件
func SHA256File(file string) string {
if f, e := os.Open(file); e == nil {
defer f.Close()
h := sha256.New()
if _, e := io.Copy(h, f); e == nil {
return hex.EncodeToString(h.Sum(nil))
}
}
return ""
}
func SHA256FileBase64(file string) string {
if f, e := os.Open(file); e == nil {
defer f.Close()
h := sha256.New()
if _, e := io.Copy(h, f); e == nil {
return base64.URLEncoding.EncodeToString(h.Sum(nil))
}
}
return ""
}
// MD5
func MD5(str string) string {
md5Ctx := md5.New()
md5Ctx.Write([]byte(str))
cipherStr := md5Ctx.Sum(nil)
return hex.EncodeToString(cipherStr)
}
// MD5File
func MD5File(file string) string {
if f, e := os.Open(file); e == nil {
defer f.Close()
h := md5.New()
if _, e := io.Copy(h, f); e == nil {
return hex.EncodeToString(h.Sum(nil))
}
}
return ""
}
func MD5FileBase64(file string) string {
if f, e := os.Open(file); e == nil {
defer f.Close()
h := md5.New()
if _, e := io.Copy(h, f); e == nil {
return base64.URLEncoding.EncodeToString(h.Sum(nil))
}
}
return ""
}
func HMACSign(data string, key string) (string, error) {
if !crypto.SHA1.Available() {
return "", ErrUnavailable
}
hasher := hmac.New(crypto.SHA1.New, []byte(key))
hasher.Write([]byte(data))
code := hasher.Sum(nil)
return base64.URLEncoding.EncodeToString(code), nil
}
func HMACVerify(data, sign string, key string) error {
if !crypto.SHA1.Available() {
return ErrUnavailable
}
sig, err := base64.URLEncoding.DecodeString(sign)
if err != nil {
return err
}
hasher := hmac.New(crypto.SHA1.New, []byte(key))
hasher.Write([]byte(data))
if !hmac.Equal(sig, hasher.Sum(nil)) {
return ErrUnavailable
}
return nil
}