go-pgp/pgp/encrypt_test.go
2024-07-25 16:31:58 +02:00

107 lines
4.5 KiB
Go

package pgp
import (
"fmt"
"testing"
"golang.org/x/crypto/openpgp"
)
func TestEncrypt(t *testing.T) {
fmt.Println("Encrypt test: START")
pubEntity, err := GetEntity([]byte(testPublicKey), []byte{})
if err != nil {
t.Error(fmt.Errorf("error getting entity: %v", err))
}
fmt.Println("Created public key entity.")
var encrypted []byte
encrypted, err = Encrypt(pubEntity, []byte(testMessage))
if err != nil {
t.Error(err)
}
fmt.Println("Encrypted test message with public key entity.")
var privateEntity *openpgp.Entity
privateEntity, err = GetEntity([]byte(testPublicKey), []byte(testPrivateKey))
if err != nil {
t.Error(fmt.Errorf("error getting entity: %v", err))
}
fmt.Println("Created private key entity.")
var decrypted []byte
decrypted, err = Decrypt(privateEntity, encrypted)
if err != nil {
t.Error(err)
}
fmt.Println("Decrypted message with private key entity.")
decryptedMessage := string(decrypted)
if decryptedMessage != testMessage {
t.Error(fmt.Errorf("decrypted message does not equal original"))
}
fmt.Println("Decrypted message equals original message.")
fmt.Println("Encrypt test: END")
}
const (
testMessage = "hello world"
testPrivateKey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
xcZYBFkNK+ABEADUpjJ/kz3j+iz9qnzUb6ONw+WHSLp8umnd1z06SBVkWFjYReqf
oPCOq67XDseK71ZSevrIt7EdTLAzl0xN8kB+8iedAGM5OCakDe3R8L83OGy1Em26
PbrrYs3TYKGDXW65TsGYCoETROGgU2zPvuBDU1RvVvd9vAlWHQis43BOWaaakCEc
00V3sdNcfV+lz7fNUXEgtmTCCr9NWX4gO3YeenIIxep4WD27VwscW5Q2B1cnxcFL
+TZzE2oVjtXljGSO94XsekuNU47zwJZNGyU6SSlSZ+KVXuSdkRRfNYHlgDWg5b8C
xVmdVUfsx3bmNlOlXoyETj83xvRlLxn3PYIgOz6OlYGba5oDogK2QLXGTXK1o9OE
kgoghmCNQqxocvb1hQXT8cEynIbAdc6/JknYaoic6ka1iTTz3uN8FE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SevrIt7EdTLAzl0xN8kB+8iedAGM5OCakDe3R8L83OGy1Em26
PbrrYs3TYKGDXW65TsGYCoETROGgU2zPvuBDU1RvVvd9vAlWHQis43BOWaaakCEc
00V3sdNcfV+lz7fNUXEgtmTCCr9NWX4gO3YeenIIxep4WD27VwscW5Q2B1cnxcFL
+TZzE2oVjtXljGSO94XsekuNU47zwJZNGyU6SSlSZ+KVXuSdkRRfNYHlgDWg5b8C
xVmdVUfsx3bmNlOlXoyETj83xvRlLxn3PYIgOz6OlYGba5oDogK2QLXGTXK1o9OE
kgoghmCNQqxocvb1hQXT8cEynIbAdc6/JknYaoic6ka1iTTz3uN8FEPw5gRlidcQ
3wkbmqIS0LJs3JmVbD7/BxMY1dwqMyvulfnLiTsWSPvk41o7dHf077t23V9w78Jg
h4Xq4HRvt37PtuO6eWW3c5aUIWmvvDqMbMEqp2y23noYoVNqEpVoHolDdoCSurv/
XxbNBnj46XwaIs6OlrO2htV0al2/WVTNnSLxCyoHXoJEDXyaOyNKn1jM/FczgYQJ
069uC804ohOfjLmbtUEYE7Hjeo5utPm2ryjnakgV5AStKgL0SyFZUwN/DwARAQAB
=gO1a
-----END PGP PUBLIC KEY BLOCK-----`
)