-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.go
48 lines (42 loc) · 1023 Bytes
/
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
package crypter
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/litsoftware/litmedia/internal/pkg/config"
"strings"
)
//加密
func Encrypt(value string) (string, error) {
return AesEncrypt(value, getKey())
}
//解密
func Decrypt(value string) (string, error) {
return AesDecrypt(value, getKey())
}
func checkMAC(message, msgMac, secret string) bool {
expectedMAC := computeHmacSha256(message, secret)
fmt.Println(expectedMAC, msgMac)
return hmac.Equal([]byte(expectedMAC), []byte(msgMac))
}
func computeHmacSha256(message string, secret string) string {
key := []byte(secret)
h := hmac.New(sha256.New, key)
h.Write([]byte(message))
sha := hex.EncodeToString(h.Sum(nil))
return sha
}
//处理密钥
func getKey() string {
appKey := config.GetString("app_key")
if strings.HasPrefix(appKey, "base64:") {
split := appKey[7:]
if key, err := base64.StdEncoding.DecodeString(split); err == nil {
return string(key)
}
return split
}
return appKey
}