meowlib/asymcrypt_test.go

60 lines
1.6 KiB
Go

package meowlib
import (
"encoding/base64"
"fmt"
"log"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewKeyPair(t *testing.T) {
kp := NewKeyPair()
fmt.Println(kp.Public)
fmt.Println(kp.Private)
}
func TestGetKey(t *testing.T) {
kp := NewKeyPair()
// fmt.Println(kp.Public)
// fmt.Println(kp.Private)
key := kp.GetCryptoKeyObject()
// fmt.Println(key.Armor())
Armpubkey, _ := key.GetArmoredPublicKey()
pubkey := base64.StdEncoding.EncodeToString([]byte(Armpubkey))
assert.Equal(t, kp.Public, pubkey, "The two public keys should be the same.")
//if kp.Public != pubkey {
// log.Fatal("error in public key")
//}
}
func TestAsymEncryptDecrypt(t *testing.T) {
kp := NewKeyPair()
foo := []byte("!#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~")
encMess, err := AsymEncrypt(kp.Public, foo)
if err != nil {
log.Println(err.Error())
}
println("len enc:", len(encMess))
decMess, err2 := AsymDecrypt(kp.Private, encMess)
if err2 != nil {
log.Println(err2.Error())
}
assert.Equal(t, foo, decMess, "The two messages should be the same.")
}
func TestAsymEncryptDecryptSigned(t *testing.T) {
kp := NewKeyPair()
foo := "!#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~"
encMess, sign, err := AsymEncryptAndSign(kp.Public, kp.Private, []byte(foo))
if err != nil {
log.Println(err.Error())
}
decMess, err2 := AsymDecryptAndCheck(kp.Private, kp.Public, encMess, sign)
if err2 != nil {
log.Println(err2.Error())
}
assert.Equal(t, foo, string(decMess), "The two messages should be the same.")
}