@@ -65,7 +65,7 @@ See specific algorithms for the parameter types to be passed in.
65
65
66
66
The ** ECDSA** algorithm is the implementation of operations described in [ §23] ( https://www.w3.org/TR/WebCryptoAPI/#ecdsa ) of the W3C specification.
67
67
68
- ` import "github.com/armortal/webcrypto-go/algorithms/ecdsa" ` .
68
+ ` import "github.com/armortal/webcrypto-go/algorithms/ecdsa" `
69
69
70
70
#### Parameter Definitions
71
71
@@ -102,6 +102,8 @@ As specified in [§23.6](https://www.w3.org/TR/WebCryptoAPI/#EcKeyImportParams-d
102
102
package main
103
103
104
104
import (
105
+ " fmt"
106
+
105
107
" github.com/armortal/webcrypto-go"
106
108
" github.com/armortal/webcrypto-go/algorithms/ecdsa"
107
109
)
@@ -123,15 +125,15 @@ func main() {
123
125
}
124
126
125
127
// key returned is a webcrypto.CryptoKeyPair
126
- ckp := key.(webcrypto.CryptoKeyPair )
128
+ cryptoKey := key.(webcrypto.CryptoKeyPair )
127
129
128
130
// sign some data with the private key
129
131
sig , err := webcrypto.Subtle ().Sign (&webcrypto.Algorithm {
130
132
Name: " ECDSA" ,
131
133
Params: &ecdsa.Params {
132
134
Hash: " SHA-256" ,
133
135
},
134
- }, ckp .PrivateKey (), []byte (" test" ))
136
+ }, cryptoKey .PrivateKey (), []byte (" test" ))
135
137
if err != nil {
136
138
panic (err)
137
139
}
@@ -142,27 +144,48 @@ func main() {
142
144
Params: &ecdsa.Params {
143
145
Hash: " SHA-256" ,
144
146
},
145
- }, ckp .PublicKey (), sig, []byte (" test" ))
147
+ }, cryptoKey .PublicKey (), sig, []byte (" test" ))
146
148
if err != nil {
147
149
panic (err)
148
150
}
149
151
150
152
if !ok {
151
- // didn't verify - do something
153
+ panic ( " signature didn't verify" )
152
154
}
153
155
154
156
// export the public/private key as webcrypto.JsonWebKey
155
- out , err := webcrypto.Subtle ().ExportKey (webcrypto.Jwk , ckp .PrivateKey ())
157
+ out , err := webcrypto.Subtle ().ExportKey (webcrypto.Jwk , cryptoKey .PrivateKey ())
156
158
if err != nil {
157
159
panic (err)
158
160
}
159
161
160
- jwk := out.(webcrypto.JsonWebKey )
161
-
162
162
// do something with jwk
163
+ jwk := out.(*webcrypto.JsonWebKey )
164
+
165
+ // export the key as PKCS8
166
+ out, err = webcrypto.Subtle ().ExportKey (webcrypto.PKCS8 , cryptoKey.PrivateKey ())
167
+ if err != nil {
168
+ panic (err)
169
+ }
170
+
171
+ // do something with the pkcs8 key
172
+ pkcs8 := out.([]byte )
173
+
174
+ // import a public/private key from a jwk
175
+ in , err := webcrypto.Subtle ().ImportKey (webcrypto.Jwk , jwk, &webcrypto.Algorithm {
176
+ Name: " ECDSA" ,
177
+ Params: &ecdsa.KeyImportParams {
178
+ NamedCurve: " P-256" ,
179
+ },
180
+ }, true , []webcrypto.KeyUsage {
181
+ webcrypto.Sign ,
182
+ })
183
+ if err != nil {
184
+ panic (err)
185
+ }
163
186
164
- // import a public/private key
165
- ck , err : = webcrypto.Subtle ().ImportKey (webcrypto.Jwk , jwk , &webcrypto.Algorithm {
187
+ // import a public/private key from PKCS8
188
+ in , err = webcrypto.Subtle ().ImportKey (webcrypto.PKCS8 , pkcs8 , &webcrypto.Algorithm {
166
189
Name: " ECDSA" ,
167
190
Params: &ecdsa.KeyImportParams {
168
191
NamedCurve: " P-256" ,
@@ -175,14 +198,15 @@ func main() {
175
198
}
176
199
177
200
// do something with the imported webcrypto.CryptoKey
201
+ fmt.Println (in.Type ())
178
202
}
179
203
```
180
204
181
205
### HMAC
182
206
183
207
The ** HMAC** algorithm is the implementation of operations described in [ §29] ( https://www.w3.org/TR/WebCryptoAPI/#hmac ) of the W3C specification.
184
208
185
- ` import "github.com/armortal/webcrypto-go/algorithms/hmac" ` .
209
+ ` import "github.com/armortal/webcrypto-go/algorithms/hmac" `
186
210
187
211
#### Parameter Definitions
188
212
0 commit comments