加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
rsa_test.go 5.97 KB
一键复制 编辑 原始数据 按行查看 历史
gouguoyin 提交于 2022-11-29 22:30 . 统一单元测试格式
package dongle
import (
"github.com/stretchr/testify/assert"
"testing"
)
var (
rsaInput = "hello world"
pkcs1PublicKey = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtdjE3fOKpAmc6eIi1I/GElIJW
hTLBZb/SpSC+Pl7bONDyt/sG7FzxMHLoxnlJnYDHmYiu0iy0s/EHhNL+1bPnBiuA
bOKi7TBgojusBLsrddCrGn08gZLRZ3ZP/oAS6+wf2/vfh2jCCKCVHbBZLHQIN3MU
0qcFQKXvfJaBTUX6gwIDAQAB
-----END PUBLIC KEY-----`
pkcs8PublicKey = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqzZNa9VrcewyU6wDoV7Y9kAHq
X1VK0B3Rb6GNmQe4zLEfce7cVTaLrc4VGTKl35tADG1cRHqtaG4S/WttpiGZBhxJ
y4MpOXb6eIPiVLsn2lL+rJo5XdbSr3gyjxEOQQ97ihtw4lDd5wMo4bIOuw1LtMez
HC1outlM6x+/BB0BSQIDAQAB
-----END PUBLIC KEY-----`
pkcs1PrivateKey = `-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCtdjE3fOKpAmc6eIi1I/GElIJWhTLBZb/SpSC+Pl7bONDyt/sG
7FzxMHLoxnlJnYDHmYiu0iy0s/EHhNL+1bPnBiuAbOKi7TBgojusBLsrddCrGn08
gZLRZ3ZP/oAS6+wf2/vfh2jCCKCVHbBZLHQIN3MU0qcFQKXvfJaBTUX6gwIDAQAB
AoGAFwAfEo56t5JcAcLNzccQVVYj2jkbO820G8hNiSxYA5WLD0QaAxcAU/Lqqbb3
ii1aUB0ppJS13NgnU6nnGGdZzUYBG1Hai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QDAOF/zWqGuY3HfV/1w
giXiWp8rc+S8tanMj5M37QQbYW5YLjUmJImoklVahv3qlgLZdEN5ZSueM5jfoSFt
Nts7AkBKoRDvSiGbi4MBbTHkzLZgfewkH/FxE7S4nctePk553fXTgCyh9ya8BRuQ
dHnxnpNkOxVPHEnnpEcVFbgrf5gjAkB7KmRI4VTiEfRgINhTJAG0VU7SH/N7+4cu
fPzfA+7ywG5c8Fa79wOB0SoB1KeUjcSLo5Ssj2fwea1F9dAeU90LAkBJQFofveaD
a3YlN4EQZOcCvJKmg7xwWuGxFVTZDVVEws7UCQbEOEEXZrNd9x0IF5kpPLR+rxua
RPgUNaDGIh5o
-----END PRIVATE KEY-----`
)
func TestRsa_PKCS1_ToString(t *testing.T) {
e1 := Encrypt.FromString("").ByRsa(pkcs1PublicKey)
d1 := Decrypt.FromHexString(e1.ToHexString()).ByRsa(pkcs1PrivateKey, PKCS1)
assert.Nil(t, d1.Error)
assert.Equal(t, "", d1.ToString())
e2 := Encrypt.FromString(rsaInput).ByRsa(pkcs1PublicKey)
d2 := Decrypt.FromHexString(e2.ToHexString()).ByRsa(pkcs1PrivateKey, PKCS1)
assert.Nil(t, d2.Error)
assert.Equal(t, rsaInput, d2.ToString())
}
func TestRsa_PKCS1_ToBytes(t *testing.T) {
e1 := Encrypt.FromBytes([]byte("")).ByRsa([]byte(pkcs1PublicKey))
d1 := Decrypt.FromHexBytes(e1.ToHexBytes()).ByRsa([]byte(pkcs1PrivateKey), PKCS1)
assert.Nil(t, d1.Error)
assert.Equal(t, []byte(""), d1.ToBytes())
e2 := Encrypt.FromBytes([]byte(rsaInput)).ByRsa([]byte(pkcs1PublicKey))
d2 := Decrypt.FromHexBytes(e2.ToHexBytes()).ByRsa([]byte(pkcs1PrivateKey), PKCS1)
assert.Nil(t, d2.Error)
assert.Equal(t, []byte(rsaInput), d2.ToBytes())
}
func TestRsa_PKCS8_ToString(t *testing.T) {
e1 := Encrypt.FromString("").ByRsa(pkcs8PublicKey)
d1 := Decrypt.FromHexString(e1.ToHexString()).ByRsa(pkcs8PrivateKey, PKCS1)
assert.Nil(t, d1.Error)
assert.Equal(t, "", d1.ToString())
e2 := Encrypt.FromString(rsaInput).ByRsa(pkcs8PublicKey)
d2 := Decrypt.FromHexString(e2.ToHexString()).ByRsa(pkcs8PrivateKey, PKCS8)
assert.Nil(t, d2.Error)
assert.Equal(t, rsaInput, d2.ToString())
}
func TestRsa_PKCS8_ToBytes(t *testing.T) {
e1 := Encrypt.FromBytes([]byte("")).ByRsa([]byte(pkcs8PublicKey))
d1 := Decrypt.FromHexBytes(e1.ToHexBytes()).ByRsa([]byte(pkcs8PrivateKey), PKCS1)
assert.Nil(t, d1.Error)
assert.Equal(t, []byte(""), d1.ToBytes())
e2 := Encrypt.FromBytes([]byte(rsaInput)).ByRsa([]byte(pkcs8PublicKey))
d2 := Decrypt.FromHexBytes(e2.ToHexBytes()).ByRsa([]byte(pkcs8PrivateKey), PKCS8)
assert.Nil(t, d2.Error)
assert.Equal(t, []byte(rsaInput), d2.ToBytes())
}
func TestRsa_PublicKey_Error(t *testing.T) {
e1 := Encrypt.FromBytes([]byte(rsaInput)).ByRsa([]byte("xxxx"))
assert.Equal(t, invalidPublicKeyError(), e1.Error)
e2 := Encrypt.FromBytes([]byte(rsaInput)).ByRsa([]byte(`-----BEGIN PUBLIC KEY-----
xxxx
-----END PUBLIC KEY-----`))
assert.Equal(t, invalidPublicKeyError(), e2.Error)
}
func TestRsa_PrivateKey_Error(t *testing.T) {
e1 := Encrypt.FromBytes([]byte(rsaInput)).ByRsa([]byte(pkcs1PublicKey))
d1 := Decrypt.FromHexString(e1.ToHexString()).ByRsa([]byte(pkcs1PrivateKey), PKCS8)
assert.Equal(t, invalidPrivateKeyError(), d1.Error)
e2 := Encrypt.FromBytes([]byte(rsaInput)).ByRsa([]byte(pkcs8PublicKey))
d2 := Decrypt.FromHexBytes(e2.ToHexBytes()).ByRsa([]byte(pkcs8PrivateKey), PKCS1)
assert.Equal(t, invalidPrivateKeyError(), d2.Error)
e3 := Encrypt.FromBytes([]byte(rsaInput)).ByRsa([]byte(pkcs1PublicKey))
d3 := Decrypt.FromHexBytes(e3.ToHexBytes()).ByRsa("xxxx", PKCS1)
assert.Equal(t, invalidPrivateKeyError(), d3.Error)
e4 := Encrypt.FromString(rsaInput).ByRsa([]byte(pkcs1PublicKey))
d4 := Decrypt.FromHexBytes(e4.ToHexBytes()).ByRsa([]byte(`-----BEGIN PRIVATE KEY-----
xxxx
-----END PRIVATE KEY-----`), PKCS8)
assert.Equal(t, invalidPrivateKeyError(), d4.Error)
e5 := Encrypt.FromString(rsaInput).ByRsa(pkcs1PublicKey)
d5 := Decrypt.FromHexString(e5.ToHexString()).ByRsa(`-----BEGIN PRIVATE KEY-----
xxxx
-----END PRIVATE KEY-----`, "xxxx")
assert.Equal(t, invalidPrivateKeyError(), d5.Error)
e6 := Encrypt.FromBytes([]byte(rsaInput)).ByRsa([]byte(pkcs1PublicKey))
d6 := Decrypt.FromHexBytes(e6.ToHexBytes()).ByRsa([]byte(pkcs1PrivateKey), "xxxx")
assert.Equal(t, invalidPrivateKeyError(), d6.Error)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化