新版本,代理功能上线

This commit is contained in:
2025-03-07 03:48:59 +08:00
parent 8ead7a7a67
commit b2d9d828e3
130 changed files with 11004 additions and 2209 deletions

View File

@@ -0,0 +1,67 @@
package crypto
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"errors"
"io"
)
// AES CBC模式加密Base64传入传出
func AesEncryptURL(plainText, key []byte) (string, error) {
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
blockSize := block.BlockSize()
plainText = PKCS7Padding(plainText, blockSize)
cipherText := make([]byte, blockSize+len(plainText))
iv := cipherText[:blockSize] // 使用前blockSize字节作为IV
_, err = io.ReadFull(rand.Reader, iv)
if err != nil {
return "", err
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(cipherText[blockSize:], plainText)
return base64.URLEncoding.EncodeToString(cipherText), nil
}
// AES CBC模式解密Base64传入传出
func AesDecryptURL(cipherTextBase64 string, key []byte) ([]byte, error) {
cipherText, err := base64.URLEncoding.DecodeString(cipherTextBase64)
if err != nil {
return nil, err
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
if len(cipherText) < blockSize {
return nil, errors.New("ciphertext too short")
}
iv := cipherText[:blockSize]
cipherText = cipherText[blockSize:]
if len(cipherText)%blockSize != 0 {
return nil, errors.New("ciphertext is not a multiple of the block size")
}
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(cipherText, cipherText)
plainText, err := PKCS7UnPadding(cipherText)
if err != nil {
return nil, err
}
return plainText, nil
}

View File

@@ -36,3 +36,37 @@ func NullTimeToTime(nt sql.NullTime) time.Time {
}
return time.Time{} // 返回零值时间
}
// Int64ToNullInt64 将 int64 转换为 sql.NullInt64
func Int64ToNullInt64(i int64) sql.NullInt64 {
return sql.NullInt64{
Int64: i,
Valid: i != 0, // 仅当 i 非零时才设置为有效
}
}
// NullInt64ToInt64 将 sql.NullInt64 转换为 int64
func NullInt64ToInt64(ni sql.NullInt64) int64 {
if ni.Valid {
return ni.Int64
}
return 0 // 返回零值 int64
}
// Float64ToNullFloat64 将 float64 转换为 sql.NullFloat64
// Valid 字段在 f 非零时设置为有效注意NaN 会被视为有效,需结合业务场景使用)
func Float64ToNullFloat64(f float64) sql.NullFloat64 {
return sql.NullFloat64{
Float64: f,
Valid: f != 0,
}
}
// NullFloat64ToFloat64 将 sql.NullFloat64 转换为 float64
// 当 Valid 为 false 时,返回 float64 零值
func NullFloat64ToFloat64(nf sql.NullFloat64) float64 {
if nf.Valid {
return nf.Float64
}
return 0.0
}