@@ -14,10 +14,14 @@ An implementation of the W3C Web Cryptography API specification (https://www.w3.
14
14
- [ Getting started] ( #getting-started )
15
15
- [ Algorithms] ( #algorithms )
16
16
- [ECDSA](#ecdsa)
17
+ - [Parameter Definitions](#parameter-definitions)
18
+ - [Examples](#examples)
17
19
- [HMAC](#hmac)
18
20
- [RSA-OAEP](#rsa-oaep)
19
21
- [SHA](#sha)
20
22
- [ Contributing] ( #contributing )
23
+ - [ Appendix] ( #appendix )
24
+ - [Hash Algorithms](#hash-algorithms)
21
25
22
26
## Background
23
27
@@ -46,10 +50,47 @@ This library is still in active development and all algorithms are not yet suppo
46
50
47
51
## Algorithms
48
52
53
+ When passing algorithms params into subtle functions, we use the ` webcrypto.Algorithm ` struct. It has the following properties:
54
+
55
+ | Field | Type | Description |
56
+ | :---- | :--- | :---------- |
57
+ | Name | ` string ` | The algorithm name. |
58
+ | Params | ` any ` | The algorithm parameters as defined by the parameters described by that algorithm in the WebCrypto specification. |
59
+
60
+ For each algorithm and function described below, listed are the appropriate algorithm params that need to be passed in.
61
+
49
62
### ECDSA
50
63
51
64
The ** ECDSA** algorithm is the implementation of operations described in [ §23] ( https://www.w3.org/TR/WebCryptoAPI/#ecdsa ) of the W3C specification.
52
65
66
+ #### Parameter Definitions
67
+
68
+ ##### Params
69
+
70
+ As specified in [ §23.1] ( https://www.w3.org/TR/WebCryptoAPI/#EcdsaParams-dictionary )
71
+
72
+ | Field | Type | Description |
73
+ | :---- | :--- | :---------- |
74
+ | Hash | ` string ` | The hash algorithm to use. See the supported [ hash algorithms] ( #hash-algorithms ) |
75
+
76
+ ##### KeyGenParams
77
+
78
+ As specified in [ §23.4] ( https://www.w3.org/TR/WebCryptoAPI/#EcKeyGenParams-dictionary )
79
+
80
+ | Field | Type | Description |
81
+ | :---- | :--- | :---------- |
82
+ | NamedCurve | ` string ` | A valid named curve. One of ` P-256 ` , ` P-384 ` , or ` P-521 ` . |
83
+
84
+ ##### KeyImportParams
85
+
86
+ As specified in [ §23.6] ( https://www.w3.org/TR/WebCryptoAPI/#EcKeyImportParams-dictionary )
87
+
88
+ | Field | Type | Description |
89
+ | :---- | :--- | :---------- |
90
+ | NamedCurve | ` string ` | A valid named curve. One of ` P-256 ` , ` P-384 ` , or ` P-521 ` . |
91
+
92
+ #### Examples
93
+
53
94
``` go
54
95
package main
55
96
@@ -59,28 +100,41 @@ import (
59
100
)
60
101
61
102
func main () {
62
- // generate a new ECDSA key
103
+ // generate a new P-256 ECDSA key
63
104
key , err := webcrypto.Subtle ().GenerateKey (
64
- &ecdsa.Algorithm {
105
+ &webcrypto.Algorithm {
106
+ Name: " ECDSA" ,
107
+ Params: &ecdsa.KeyGenParams {
65
108
NamedCurve: " P-256" ,
66
- }, true , webcrypto.Sign , webcrypto.Verify )
109
+ },
110
+ }, true , []webcrypto.KeyUsage {
111
+ webcrypto.Sign ,
112
+ webcrypto.Verify ,
113
+ })
67
114
if err != nil {
68
115
panic (err)
69
116
}
70
117
118
+ // key returned is a webcrypto.CryptoKeyPair
71
119
ckp := key.(webcrypto.CryptoKeyPair )
72
120
73
121
// sign some data with the private key
74
- sig , err := webcrypto.Subtle ().Sign (&ecdsa.Algorithm {
75
- Hash: " SHA-256" ,
122
+ sig , err := webcrypto.Subtle ().Sign (&webcrypto.Algorithm {
123
+ Name: " ECDSA" ,
124
+ Params: &ecdsa.Params {
125
+ Hash: " SHA-256" ,
126
+ },
76
127
}, ckp.PrivateKey (), []byte (" test" ))
77
128
if err != nil {
78
129
panic (err)
79
130
}
80
131
81
132
// verify the signature with the public key
82
- ok , err := webcrypto.Subtle ().Verify (&ecdsa.Algorithm {
83
- Hash: " SHA-256" ,
133
+ ok , err := webcrypto.Subtle ().Verify (&webcrypto.Algorithm {
134
+ Name: " ECDSA" ,
135
+ Params: &ecdsa.Params {
136
+ Hash: " SHA-256" ,
137
+ }
84
138
}, ckp.PublicKey (), sig, []byte (" test" ))
85
139
if err != nil {
86
140
panic (err)
@@ -89,6 +143,31 @@ func main() {
89
143
if !ok {
90
144
// didn't verify - do something
91
145
}
146
+
147
+ // export the public/private key as webcrypto.JsonWebKey
148
+ out , err := webcrypto.Subtle ().ExportKey (webcrypto.JWK , ckp.PrivateKey ())
149
+ if err != nil {
150
+ panic (err)
151
+ }
152
+
153
+ jwk := out.(webcrypto.JsonWebKey )
154
+
155
+ // do something with jwk
156
+
157
+ // import a public/private key and return webcrypto.CryptoKey
158
+ ck , err := webcrypto.Subtle ().ImportKey (webcrypto.JWK , jwk, &webcrypto.Algorithm {
159
+ Name: " ECDSA" ,
160
+ Params: &ecdsa.KeyImportParams {
161
+ NamedCurve: " P-256" ,
162
+ },
163
+ }, true , []webcrypto.KeyUsages {
164
+ webcrypto.Sign ,
165
+ })
166
+ if err != nil {
167
+ panic (err)
168
+ }
169
+
170
+ // do something with the imported key
92
171
}
93
172
```
94
173
@@ -229,7 +308,16 @@ func main() {
229
308
}
230
309
```
231
310
232
-
233
311
## Contributing
234
312
235
- If you have found a bug or would like to see new features, please create a new issue in this repository. If there is an issue that poses a security risk, please refrain from posting the issue publicly and contact
[ [email protected] ] ( mailto://[email protected] ) instead.
313
+ If you have found a bug or would like to see new features, please create a new issue in this repository. If there is an issue that poses a security risk, please refrain from posting the issue publicly and contact
[ [email protected] ] ( mailto://[email protected] ) instead.
314
+
315
+ ## Apendix
316
+
317
+ ### Hash Algorithms
318
+
319
+ Unless otherwise specified by a particular algorithm, the supported hash algorithms are
320
+ - ` SHA-1 `
321
+ - ` SHA-256 `
322
+ - ` SHA-384 `
323
+ - ` SHA-512 `
0 commit comments