1
16
17 package jose
18
19 import (
20 "crypto/ecdsa"
21 "crypto/rand"
22 "crypto/rsa"
23 "fmt"
24 )
25
26
27 var encrypter Encrypter
28
29 func Example_jWE() {
30
31 privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
32 if err != nil {
33 panic(err)
34 }
35
36
37
38 publicKey := &privateKey.PublicKey
39 encrypter, err := NewEncrypter(A128GCM, Recipient{Algorithm: RSA_OAEP, Key: publicKey}, nil)
40 if err != nil {
41 panic(err)
42 }
43
44
45
46
47 var plaintext = []byte("Lorem ipsum dolor sit amet")
48 object, err := encrypter.Encrypt(plaintext)
49 if err != nil {
50 panic(err)
51 }
52
53
54
55
56 serialized := object.FullSerialize()
57
58
59
60 object, err = ParseEncrypted(serialized)
61 if err != nil {
62 panic(err)
63 }
64
65
66
67
68 decrypted, err := object.Decrypt(privateKey)
69 if err != nil {
70 panic(err)
71 }
72
73 fmt.Printf(string(decrypted))
74
75 }
76
77 func Example_jWS() {
78
79 privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
80 if err != nil {
81 panic(err)
82 }
83
84
85 signer, err := NewSigner(SigningKey{Algorithm: PS512, Key: privateKey}, nil)
86 if err != nil {
87 panic(err)
88 }
89
90
91
92
93 var payload = []byte("Lorem ipsum dolor sit amet")
94 object, err := signer.Sign(payload)
95 if err != nil {
96 panic(err)
97 }
98
99
100
101
102 serialized := object.FullSerialize()
103
104
105
106 object, err = ParseSigned(serialized)
107 if err != nil {
108 panic(err)
109 }
110
111
112
113
114 output, err := object.Verify(&privateKey.PublicKey)
115 if err != nil {
116 panic(err)
117 }
118
119 fmt.Printf(string(output))
120
121 }
122
123 func ExampleNewEncrypter_publicKey() {
124 var publicKey *rsa.PublicKey
125
126
127 NewEncrypter(A128GCM, Recipient{Algorithm: RSA_OAEP, Key: publicKey}, nil)
128
129
130 NewEncrypter(A128CBC_HS256, Recipient{Algorithm: RSA1_5, Key: publicKey}, nil)
131 }
132
133 func ExampleNewEncrypter_symmetric() {
134 var sharedKey []byte
135
136
137 NewEncrypter(A128GCM, Recipient{Algorithm: A128GCMKW, Key: sharedKey}, nil)
138
139
140 NewEncrypter(A128GCM, Recipient{Algorithm: DIRECT, Key: sharedKey}, nil)
141 }
142
143 func ExampleNewSigner_publicKey() {
144 var rsaPrivateKey *rsa.PrivateKey
145 var ecdsaPrivateKey *ecdsa.PrivateKey
146
147
148 NewSigner(SigningKey{Algorithm: RS256, Key: rsaPrivateKey}, nil)
149
150
151 NewSigner(SigningKey{Algorithm: ES384, Key: ecdsaPrivateKey}, nil)
152 }
153
154 func ExampleNewSigner_symmetric() {
155 var sharedKey []byte
156
157
158 NewSigner(SigningKey{Algorithm: HS256, Key: sharedKey}, nil)
159
160
161 NewSigner(SigningKey{Algorithm: HS512, Key: sharedKey}, nil)
162 }
163
164 func ExampleNewMultiEncrypter() {
165 var publicKey *rsa.PublicKey
166 var sharedKey []byte
167
168
169 NewMultiEncrypter(A128GCM, []Recipient{
170 {Algorithm: A128GCMKW, Key: sharedKey},
171 {Algorithm: RSA_OAEP, Key: publicKey},
172 }, nil)
173 }
174
175 func ExampleNewMultiSigner() {
176 var privateKey *rsa.PrivateKey
177 var sharedKey []byte
178
179
180 NewMultiSigner([]SigningKey{
181 {Algorithm: HS256, Key: sharedKey},
182 {Algorithm: PS384, Key: privateKey},
183 }, nil)
184 }
185
186 func ExampleEncrypter_encrypt() {
187
188 var plaintext = []byte("This is a secret message")
189
190 encrypter.Encrypt(plaintext)
191 }
192
193 func ExampleEncrypter_encryptWithAuthData() {
194
195
196
197 var plaintext = []byte("This is a secret message")
198 var aad = []byte("This is authenticated, but public data")
199
200 encrypter.EncryptWithAuthData(plaintext, aad)
201 }
202
View as plain text