@@ -20,6 +20,7 @@ An implementation of the W3C Web Cryptography API specification (https://www.w3.
20
20
- [Parameter Definitions](#parameter-definitions-1)
21
21
- [Examples](#examples-1)
22
22
- [RSA-OAEP](#rsa-oaep)
23
+ - [RSASSA-PKCS1-v1_5]()
23
24
- [SHA](#sha)
24
25
- [ Contributing] ( #contributing )
25
26
- [ Appendix] ( #appendix )
@@ -124,16 +125,16 @@ func main() {
124
125
panic (err)
125
126
}
126
127
127
- // key returned is a webcrypto.CryptoKeyPair
128
- cryptoKey := key.(webcrypto.CryptoKeyPair )
128
+ // key returned is a webcrypto.CryptoKeyPair that contains two *ecdsa.CryptoKey
129
+ cryptoKeyPair := key.(webcrypto.CryptoKeyPair )
129
130
130
131
// sign some data with the private key
131
132
sig , err := webcrypto.Subtle ().Sign (&webcrypto.Algorithm {
132
133
Name: " ECDSA" ,
133
134
Params: &ecdsa.Params {
134
135
Hash: " SHA-256" ,
135
136
},
136
- }, cryptoKey .PrivateKey (), []byte (" test" ))
137
+ }, cryptoKeyPair .PrivateKey (), []byte (" test" ))
137
138
if err != nil {
138
139
panic (err)
139
140
}
@@ -144,7 +145,7 @@ func main() {
144
145
Params: &ecdsa.Params {
145
146
Hash: " SHA-256" ,
146
147
},
147
- }, cryptoKey .PublicKey (), sig, []byte (" test" ))
148
+ }, cryptoKeyPair .PublicKey (), sig, []byte (" test" ))
148
149
if err != nil {
149
150
panic (err)
150
151
}
@@ -154,7 +155,7 @@ func main() {
154
155
}
155
156
156
157
// export the public/private key as webcrypto.JsonWebKey
157
- out , err := webcrypto.Subtle ().ExportKey (webcrypto.Jwk , cryptoKey .PrivateKey ())
158
+ out , err := webcrypto.Subtle ().ExportKey (webcrypto.Jwk , cryptoKeyPair .PrivateKey ())
158
159
if err != nil {
159
160
panic (err)
160
161
}
@@ -163,7 +164,7 @@ func main() {
163
164
jwk := out.(*webcrypto.JsonWebKey )
164
165
165
166
// export the key as PKCS8
166
- out, err = webcrypto.Subtle ().ExportKey (webcrypto.PKCS8 , cryptoKey .PrivateKey ())
167
+ out, err = webcrypto.Subtle ().ExportKey (webcrypto.PKCS8 , cryptoKeyPair .PrivateKey ())
167
168
if err != nil {
168
169
panic (err)
169
170
}
@@ -345,39 +346,145 @@ func main() {
345
346
346
347
The ** RSA-OAEP** algorithm is the implementation of operations described in [ §22] ( https://www.w3.org/TR/WebCryptoAPI/#rsa-oaep ) of the W3C specification.
347
348
349
+ ` import "github.com/armortal/webcrypto-go/algorithms/rsa" `
350
+
351
+ #### Parameter Definitions
352
+
353
+ Below are the parameters that supported RSA-OAEP operations will take according to
354
+ [ §22.2] ( https://www.w3.org/TR/WebCryptoAPI/#rsa-oaep-registration ) .
355
+
356
+ #### OaepParams
357
+
358
+ As specified in [ §22.3] ( https://www.w3.org/TR/WebCryptoAPI/#rsa-oaep-params )
359
+
360
+ | Field | Type | Description |
361
+ | :---- | :--- | :---------- |
362
+ | Label | ` string ` | The optional label/application data to associate with the message. |
363
+
364
+ #### Examples
365
+
348
366
``` go
349
367
package main
350
368
351
369
import (
370
+ " fmt"
371
+ " math/big"
372
+
352
373
" github.com/armortal/webcrypto-go"
353
374
" github.com/armortal/webcrypto-go/algorithms/rsa"
354
375
)
355
376
356
377
func main () {
357
- // generateKey
378
+ // generate a new key
358
379
key , err := webcrypto.Subtle ().GenerateKey (
359
- &rsa .Algorithm {
380
+ &webcrypto .Algorithm {
360
381
Name: " RSA-OAEP" ,
361
- HashedKeyGenParams : &rsa.HashedKeyGenParams {
382
+ Params : &rsa.HashedKeyGenParams {
362
383
KeyGenParams: rsa.KeyGenParams {
363
- ModulusLength: 2048 ,
364
- PublicExponent: *big.NewInt (65537 ),
384
+ ModulusLength: 2048 ,
385
+ PublicExponent: *big.NewInt (65537 ),
365
386
},
366
387
Hash: " SHA-256" ,
367
388
},
368
-
369
- }, true , webcrypto.Decrypt , webcrypto.Encrypt )
389
+ }, true , []webcrypto.KeyUsage {webcrypto.Decrypt , webcrypto.Encrypt })
370
390
371
391
if err != nil {
372
392
panic (err)
373
393
}
374
394
375
395
cryptoKeyPair := key.(webcrypto.CryptoKeyPair )
376
396
377
- // do something with cryptoKeyPair
397
+ // encrypt some data with an optional label
398
+ encrypted , err := webcrypto.Subtle ().Encrypt (&webcrypto.Algorithm {
399
+ Name: " RSA-OAEP" ,
400
+ Params: &rsa.OaepParams {
401
+ Label: []byte (" optional" ),
402
+ },
403
+ }, cryptoKeyPair.PublicKey (), []byte (" test" ))
404
+
405
+ if err != nil {
406
+ panic (err)
407
+ }
408
+
409
+ // decrypt the data
410
+ decrypted , err := webcrypto.Subtle ().Decrypt (&webcrypto.Algorithm {
411
+ Name: " RSA-OAEP" ,
412
+ Params: &rsa.OaepParams {
413
+ Label: []byte (" optional" ),
414
+ },
415
+ }, cryptoKeyPair.PrivateKey (), encrypted)
416
+
417
+ if err != nil {
418
+ panic (err)
419
+ }
420
+
421
+ // do something with decrypted data
422
+ fmt.Println (string (decrypted))
423
+
424
+ // export the private/public key as jwk
425
+ out , err := webcrypto.Subtle ().ExportKey (webcrypto.Jwk , cryptoKeyPair.PrivateKey ())
426
+ if err != nil {
427
+ panic (err)
428
+ }
429
+
430
+ // do something with jwk
431
+ jwk := out.(*webcrypto.JsonWebKey )
432
+
433
+ // import a key from jwk
434
+ in , err := webcrypto.Subtle ().ImportKey (webcrypto.Jwk , jwk, &webcrypto.Algorithm {
435
+ Name: " RSA-OAEP" ,
436
+ Params: &rsa.HashedImportParams {
437
+ Hash: " SHA-256" ,
438
+ },
439
+ }, true , []webcrypto.KeyUsage {webcrypto.Decrypt })
440
+
441
+ if err != nil {
442
+ panic (err)
443
+ }
444
+
445
+ // do something with the imported key
446
+ fmt.Println (in.Type ())
378
447
}
379
448
```
380
449
450
+ ### RSASSA-PKCS1-v1_5
451
+
452
+ This algorithm is currently not supported. However, parameter definitions for those used in [ RSA-OAEP] ( #rsa-oaep ) operations
453
+ come from those defined in this algorithm.
454
+
455
+ #### Parameter Definitions
456
+
457
+ Below are the parameters that supported RSASSA-PKCS1-v1_5 operations will take according to
458
+ [ §20.2] ( https://www.w3.org/TR/WebCryptoAPI/#rsassa-pkcs1-registration ) .
459
+
460
+ ##### KeyGenParams
461
+
462
+ As specified in [ §20.3] ( https://www.w3.org/TR/WebCryptoAPI/#RsaKeyGenParams-dictionary )
463
+
464
+ | Field | Type | Description |
465
+ | :---- | :--- | :---------- |
466
+ | ModulusLength | ` uint64 ` | The length, in bits, of the RSA modulus. |
467
+ | PublicExponent | ` *big.Int ` | The RSA public exponent. |
468
+
469
+ ##### HashedKeyGenParams
470
+
471
+ As specified in [ §20.4] ( https://www.w3.org/TR/WebCryptoAPI/#RsaHashedKeyGenParams-dictionary )
472
+
473
+ | Field | Type | Description |
474
+ | :---- | :--- | :---------- |
475
+ | Hash | ` string ` | The [ hash algorithm] ( #hash-algorithms ) to use. |
476
+ | ModulusLength | ` uint64 ` | The length, in bits, of the RSA modulus. |
477
+ | PublicExponent | ` *big.Int ` | The RSA public exponent. |
478
+
479
+ ##### HashedImportParams
480
+
481
+ As specified in [ §20.7] ( https://www.w3.org/TR/WebCryptoAPI/#RsaHashedImportParams-dictionary )
482
+
483
+ | Field | Type | Description |
484
+ | :---- | :--- | :---------- |
485
+ | Hash | ` string ` | The [ hash algorithm] ( #hash-algorithms ) to use. |
486
+
487
+
381
488
## SHA
382
489
383
490
The ** SHA** algorithm is the implementation of operations described in [ §30] ( https://www.w3.org/TR/WebCryptoAPI/#sha ) of the W3C specification.
0 commit comments