-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryption_test.go
112 lines (90 loc) · 2.11 KB
/
encryption_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package crypto
import (
"fmt"
"math/rand"
"testing"
"gopkg.in/square/go-jose.v1"
)
var (
IPFSSTRING = []byte("/ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG")
)
func TestEncrypter_Encrypt_Multi(t *testing.T) {
// initialize encrypter
enc, err := NewEncrypter()
if err != nil {
t.Error(err)
}
// initialize keys and add them as recipients
var keys [3]*jose.JsonWebKey
for i := 0; i < 3; i++ {
keys[i], err = NewKey()
if err != nil {
t.Fatal(err)
}
jp, _ := keys[i].MarshalJSON()
fmt.Println(string(jp))
pk, err := NewPublicKey(keys[i])
if err != nil {
t.Fatal(err)
}
j, _ := pk.MarshalJSON()
fmt.Println(string(j))
err = enc.AddRecipient(pk)
if err != nil {
t.Fatal(err)
}
}
// encrypt message
msg, err := enc.Encrypt(IPFSSTRING)
if err != nil {
panic(err)
}
fmt.Println(msg.FullSerialize())
// select random recipient key and try to decrypt
k := keys[rand.Intn(3)]
_, _, m, err := msg.DecryptMulti(k)
if err != nil {
t.Fatal(err)
}
if string(m) != string(IPFSSTRING) {
t.Errorf("Decrypted message not the same as input message: %s != %s", string(m), string(IPFSSTRING))
}
}
func TestSymetricEncrypter_Encrypt(t *testing.T) {
// create random symetric key
symkey := NewSymmetricKey(SYMKEYALG)
// initialize encrypter with key
s, err := NewSymmetricEncrypter(symkey)
if err != nil {
panic(err)
}
// encrypt message
smsg, err := s.Encrypt(IPFSSTRING)
if err != nil {
panic(err)
}
// decrypt message
sdecrypted, err := smsg.Decrypt(symkey)
if string(sdecrypted) != string(IPFSSTRING) {
t.Errorf("Decrypted message not the same as input message: %s != %s", string(sdecrypted), string(IPFSSTRING))
}
}
func TestSymetricEncrypter_EncryptCompact(t *testing.T) {
// create random symetric key
symkey := NewSymmetricKey(SYMKEYALG)
// initialize encrypter with key
s, err := NewSymmetricEncrypter(symkey)
if err != nil {
panic(err)
}
// encrypt message
smsg, err := s.Encrypt(IPFSSTRING)
if err != nil {
panic(err)
}
m, err := smsg.CompactSerialize()
if err != nil {
t.Fatal(err)
}
fmt.Println("Compact format encrypted string:", m)
}