package meowlib import ( "encoding/base64" "fmt" "log" "os" "testing" "github.com/stretchr/testify/assert" ) 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-----` 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)) println(len([]byte(pubkey))) binpubkey, _ := key.GetPublicKey() println(len(binpubkey)) 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.") } func TestAsymEncryptDecryptSigned2(t *testing.T) { kp := NewKeyPair() foo := "!#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~" encMess, sign, err := AsymEncryptAndSign2(kp.Public, kp.Private, []byte(foo)) if err != nil { log.Println(err.Error()) } decMess, err2 := AsymDecryptAndCheck2(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.") } 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)) } func TestFlutterDecode(t *testing.T) { pub, err := os.ReadFile("/home/yves/Documents/code/flutter/meowlib/pub.key") if err != nil { fmt.Println("Err") } priv, err := os.ReadFile("/home/yves/Documents/code/flutter/meowlib/priv.key") if err != nil { fmt.Println("Err") } msg, err := os.ReadFile("/home/yves/Documents/code/flutter/meowlib/msg.enc") if err != nil { fmt.Println("Err") } sign, err := os.ReadFile("/home/yves/Documents/code/flutter/meowlib/msg.sign") if err != nil { fmt.Println("Err") } decMsg, err := AsymDecrypt(base64.StdEncoding.EncodeToString(priv), msg) if err != nil { log.Println(err.Error()) } println(string(decMsg)) decMsg, err = AsymDecryptAndCheck2(base64.StdEncoding.EncodeToString(priv), base64.StdEncoding.EncodeToString(pub), msg, sign) if err != nil { log.Println(err.Error()) } println(string(decMsg)) println(base64.StdEncoding.EncodeToString(pub)) } func TestFlutterEncode(t *testing.T) { pub, err := os.ReadFile("/home/yves/Documents/code/flutter/meowlib/pub.key") if err != nil { fmt.Println("Err") } priv, err := os.ReadFile("/home/yves/Documents/code/flutter/meowlib/priv.key") if err != nil { fmt.Println("Err") } encMess, sign2, err := AsymEncryptAndSign2(base64.StdEncoding.EncodeToString(pub), base64.StdEncoding.EncodeToString(priv), []byte("Hello")) if err != nil { log.Println(err.Error()) } err = os.WriteFile("/home/yves/Documents/code/flutter/meowlib/gomsg.enc", encMess, 0644) if err != nil { fmt.Println("Err") } err = os.WriteFile("/home/yves/Documents/code/flutter/meowlib/gomsg.sign", sign2, 0644) if err != nil { fmt.Println("Err") } println(base64.StdEncoding.EncodeToString(pub)) }