44 lines
720 B
Go
44 lines
720 B
Go
package tool
|
|
|
|
import "testing"
|
|
|
|
func TestRSA(t *testing.T) {
|
|
var (
|
|
crt, key string
|
|
)
|
|
|
|
cfg, err := LoadTLSConfig(StringToBytes(crt), StringToBytes(key))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
raw := []byte("admin")
|
|
enc, err := RSAEncrypt(raw, cfg)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
t.Logf("Encrypted data: %s", enc)
|
|
|
|
org, err := RSADecrypt(enc, cfg)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
t.Logf("Decrypted data: %s", string(org))
|
|
|
|
enc2, err := RSAEncryptByCert(raw, []byte(crt))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
org2, err := RSADecrypt(enc2, cfg)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if string(org) != string(org2) {
|
|
t.Fatalf("Original and decrypted data don't match, org1 = %s, org2 = %s", org, org2)
|
|
}
|
|
}
|