Compare commits

...

5 Commits

Author SHA1 Message Date
Bobo
efc24c452f feat: crypto 2025-05-07 16:00:40 +08:00
Bobo
78cef077e5 feat: crypto 2025-05-07 15:57:19 +08:00
Bobo
0420e35a30 feat: crypto 2025-05-07 15:05:18 +08:00
Bobo
0b18560901 feat: crypto 2025-05-07 14:42:12 +08:00
Bobo
7b29f09e37 feat: entgo 2025-05-07 14:07:13 +08:00
8 changed files with 62 additions and 70 deletions

View File

@@ -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
} }
@@ -42,23 +40,44 @@ func PKCS5UnPadding(origData []byte) []byte {
} }
// AesEncrypt AES加密 // AesEncrypt AES加密
func AesEncrypt(origData, key []byte) ([]byte, error) { func AesEncrypt(plainText, key, iv []byte) ([]byte, error) {
if plainText == nil {
return nil, fmt.Errorf("plain text is nil")
}
if key == nil {
return nil, fmt.Errorf("key is nil")
}
block, err := aes.NewCipher(key) block, err := aes.NewCipher(key)
if err != nil { if err != nil {
return nil, err return nil, err
} }
//AES分组长度为128位所以blockSize=16单位字节 // AES分组长度为128位所以blockSize=16单位字节
blockSize := block.BlockSize() blockSize := block.BlockSize()
origData = PKCS5Padding(origData, blockSize)
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize]) //初始向量的长度必须等于块block的长度16字节 if iv == nil {
crypted := make([]byte, len(origData)) // 初始向量的长度必须等于块block的长度16字节
blockMode.CryptBlocks(crypted, origData) iv = key[:blockSize]
return crypted, nil }
plainText = PKCS5Padding(plainText, blockSize)
blockMode := cipher.NewCBCEncrypter(block, iv)
cryptedText := make([]byte, len(plainText))
blockMode.CryptBlocks(cryptedText, plainText)
return cryptedText, nil
} }
// AesDecrypt AES解密 // AesDecrypt AES解密
func AesDecrypt(crypted, key []byte) ([]byte, error) { func AesDecrypt(cryptedText, key, iv []byte) ([]byte, error) {
if cryptedText == nil {
return nil, fmt.Errorf("crypted text is nil")
}
if key == nil {
return nil, fmt.Errorf("key is nil")
}
block, err := aes.NewCipher(key) block, err := aes.NewCipher(key)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -66,9 +85,16 @@ func AesDecrypt(crypted, key []byte) ([]byte, error) {
//AES分组长度为128位所以blockSize=16单位字节 //AES分组长度为128位所以blockSize=16单位字节
blockSize := block.BlockSize() blockSize := block.BlockSize()
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize]) //初始向量的长度必须等于块block的长度16字节
origData := make([]byte, len(crypted)) if iv == nil {
blockMode.CryptBlocks(origData, crypted) // 初始向量的长度必须等于块block的长度16字节
origData = PKCS5UnPadding(origData) iv = key[:blockSize]
return origData, nil }
blockMode := cipher.NewCBCDecrypter(block, iv)
plainText := make([]byte, len(cryptedText))
blockMode.CryptBlocks(plainText, cryptedText)
plainText = PKCS5UnPadding(plainText)
return plainText, nil
} }

View File

@@ -15,7 +15,7 @@ func TestDecryptAES(t *testing.T) {
aesKey = DefaultAESKey aesKey = DefaultAESKey
plainText := []byte("cloud123456") plainText := []byte("cloud123456")
encryptText, err := AesEncrypt(plainText, aesKey) encryptText, err := AesEncrypt(plainText, aesKey, nil)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
@@ -30,7 +30,7 @@ func TestDecryptAES(t *testing.T) {
return return
} }
decryptText, err := AesDecrypt(bytesPass, aesKey) decryptText, err := AesDecrypt(bytesPass, aesKey, nil)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return

View File

@@ -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]
}

View File

@@ -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"

View File

@@ -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
) )

View File

@@ -1,15 +1,17 @@
package entgo package entgo
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/tx7do/go-utils/stringcase"
"strings" "strings"
"encoding/json"
"entgo.io/ent/dialect" "entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql"
"github.com/go-kratos/kratos/v2/encoding" "github.com/go-kratos/kratos/v2/encoding"
"github.com/tx7do/go-utils/stringcase"
) )
type FilterOp int type FilterOp int

View File

@@ -10,6 +10,7 @@ import (
"github.com/go-kratos/kratos/v2/encoding" "github.com/go-kratos/kratos/v2/encoding"
_ "github.com/go-kratos/kratos/v2/encoding/json" _ "github.com/go-kratos/kratos/v2/encoding/json"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )

View File

@@ -1,9 +1,9 @@
git tag v1.1.17 git tag v1.1.19
git tag bank_card/v1.1.5 git tag bank_card/v1.1.5
git tag geoip/v1.1.5 git tag geoip/v1.1.5
git tag entgo/v1.1.26 git tag entgo/v1.1.27
git tag gorm/v1.1.5 git tag gorm/v1.1.5
git push origin --tags git push origin --tags