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")
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
2022-09-18 21:17:28 +02:00
|
|
|
func TestAsymEncryptDecrypt(t *testing.T) {
|
2022-09-02 12:07:21 +02:00
|
|
|
kp := NewKeyPair()
|
2022-09-18 21:17:28 +02:00
|
|
|
foo := []byte("!#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~")
|
|
|
|
encMess, err := AsymEncrypt(kp.Public, foo)
|
2022-09-02 12:07:21 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
2022-09-18 21:17:28 +02:00
|
|
|
println("len enc:", len(encMess))
|
|
|
|
decMess, err2 := AsymDecrypt(kp.Private, encMess)
|
2022-09-02 12:07:21 +02:00
|
|
|
if err2 != nil {
|
|
|
|
log.Println(err2.Error())
|
|
|
|
}
|
|
|
|
assert.Equal(t, foo, decMess, "The two messages should be the same.")
|
|
|
|
}
|
|
|
|
|
2022-09-18 21:17:28 +02:00
|
|
|
func TestAsymEncryptDecryptSigned(t *testing.T) {
|
2022-09-02 12:07:21 +02:00
|
|
|
kp := NewKeyPair()
|
2022-09-18 21:17:28 +02:00
|
|
|
foo := "!#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~"
|
|
|
|
encMess, sign, err := AsymEncryptAndSign(kp.Public, kp.Private, []byte(foo))
|
2022-09-02 12:07:21 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
2022-09-18 21:17:28 +02:00
|
|
|
decMess, err2 := AsymDecryptAndCheck(kp.Private, kp.Public, encMess, sign)
|
2022-09-02 12:07:21 +02:00
|
|
|
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
|
|
|
}
|