-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase64.go
More file actions
41 lines (35 loc) · 897 Bytes
/
base64.go
File metadata and controls
41 lines (35 loc) · 897 Bytes
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
package rose
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
)
// Base64Encode 对字符串进行Base64编码
func Base64Encode(str string) string {
return base64.StdEncoding.EncodeToString([]byte(str))
}
// Base64Decode 对字符串进行Base64解码
func Base64Decode(str string) (string, error) {
data, err := base64.StdEncoding.DecodeString(str)
if err != nil {
return "", err
} else {
return string(data), nil
}
}
func Base64URLEncode(str string) string {
return base64.RawURLEncoding.EncodeToString([]byte(str))
}
func Base64URLDecode(str string) (string, error) {
data, err := base64.RawURLEncoding.DecodeString(str)
if err != nil {
return "", err
} else {
return string(data), nil
}
}
func Base64HmacSha256(data, key string) string {
h := hmac.New(sha256.New, []byte(key))
h.Write([]byte(data))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}