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"
|
2022-12-03 00:05:28 +01:00
|
|
|
"os"
|
2021-10-18 21:05:44 +02:00
|
|
|
"testing"
|
2022-09-02 12:07:21 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-10-18 21:05:44 +02:00
|
|
|
)
|
|
|
|
|
2022-10-29 20:07:35 +02:00
|
|
|
var privateKeyECC = `-----BEGIN PGP PRIVATE KEY BLOCK-----
|
|
|
|
Version: openpgp-mobile
|
|
|
|
|
|
|
|
xVgEY1j9rBYJKwYBBAHaRw8BAQdAk3jWTLSrUqL+vyeEv63DTLlp03IcMExucmFE
|
|
|
|
RG+IbZgAAQDgQazOcCioSeZWQ16Vn/TMYiAgXgsAlL5b5NZWgyTNzA/+zSFUZXN0
|
|
|
|
IChzYW1wbGUpIDxzYW1wbGVAc2FtcGxlLmNvbT7CiwQTFggAPQUCY1j9rAmQCUTp
|
|
|
|
H7mMc3UWIQSDHOWl4MeCw6GJdvkJROkfuYxzdQIbAwIeAQIZAQILBwIVCAMWAAIC
|
|
|
|
IgEAAKj5AP4x9KvZFpriLd2K97CaZs5Mzb4r4jeL/q0pMV6d6SvJ+QEAtrqfQovO
|
|
|
|
hEtadCopy1R3gepIdeX4Fh7tHNYi3pOO5wjHXQRjWP2sEgorBgEEAZdVAQUBAQdA
|
|
|
|
j48HXuKTfojSYLslNmAtCj6MkwFpj4TR2b5KkKD29kgDAQoJAAD/SulDoAyVe/VR
|
|
|
|
6dY5Xe887TZSOCKCk5KNTzBtfMj0nhAQzMJ4BBgWCAAqBQJjWP2sCZAJROkfuYxz
|
|
|
|
dRYhBIMc5aXgx4LDoYl2+QlE6R+5jHN1AhsMAAB0gAEAz9sGgXtTsfJiEMZhKNj8
|
|
|
|
XAbymYPQCrsmEBea1uqbB8UA/iKqk0cOabVYTzwBA53G0Tx0C67Xqy46mAtR+W4O
|
|
|
|
CfMK
|
|
|
|
=CJ/U
|
|
|
|
-----END PGP PRIVATE KEY BLOCK-----`
|
|
|
|
|
|
|
|
var publicKeyECC = `-----BEGIN PGP PUBLIC KEY BLOCK-----
|
|
|
|
Version: openpgp-mobile
|
|
|
|
|
|
|
|
xjMEY1j9rBYJKwYBBAHaRw8BAQdAk3jWTLSrUqL+vyeEv63DTLlp03IcMExucmFE
|
|
|
|
RG+IbZjNIVRlc3QgKHNhbXBsZSkgPHNhbXBsZUBzYW1wbGUuY29tPsKLBBMWCAA9
|
|
|
|
BQJjWP2sCZAJROkfuYxzdRYhBIMc5aXgx4LDoYl2+QlE6R+5jHN1AhsDAh4BAhkB
|
|
|
|
AgsHAhUIAxYAAgIiAQAAqPkA/jH0q9kWmuIt3Yr3sJpmzkzNviviN4v+rSkxXp3p
|
|
|
|
K8n5AQC2up9Ci86ES1p0KinLVHeB6kh15fgWHu0c1iLek47nCM44BGNY/awSCisG
|
|
|
|
AQQBl1UBBQEBB0CPjwde4pN+iNJguyU2YC0KPoyTAWmPhNHZvkqQoPb2SAMBCgnC
|
|
|
|
eAQYFggAKgUCY1j9rAmQCUTpH7mMc3UWIQSDHOWl4MeCw6GJdvkJROkfuYxzdQIb
|
|
|
|
DAAAdIABAM/bBoF7U7HyYhDGYSjY/FwG8pmD0Aq7JhAXmtbqmwfFAP4iqpNHDmm1
|
|
|
|
WE88AQOdxtE8dAuu16suOpgLUfluDgnzCg==
|
|
|
|
=oWIw
|
|
|
|
-----END PGP PUBLIC KEY BLOCK-----`
|
|
|
|
|
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-10-22 14:41:48 +02:00
|
|
|
println(len([]byte(pubkey)))
|
|
|
|
binpubkey, _ := key.GetPublicKey()
|
|
|
|
println(len(binpubkey))
|
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
|
|
|
}
|
2022-10-29 20:07:35 +02:00
|
|
|
|
2022-11-20 17:52:27 +01:00
|
|
|
func TestAsymEncryptDecryptSigned2(t *testing.T) {
|
|
|
|
kp := NewKeyPair()
|
|
|
|
foo := "!#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~"
|
2022-12-15 21:59:31 +01:00
|
|
|
encMess, sign, err := AsymEncryptAndSign(kp.Public, kp.Private, []byte(foo))
|
2022-11-20 17:52:27 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
2022-12-15 21:59:31 +01:00
|
|
|
decMess, err2 := AsymDecryptAndCheck(kp.Private, kp.Public, encMess, sign)
|
2022-11-20 17:52:27 +01:00
|
|
|
if err2 != nil {
|
|
|
|
log.Println(err2.Error())
|
|
|
|
}
|
|
|
|
assert.Equal(t, foo, string(decMess), "The two messages should be the same.")
|
|
|
|
}
|
|
|
|
|
2022-10-29 20:07:35 +02:00
|
|
|
func TestFlutterCompat(t *testing.T) {
|
|
|
|
pub := base64.StdEncoding.EncodeToString([]byte(publicKeyECC))
|
|
|
|
priv := base64.StdEncoding.EncodeToString([]byte(privateKeyECC))
|
|
|
|
|
|
|
|
encMess, sign, err := AsymEncryptAndSign(pub, priv, []byte("Hello"))
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
println(base64.StdEncoding.EncodeToString(encMess))
|
|
|
|
println(base64.StdEncoding.EncodeToString(sign))
|
2022-11-20 17:52:27 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFlutterDecode(t *testing.T) {
|
2022-12-03 00:05:28 +01:00
|
|
|
pub, err := os.ReadFile("/home/yves/Documents/code/flutter/meowlib/pub.key")
|
2022-11-20 17:52:27 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Err")
|
|
|
|
}
|
2022-12-03 00:05:28 +01:00
|
|
|
priv, err := os.ReadFile("/home/yves/Documents/code/flutter/meowlib/priv.key")
|
2022-11-20 17:52:27 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Err")
|
|
|
|
}
|
2022-12-03 00:05:28 +01:00
|
|
|
msg, err := os.ReadFile("/home/yves/Documents/code/flutter/meowlib/msg.enc")
|
2022-11-20 17:52:27 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Err")
|
|
|
|
}
|
2022-12-03 00:05:28 +01:00
|
|
|
sign, err := os.ReadFile("/home/yves/Documents/code/flutter/meowlib/msg.sign")
|
2022-11-20 17:52:27 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Err")
|
|
|
|
}
|
|
|
|
decMsg, err := AsymDecrypt(base64.StdEncoding.EncodeToString(priv), msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
|
|
|
println(string(decMsg))
|
2022-12-15 21:59:31 +01:00
|
|
|
decMsg, err = AsymDecryptAndCheck(base64.StdEncoding.EncodeToString(priv), base64.StdEncoding.EncodeToString(pub), msg, sign)
|
2022-11-20 17:52:27 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
|
|
|
println(string(decMsg))
|
|
|
|
println(base64.StdEncoding.EncodeToString(pub))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFlutterEncode(t *testing.T) {
|
2022-12-03 00:05:28 +01:00
|
|
|
pub, err := os.ReadFile("/home/yves/Documents/code/flutter/meowlib/pub.key")
|
2022-11-20 17:52:27 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Err")
|
|
|
|
}
|
2022-12-03 00:05:28 +01:00
|
|
|
priv, err := os.ReadFile("/home/yves/Documents/code/flutter/meowlib/priv.key")
|
2022-11-20 17:52:27 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Err")
|
|
|
|
}
|
|
|
|
|
2022-12-15 21:59:31 +01:00
|
|
|
encMess, sign2, err := AsymEncryptAndSign(base64.StdEncoding.EncodeToString(pub), base64.StdEncoding.EncodeToString(priv), []byte("Hello"))
|
2022-11-20 17:52:27 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
|
|
|
|
2022-12-03 00:05:28 +01:00
|
|
|
err = os.WriteFile("/home/yves/Documents/code/flutter/meowlib/gomsg.enc", encMess, 0644)
|
2022-11-20 17:52:27 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Err")
|
|
|
|
}
|
2022-12-03 00:05:28 +01:00
|
|
|
err = os.WriteFile("/home/yves/Documents/code/flutter/meowlib/gomsg.sign", sign2, 0644)
|
2022-11-20 17:52:27 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Err")
|
|
|
|
}
|
|
|
|
println(base64.StdEncoding.EncodeToString(pub))
|
|
|
|
|
2022-10-29 20:07:35 +02:00
|
|
|
}
|