模块介绍
使用 rsa
非对称加密算法,进行加密解密,已实现公钥加密、私钥解密。
方法说明
方法 | 说明 |
---|---|
ras.NewPublic(publicKey).Encrypt(encryptStr string) | 公钥加密 |
rsa.NewPrivate(privateKey).Decrypt(decryptStr string) | 私钥解密 |
测试用例
func TestEncrypt(t *testing.T) {
str, err := NewPublic(publicKey).Encrypt("123456")
if err != nil {
t.Error("rsa public encrypt error", err)
return
}
t.Log(str)
}
func TestDecrypt(t *testing.T) {
decryptStr := "KTKXckjkCLI6Vk_y_XROnY-a6nJpllruL-CX-v_2AFxfghA2tZ2nkQyS6d1-IIYMlgwm4ivwlzy-phLtaN9BB03htA5D9rwjA_JwYtqAG4iwuvgaDl2SiZ_H2ACv-aV1kNRgnyjh14hs0JiSt5VHEiJ3D2xYzOCKwtEzoo0WczJ-MYb3u_-bfcnm9YtvgtG5-y3Jy7WYr-IwXdBKqPO0E-jzrtY7m3Q1yC4znHdzjNpxCj0I6YRx4CZ362b706qNX7sl3E5KTJeSmYrsurB-SxQT1CaqGzVt7mshx1v2qGnv5NBNXpj7ZPKWGJbgaCUxcuxd1Mg0o81HnfbsGuSlFQ=="
str, err := NewPrivate(privateKey).Decrypt(decryptStr)
if err != nil {
t.Error("rsa private decrypt error", err)
return
}
t.Log(str)
}
基准测试
func BenchmarkEncryptAndDecrypt(b *testing.B) {
b.ResetTimer()
rsaPublic := NewPublic(publicKey)
rsaPrivate := NewPrivate(privateKey)
for i := 0; i < b.N; i++ {
encryptString, _ := rsaPublic.Encrypt("123456")
rsaPrivate.Decrypt(encryptString)
}
}
// 输出
goos: darwin
goarch: amd64
pkg: github.com/xinliangnote/go-gin-api/pkg/rsa
BenchmarkEncryptAndDecrypt
BenchmarkEncryptAndDecrypt-12 879 1326145 ns/op
PASS
Process finished with exit code 0