Compare commits
2 Commits
bank_card/
...
entgo/v1.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0420e35a30 | ||
|
|
0b18560901 |
@@ -6,8 +6,7 @@ import (
|
|||||||
|
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
|
"crypto/rand"
|
||||||
"math/rand/v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// DefaultAESKey 默认AES密钥(16字节)
|
// DefaultAESKey 默认AES密钥(16字节)
|
||||||
@@ -19,8 +18,7 @@ func GenerateAESKey(length int) ([]byte, error) {
|
|||||||
return nil, fmt.Errorf("invalid key length: %d, must be 16, 24, or 32 bytes", length)
|
return nil, fmt.Errorf("invalid key length: %d, must be 16, 24, or 32 bytes", length)
|
||||||
}
|
}
|
||||||
key := make([]byte, length)
|
key := make([]byte, length)
|
||||||
src := rand.ChaCha8{}
|
_, err := rand.Read(key)
|
||||||
_, err := src.Read(key)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,9 @@
|
|||||||
package crypto
|
package crypto
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"crypto/aes"
|
|
||||||
"crypto/cipher"
|
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
|
||||||
"encoding/base64"
|
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
@@ -59,44 +54,3 @@ func GenerateSalt(length int) (string, error) {
|
|||||||
}
|
}
|
||||||
return hex.EncodeToString(salt), nil
|
return hex.EncodeToString(salt), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecryptAES AES解密函数
|
|
||||||
func DecryptAES(cipherText, key string) (string, error) {
|
|
||||||
// 将密文从Base64解码
|
|
||||||
cipherData, err := base64.StdEncoding.DecodeString(cipherText)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建AES块
|
|
||||||
block, err := aes.NewCipher([]byte(key))
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查密文长度是否为块大小的倍数
|
|
||||||
if len(cipherData)%aes.BlockSize != 0 {
|
|
||||||
return "", fmt.Errorf("ciphertext is not a multiple of the block size")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建CBC解密器
|
|
||||||
iv := cipherData[:aes.BlockSize] // 提取IV
|
|
||||||
cipherData = cipherData[aes.BlockSize:]
|
|
||||||
mode := cipher.NewCBCDecrypter(block, iv)
|
|
||||||
|
|
||||||
// 解密
|
|
||||||
plainText := make([]byte, len(cipherData))
|
|
||||||
mode.CryptBlocks(plainText, cipherData)
|
|
||||||
|
|
||||||
// 去除填充
|
|
||||||
plainText = pkcs7Unpad(plainText)
|
|
||||||
|
|
||||||
return string(plainText), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// pkcs7Unpad PKCS7填充去除
|
|
||||||
func pkcs7Unpad(data []byte) []byte {
|
|
||||||
length := len(data)
|
|
||||||
padding := int(data[length-1])
|
|
||||||
return data[:length-padding]
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -26,6 +26,15 @@ func TestVerifyPassword(t *testing.T) {
|
|||||||
assert.True(t, bMatched)
|
assert.True(t, bMatched)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestVerifyPasswordWithSalt_CorrectPassword(t *testing.T) {
|
||||||
|
password := "securePassword"
|
||||||
|
salt, _ := GenerateSalt(16)
|
||||||
|
hashedPassword, _ := HashPasswordWithSalt(password, salt)
|
||||||
|
|
||||||
|
result := VerifyPasswordWithSalt(password, salt, hashedPassword)
|
||||||
|
assert.True(t, result, "Password verification should succeed with correct password and salt")
|
||||||
|
}
|
||||||
|
|
||||||
func TestJwtToken(t *testing.T) {
|
func TestJwtToken(t *testing.T) {
|
||||||
const bearerWord string = "Bearer"
|
const bearerWord string = "Bearer"
|
||||||
token := "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjowfQ.XgcKAAjHbA6o4sxxbEaMi05ingWvKdCNnyW9wowbJvs"
|
token := "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjowfQ.XgcKAAjHbA6o4sxxbEaMi05ingWvKdCNnyW9wowbJvs"
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ require (
|
|||||||
github.com/go-kratos/kratos/v2 v2.8.4
|
github.com/go-kratos/kratos/v2 v2.8.4
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/stretchr/testify v1.10.0
|
github.com/stretchr/testify v1.10.0
|
||||||
github.com/tx7do/go-utils v1.1.16
|
github.com/tx7do/go-utils v1.1.18
|
||||||
go.opentelemetry.io/otel v1.35.0
|
go.opentelemetry.io/otel v1.35.0
|
||||||
google.golang.org/protobuf v1.36.6
|
google.golang.org/protobuf v1.36.6
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user