-
Notifications
You must be signed in to change notification settings - Fork 4
/
crypto.go
41 lines (32 loc) · 862 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
package xcrypto
import (
"os"
"github.com/fufuok/utils"
"github.com/fufuok/utils/xhash"
)
// SetenvEncrypt 加密并设置环境变量(string)
func SetenvEncrypt(key, value, secret string) (string, error) {
value = Encrypt(value, secret)
if err := os.Setenv(key, value); err != nil {
return "", err
}
return value, nil
}
// GetenvDecrypt 解密环境变量参数(string)
func GetenvDecrypt(key string, secret string) string {
return Decrypt(os.Getenv(key), secret)
}
// Encrypt 加密 (密钥取 32 位 MD5, AES-CBC, base58)
func Encrypt(value, secret string) string {
if secret != "" {
value = AesCBCEnStringB58(value, utils.S2B(xhash.MD5Hex(secret)))
}
return value
}
// Decrypt 解密
func Decrypt(value, secret string) string {
if secret != "" {
value = AesCBCDeStringB58(value, utils.S2B(xhash.MD5Hex(secret)))
}
return value
}