You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
3.6 KiB
Go
143 lines
3.6 KiB
Go
package cryptokit
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/aes"
|
|
"crypto/md5"
|
|
"crypto/sha1"
|
|
"cu-helper/cus-eng-con-sys/config"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"github.com/mizuki1412/go-core-kit/class/exception"
|
|
"github.com/spf13/cast"
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
func Encrypt(word string) string {
|
|
oraginkey := getEncryptKey()
|
|
secretKey := getSecretKey(oraginkey)
|
|
encrypted := aesEncryptECB([]byte(word), secretKey)
|
|
return encrypted
|
|
}
|
|
|
|
func Decrypt(word string) string {
|
|
oraginkey := getEncryptKey()
|
|
secretKey := getSecretKey(oraginkey)
|
|
decrypted := aesDecryptECB(word, secretKey)
|
|
return decrypted
|
|
}
|
|
|
|
func RandomStr(flag bool, min, max int) string {
|
|
var str string
|
|
var rangeNum int
|
|
arr := []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
|
if flag {
|
|
rangeNum = rand.Intn(max-min+1) + min
|
|
} else {
|
|
rangeNum = min
|
|
}
|
|
for i := 0; i < rangeNum; i++ {
|
|
index := rand.Intn(len(arr))
|
|
str += string(arr[index])
|
|
}
|
|
return str
|
|
}
|
|
|
|
func GetTimeStamp() string {
|
|
return cast.ToString(time.Now().UnixNano() / int64(time.Millisecond))
|
|
}
|
|
|
|
// getEncryptKey 获取密钥
|
|
func getEncryptKey() string {
|
|
sid := config.Conf.Sid
|
|
//if sid == "" {
|
|
// sid = "1AcTYc7684QBbd376mj51V235"
|
|
//}
|
|
// 生成 sid 的子字符串
|
|
subStr := sid[3:] + sid[9:12]
|
|
// 计算 MD5 哈希
|
|
hash := md5.Sum([]byte(subStr))
|
|
return hex.EncodeToString(hash[:])
|
|
}
|
|
|
|
func sha1T(input string) []byte {
|
|
hash := sha1.New()
|
|
hash.Write([]byte(input))
|
|
return hash.Sum(nil)
|
|
}
|
|
|
|
func sha1Hex(input []byte) string {
|
|
hash := sha1.New()
|
|
hash.Write(input)
|
|
return hex.EncodeToString(hash.Sum(nil))
|
|
}
|
|
|
|
func getSecretKey(key string) []byte {
|
|
/*
|
|
var hexStr = CryptoJS.SHA1(CryptoJS.SHA1(oraginkey)).toString().substring(0, 32);
|
|
写这段JS代码的人真变态……害我要这么实现
|
|
*/
|
|
hexStr := sha1Hex(sha1T(key))[:32]
|
|
secretKey, _ := hex.DecodeString(hexStr)
|
|
return secretKey
|
|
}
|
|
|
|
func pkcs7Padding(src []byte, blockSize int) []byte {
|
|
padding := blockSize - len(src)%blockSize
|
|
padText := bytes.Repeat([]byte{byte(padding)}, padding)
|
|
return append(src, padText...)
|
|
}
|
|
|
|
func pkcs7UnPadding(src []byte) []byte {
|
|
length := len(src)
|
|
if length == 0 {
|
|
panic(exception.New("unpad error: input length is zero"))
|
|
}
|
|
unPadding := int(src[length-1])
|
|
if unPadding > length {
|
|
panic(exception.New("unpad error: padding size is invalid"))
|
|
}
|
|
return src[:(length - unPadding)]
|
|
}
|
|
|
|
// aesEncryptECB encrypts plaintext using AES in ECB mode with PKCS7 padding
|
|
func aesEncryptECB(plaintext, key []byte) string {
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
panic(exception.New(err.Error()))
|
|
}
|
|
blockSize := block.BlockSize()
|
|
plaintext = pkcs7Padding(plaintext, blockSize)
|
|
ciphertext := make([]byte, len(plaintext))
|
|
for bs, be := 0, blockSize; bs < len(plaintext); bs, be = bs+blockSize, be+blockSize {
|
|
block.Encrypt(ciphertext[bs:be], plaintext[bs:be])
|
|
}
|
|
return base64.StdEncoding.EncodeToString(ciphertext)
|
|
}
|
|
|
|
// aesDecryptECB decrypts ciphertext using AES in ECB mode with PKCS7 unPadding
|
|
func aesDecryptECB(ciphertextBase64 string, key []byte) string {
|
|
ciphertext, err := base64.StdEncoding.DecodeString(ciphertextBase64)
|
|
if err != nil {
|
|
panic(exception.New(err.Error()))
|
|
}
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
panic(exception.New(err.Error()))
|
|
}
|
|
blockSize := block.BlockSize()
|
|
if len(ciphertext)%blockSize != 0 {
|
|
panic(exception.New(err.Error()))
|
|
}
|
|
plaintext := make([]byte, len(ciphertext))
|
|
for bs, be := 0, blockSize; bs < len(ciphertext); bs, be = bs+blockSize, be+blockSize {
|
|
block.Decrypt(plaintext[bs:be], ciphertext[bs:be])
|
|
}
|
|
plaintext = pkcs7UnPadding(plaintext)
|
|
if err != nil {
|
|
panic(exception.New(err.Error()))
|
|
}
|
|
return string(plaintext)
|
|
}
|