181 lines
5.0 KiB
Go
181 lines
5.0 KiB
Go
package utils
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"math/rand"
|
|
"strings"
|
|
"time"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
// EncryptPassword 将密码加密,需要传入密码返回的是加密后的密码
|
|
func EncryptPassword(password string) (string, error) {
|
|
// 加密密码,使用 bcrypt 包当中的 GenerateFromPassword 方法,bcrypt.DefaultCost 代表使用默认加密成本
|
|
encryptPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
// 如果有错误则返回异常,加密后的空字符串返回为空字符串,因为加密失败
|
|
return "", err
|
|
} else {
|
|
// 返回加密后的密码和空异常
|
|
return string(encryptPassword), nil
|
|
}
|
|
}
|
|
|
|
// EqualsPassword 对比密码是否正确
|
|
// password未加密的
|
|
// encryptPassword 加密的
|
|
func EqualsPassword(password, encryptPassword string) bool {
|
|
// 使用 bcrypt 当中的 CompareHashAndPassword 对比密码是否正确,第一个参数为加密后的密码,第二个参数为未加密的密码
|
|
err := bcrypt.CompareHashAndPassword([]byte(encryptPassword), []byte(password))
|
|
|
|
// 对比密码是否正确会返回一个异常,按照官方的说法是只要异常是 nil 就证明密码正确
|
|
return err == nil
|
|
}
|
|
|
|
func GetSaltPassword(salt, password string) string {
|
|
pwd := fmt.Sprintf("%s%s", salt, password)
|
|
return pwd
|
|
}
|
|
|
|
// GetRandstring 取得随机字符串:使用字符串拼接
|
|
func GetRandString(length int) string {
|
|
if length < 1 {
|
|
return ""
|
|
}
|
|
char := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
charArr := strings.Split(char, "")
|
|
charlen := len(charArr)
|
|
ran := rand.New(rand.NewSource(time.Now().Unix()))
|
|
|
|
var rchar string = ""
|
|
for i := 1; i <= length; i++ {
|
|
rchar = rchar + charArr[ran.Intn(charlen)]
|
|
}
|
|
return rchar
|
|
}
|
|
|
|
func HmacSha256(key string, data string) []byte {
|
|
mac := hmac.New(sha256.New, []byte(key))
|
|
_, _ = mac.Write([]byte(data))
|
|
|
|
return mac.Sum(nil)
|
|
}
|
|
|
|
// HmacSha256ToHex 将加密后的二进制转16进制字符串
|
|
func HmacSha256ToHex(key string, data string) string {
|
|
return hex.EncodeToString(HmacSha256(key, data))
|
|
}
|
|
|
|
// HmacSha256ToBase64 将加密后的二进制转Base64字符串
|
|
func HmacSha256ToBase64(key string, data string) string {
|
|
return base64.URLEncoding.EncodeToString(HmacSha256(key, data))
|
|
}
|
|
|
|
func EncryptPhone(phone string) (string, Error) {
|
|
// 提取各部分
|
|
prefix := phone[NumberZero:NumberThree] // 前三位
|
|
middle := phone[NumberThree:NumberSeven] // 中四位
|
|
suffix := phone[NumberSeven:NumberEleven] // 尾四位
|
|
newPhone := StringEmpty
|
|
username, cErr := Crypto{}.AESEncryptECB(prefix)
|
|
if cErr != nil {
|
|
return StringEmpty, cErr
|
|
}
|
|
newPhone = username
|
|
username, cErr = Crypto{}.AESEncryptECB(middle)
|
|
if cErr != nil {
|
|
return StringEmpty, cErr
|
|
}
|
|
newPhone += username
|
|
username, cErr = Crypto{}.AESEncryptECB(suffix)
|
|
if cErr != nil {
|
|
return StringEmpty, cErr
|
|
}
|
|
newPhone += username
|
|
return newPhone, nil
|
|
}
|
|
|
|
func DecryptPhone(phone string) (string, Error) {
|
|
// 提取各部分
|
|
prefix := phone[NumberZero:24] // 前三位
|
|
middle := phone[24:48] // 中四位
|
|
suffix := phone[48:72] // 尾四位
|
|
newPhone := StringEmpty
|
|
username, cErr := Crypto{}.AESDecryptECB(prefix)
|
|
if cErr != nil {
|
|
return StringEmpty, cErr
|
|
}
|
|
newPhone = username
|
|
username, cErr = Crypto{}.AESDecryptECB(middle)
|
|
if cErr != nil {
|
|
return StringEmpty, cErr
|
|
}
|
|
newPhone += username
|
|
username, cErr = Crypto{}.AESDecryptECB(suffix)
|
|
if cErr != nil {
|
|
return StringEmpty, cErr
|
|
}
|
|
newPhone += username
|
|
return newPhone, nil
|
|
}
|
|
|
|
func GetSearchPhone(mobile string) string {
|
|
phone := StringEmpty
|
|
if len(mobile) == 11 {
|
|
prefix := mobile[NumberZero:3] // 前三位
|
|
username, cErr := Crypto{}.AESEncryptECB(prefix)
|
|
if cErr == nil {
|
|
phone = username
|
|
}
|
|
prefix = mobile[NumberThree:NumberSeven] // 中四位
|
|
username, cErr = Crypto{}.AESEncryptECB(prefix)
|
|
if cErr == nil {
|
|
phone += username
|
|
}
|
|
prefix = mobile[NumberSeven:NumberEleven] // 尾四位
|
|
username, cErr = Crypto{}.AESEncryptECB(prefix)
|
|
if cErr == nil {
|
|
phone += username
|
|
}
|
|
} else if len(mobile) > NumberTwo {
|
|
prefix := mobile[NumberZero:NumberThree] //前三位
|
|
if len(mobile) == NumberFour {
|
|
prefix = mobile[NumberZero:NumberFour] // 前四位
|
|
}
|
|
username, cErr := Crypto{}.AESEncryptECB(prefix)
|
|
if cErr == nil {
|
|
phone = username
|
|
}
|
|
if len(mobile) > NumberFour {
|
|
prefix = mobile[NumberThree:] // 中四位
|
|
username, cErr = Crypto{}.AESEncryptECB(prefix)
|
|
if cErr == nil {
|
|
phone += username
|
|
}
|
|
}
|
|
}
|
|
return phone
|
|
}
|
|
|
|
// GetRandstring 取得随机字符串:使用字符串拼接
|
|
func GetRandstring(length int) string {
|
|
if length < 1 {
|
|
return StringEmpty
|
|
}
|
|
char := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
charArr := strings.Split(char, StringEmpty)
|
|
charlen := len(charArr)
|
|
ran := rand.New(rand.NewSource(time.Now().Unix()))
|
|
|
|
var rchar string = StringEmpty
|
|
for i := 1; i <= length; i++ {
|
|
rchar = rchar + charArr[ran.Intn(charlen)]
|
|
}
|
|
return rchar
|
|
}
|