-
Notifications
You must be signed in to change notification settings - Fork 0
/
PKCipher_test.go
97 lines (69 loc) · 2.16 KB
/
PKCipher_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
package hippo
import (
"encoding/json"
"github.com/stretchr/testify/require"
"testing"
)
func test_pkcipher_basic(t *testing.T, algorithm string) {
data := []byte("Four score and seven years ago")
keys, err := GeneratePKCipher(algorithm)
require.Nil(t, err)
require.NotNil(t, keys)
ciphertext, err := keys.Encrypt(data)
require.Nil(t, err)
require.NotNil(t, ciphertext)
cleartext, err := keys.Decrypt(ciphertext)
require.Nil(t, err)
require.NotNil(t, cleartext)
require.Equal(t, cleartext, data)
}
func test_pkcipher_json_public(t *testing.T, algorithm string) {
data := []byte("Four score and seven years ago")
// Create a keypair
keys, err := GeneratePKCipher(algorithm)
require.Nil(t, err)
require.NotNil(t, keys)
// Marshal the public key out to JSON
publicjson, err := json.Marshal(keys.PublicKey())
require.Nil(t, err)
require.NotNil(t, publicjson)
// Read the public key back in
public := PublicKey{}
err = json.Unmarshal(publicjson, &public)
require.Nil(t, err)
encrypter, err := NewEncrypter(public)
require.Nil(t, err)
require.NotNil(t, encrypter)
// Encrypt the data from the unmarshaled public key
ciphertext, err := encrypter.Encrypt(data)
require.Nil(t, err)
require.NotNil(t, ciphertext)
// Decrypt the data with the original private key
cleartext, err := keys.Decrypt(ciphertext)
require.Nil(t, err)
require.NotNil(t, cleartext)
require.Equal(t, cleartext, data)
}
func test_pkcipher_json_private(t *testing.T, algorithm string) {
data := []byte("Four score and seven years ago")
keys, err := GeneratePKCipher(algorithm)
require.Nil(t, err)
require.NotNil(t, keys)
ciphertext, err := keys.Encrypt(data)
require.Nil(t, err)
require.NotNil(t, ciphertext)
privatejson, err := json.Marshal(keys.PrivateKey())
require.Nil(t, err)
require.NotNil(t, privatejson)
t.Log("Private Key", string(privatejson))
private := PrivateKey{}
err = json.Unmarshal(privatejson, &private)
require.Nil(t, err)
decrypter, err := NewDecrypter(private)
require.Nil(t, err)
require.NotNil(t, decrypter)
cleartext, err := decrypter.Decrypt(ciphertext)
require.Nil(t, err)
require.NotNil(t, cleartext)
require.Equal(t, cleartext, data)
}