-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
194 lines (154 loc) · 4.08 KB
/
examples_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package hippo
import (
"bytes"
"encoding/json"
"fmt"
)
// Example_Sign is a minimal demonstration of generating a key,
// signing some data, sharing the key bytes, and verifying the data.
func Example_Sign() {
// GenerateCredentials a key
sender, err := GenerateCredentials("ed25519")
if err != nil {
panic(err)
}
// Sign some data with that key
data := []byte("Four score and seven years ago")
signature, err := sender.Sign(data)
if err != nil {
panic(err)
}
// Marshal the key to JSON to share it somehow
sharedkey, err := json.Marshal(sender.PublicKey())
if err != nil {
panic(err)
}
// Receive a shared key as JSON and unmarshal
publickey := PublicKey{}
err = json.Unmarshal(sharedkey, &publickey)
if err != nil {
panic(err)
}
// Turn the key into an actionable verifier (before this it's just
// data, the Verifier is an object with specific crypto methods)
verifier, err := NewVerifier(publickey)
if err != nil {
panic(err)
}
// Verify that this key did sign the data
err = verifier.Verify(data, signature)
if err != nil {
panic(err)
}
fmt.Printf("Verified")
// Output: Verified
}
// Example_CertificateChain shows a root CA generating a valid
// certificate for an intermediary CA, which generates a valid
// certificate for a secondary CA, which in turn generates a valid
// certificate for a user that is verified against a pool including
// just the root CA.
func Example_CertificateChain() {
// Create some keys
user, err := GenerateCredentials(AlgorithmEd25519)
if err != nil {
panic(err)
}
ca1, err := GenerateCredentials(AlgorithmECDSA_P256)
if err != nil {
panic(err)
}
ca2, err := GenerateCredentials(AlgorithmECDSA_P256)
if err != nil {
panic(err)
}
root, err := GenerateCredentials(AlgorithmEd25519)
if err != nil {
panic(err)
}
// Root makes a certificate for CA1
ca1_id := NewTestament("ca1", ca1.PublicKey(), Claims{"CertificateAuthority": true})
ca1_cert, err := ca1_id.Sign("root", root)
if err != nil {
panic(err)
}
// CA1 makes a certificate for CA2
ca2_id := NewTestament("ca2", ca2.PublicKey(), Claims{"CertificateAuthority": true})
ca2_cert, err := ca2_id.Sign("ca1", ca1)
if err != nil {
panic(err)
}
// CA2 makes a certificate for the user
user_id := NewTestament("Joe", user.PublicKey(), nil)
user_cert, err := user_id.Sign("ca2", ca2)
if err != nil {
panic(err)
}
// Put them together to make a certificate
out_certificate := &Certificate{Chain{user_cert, ca2_cert, ca1_cert}}
// Marshal to JSON to share the certificate somehow
bytes, err := out_certificate.ToBytes()
if err != nil {
panic(err)
}
// Unmarshal the shared certificate
var in_certificate Certificate
err = json.Unmarshal(bytes, &in_certificate)
if err != nil {
panic(err)
}
// Create a pool comprised of the root
pool := NewVerifierPool()
err = pool.Add("root", root)
if err != nil {
panic(err)
}
// Verify the certificate against the pool
err = pool.Verify(&in_certificate)
if err != nil {
panic(err)
}
fmt.Println("Verified")
// Output: Verified
}
// Example_Encrypt is a minimal demonstration of generating a key,
// marshaling it to JSON, encrypting data with the unmarshalled key,
// and then decrypting the data from the original key.
func Example_Encrypt() {
data := []byte("Four score and seven years ago")
// Create a keypair
keys, err := GeneratePKCipher("rsa-oaep-2048")
if err != nil {
panic(err)
}
// Marshal the public key out to JSON
publicjson, err := json.Marshal(keys.PublicKey())
if err != nil {
panic(err)
}
// Read the public key back in
public := PublicKey{}
err = json.Unmarshal(publicjson, &public)
if err != nil {
panic(err)
}
encrypter, err := NewEncrypter(public)
if err != nil {
panic(err)
}
// Encrypt the data from the unmarshaled public key
ciphertext, err := encrypter.Encrypt(data)
if err != nil {
panic(err)
}
// Decrypt the data with the original private key
cleartext, err := keys.Decrypt(ciphertext)
if err != nil {
panic(err)
}
if bytes.Compare(cleartext, data) != 0 {
panic("Data mismatch!")
}
fmt.Println("Received")
// Output: Received
}