2022-04-11 19:49:34 +08:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
2022-08-26 19:22:56 +08:00
|
|
|
"crypto/md5" // #nosec
|
2022-06-29 20:02:18 +08:00
|
|
|
"crypto/sha256"
|
2022-04-11 19:49:34 +08:00
|
|
|
"encoding/base64"
|
2022-06-29 20:02:18 +08:00
|
|
|
"encoding/hex"
|
2022-04-11 19:49:34 +08:00
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
2022-06-29 20:02:18 +08:00
|
|
|
func SHA256(src string, salt string) string {
|
|
|
|
h := sha256.New()
|
|
|
|
h.Write([]byte(src + salt))
|
|
|
|
sum := h.Sum(nil)
|
|
|
|
s := hex.EncodeToString(sum)
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-04-11 19:49:34 +08:00
|
|
|
// PasswordEncrypt encrypt password
|
|
|
|
func PasswordEncrypt(pwd string) (string, error) {
|
2022-06-29 20:02:18 +08:00
|
|
|
bytes, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.MinCost)
|
2022-04-11 19:49:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(bytes), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func Base64Decode(pwd string) (string, error) {
|
|
|
|
bytes, err := base64.StdEncoding.DecodeString(pwd)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(bytes), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func Base64Encode(pwd string) string {
|
|
|
|
return base64.StdEncoding.EncodeToString([]byte(pwd))
|
|
|
|
}
|
2022-08-26 19:22:56 +08:00
|
|
|
|
|
|
|
func MD5(str string) string {
|
|
|
|
// #nosec
|
|
|
|
data := md5.Sum([]byte(str))
|
|
|
|
return hex.EncodeToString(data[:])[8:24]
|
|
|
|
}
|