feat: password.

This commit is contained in:
Bobo
2025-06-14 23:28:15 +08:00
parent 3ca4745bac
commit be5aa063df
19 changed files with 1109 additions and 0 deletions

63
password/rsa_test.go Normal file
View File

@@ -0,0 +1,63 @@
package password
import (
"testing"
)
func TestRSACrypto_EncryptAndDecrypt(t *testing.T) {
// 创建 RSACrypto 实例
crypto, err := NewRSACrypto(2048)
if err != nil {
t.Fatalf("创建 RSACrypto 实例失败: %v", err)
}
// 测试加密
originalData := "securedata"
encryptedData, err := crypto.Encrypt(originalData)
if err != nil {
t.Fatalf("加密失败: %v", err)
}
if encryptedData == "" {
t.Fatal("加密结果为空")
}
t.Log(encryptedData)
// 测试解密
decryptedData, err := crypto.Decrypt(encryptedData)
if err != nil {
t.Fatalf("解密失败: %v", err)
}
if decryptedData != originalData {
t.Fatalf("解密结果不匹配,期望: %s实际: %s", originalData, decryptedData)
}
}
func TestRSACrypto_ExportKeys(t *testing.T) {
// 创建 RSACrypto 实例
crypto, err := NewRSACrypto(2048)
if err != nil {
t.Fatalf("创建 RSACrypto 实例失败: %v", err)
}
// 测试导出私钥
privateKey, err := crypto.ExportPrivateKey()
if err != nil {
t.Fatalf("导出私钥失败: %v", err)
}
if privateKey == "" {
t.Fatal("导出的私钥为空")
}
// 测试导出公钥
publicKey, err := crypto.ExportPublicKey()
if err != nil {
t.Fatalf("导出公钥失败: %v", err)
}
if publicKey == "" {
t.Fatal("导出的公钥为空")
}
}