meowlib/asymcrypt_test.go
2022-09-02 12:07:21 +02:00

59 lines
1.4 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 TestEncryptDecrypt(t *testing.T) {
kp := NewKeyPair()
foo := "totoaimelesfrites!"
encMess, err := Encrypt(kp.Public, []byte(foo))
if err != nil {
log.Println(err.Error())
}
decMess, err2 := Decrypt(kp.Private, encMess)
if err2 != nil {
log.Println(err2.Error())
}
assert.Equal(t, foo, decMess, "The two messages should be the same.")
}
func TestEncryptDecryptSigned(t *testing.T) {
kp := NewKeyPair()
foo := "totoaimelesfrites!"
encMess, sign, err := EncryptAndSign(kp.Public, kp.Private, []byte(foo))
if err != nil {
log.Println(err.Error())
}
decMess, err2 := DecryptAndCheck(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.")
}