-
Notifications
You must be signed in to change notification settings - Fork 2
/
crypto_openpgp_test.go
94 lines (82 loc) · 2.02 KB
/
crypto_openpgp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"bytes"
"crypto"
gpg "github.com/Geo25rey/crypto/openpgp"
"github.com/Geo25rey/crypto/openpgp/armor"
"github.com/Geo25rey/crypto/openpgp/packet"
"io"
"io/ioutil"
"strings"
"testing"
)
func TestSerialEncryption(t *testing.T) {
DefaultEncryptionConfig := func() *packet.Config {
return &packet.Config{
DefaultHash: crypto.SHA512,
DefaultCipher: packet.CipherAES256,
DefaultCompressionAlgo: packet.CompressionNone,
RSABits: 4096,
}
}
const MessageEncoding = "Message"
entity, _ := gpg.NewEntity("Name", "", "", DefaultEncryptionConfig())
// Remove Private Keys to share
buf := bytes.NewBuffer(make([]byte, 0))
err := entity.Serialize(buf)
publicEntity, err := gpg.ReadEntity(packet.NewReader(buf))
if err != nil {
t.Error(err)
t.FailNow()
}
messageSent := "Some message"
content := bytes.NewBuffer([]byte(messageSent))
println("Creating Message")
// Create an Encrypted Message to send
to := gpg.EntityList{publicEntity}
buf = bytes.NewBuffer(make([]byte, 0))
w2, err := armor.Encode(buf, MessageEncoding, make(map[string]string))
if err != nil {
t.Error(err)
t.FailNow()
}
w3, err := gpg.Encrypt(w2, to, nil, nil, DefaultEncryptionConfig())
if err != nil {
t.Error(err)
t.FailNow()
}
_, err = io.Copy(w3, content)
if err != nil {
t.Error(err)
t.FailNow()
}
err = w3.Close()
if err != nil {
t.Error(err)
t.FailNow()
}
err = w2.Close()
if err != nil {
t.Error(err)
t.FailNow()
}
decode, err := armor.Decode(buf)
if err != nil {
t.Error(err)
t.FailNow()
}
messageDetails, err := gpg.ReadMessage(decode.Body, gpg.EntityList{entity}, nil, DefaultEncryptionConfig())
if err != nil {
t.Error(err)
t.FailNow()
}
messageRead, err := ioutil.ReadAll(messageDetails.UnverifiedBody)
if err != nil {
t.Error(err)
t.FailNow()
}
if strings.Compare(messageSent, string(messageRead)) != 0 {
t.Errorf("Message sent isn't the same as message read\n'%s' != '%s'", messageSent, string(messageRead))
t.FailNow()
}
}