2022-01-15 22:19:29 +01:00
|
|
|
package meowlib
|
2021-10-18 21:05:44 +02:00
|
|
|
|
|
|
|
import (
|
2022-01-15 22:19:29 +01:00
|
|
|
"encoding/base64"
|
2021-10-18 21:05:44 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"testing"
|
2022-09-02 12:07:21 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-10-18 21:05:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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())
|
2022-01-15 22:19:29 +01:00
|
|
|
Armpubkey, _ := key.GetArmoredPublicKey()
|
|
|
|
pubkey := base64.StdEncoding.EncodeToString([]byte(Armpubkey))
|
2022-09-02 12:07:21 +02:00
|
|
|
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())
|
2021-10-18 21:05:44 +02:00
|
|
|
}
|
2022-09-02 12:07:21 +02:00
|
|
|
assert.Equal(t, foo, string(decMess), "The two messages should be the same.")
|
2021-10-18 21:05:44 +02:00
|
|
|
}
|