Skip to content

Commit bd4922a

Browse files
committed
refactor(#29): updated readme, added SHA examples
1 parent 8d8b644 commit bd4922a

File tree

3 files changed

+76
-25
lines changed

3 files changed

+76
-25
lines changed

README.md

+47-25
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ An implementation of the W3C Web Cryptography API specification (https://www.w3.
2020
- [Parameter Definitions](#parameter-definitions-1)
2121
- [Examples](#examples-1)
2222
- [RSA-OAEP](#rsa-oaep)
23-
- [RSASSA-PKCS1-v1_5]()
23+
- [Parameter Definitions](#parameter-definitions-2)
24+
- [Examples](#examples-2)
25+
- [RSASSA-PKCS1-v1_5](#rsassa-pkcs1-v1_5)
26+
- [Parameter Definitions](#parameter-definitions-3)
2427
- [SHA](#sha)
28+
- [Parameter Definitions](#parameter-definitions-4)
29+
- [Examples](#examples-3)
2530
- [Contributing](#contributing)
26-
- [Appendix](#appendix)
27-
- [Hash Algorithms](#hash-algorithms)
2831

2932
## Background
3033

@@ -79,7 +82,7 @@ As specified in [§23.3](https://www.w3.org/TR/WebCryptoAPI/#EcdsaParams-diction
7982

8083
| Field | Type | Description |
8184
| :---- | :--- | :---------- |
82-
| Hash | `string` | The hash algorithm to use. See the supported [hash algorithms](#hash-algorithms) |
85+
| Hash | `string` | The hash algorithm to use. See the supported [hash algorithms](#parameter-definitions-4) |
8386

8487
##### KeyGenParams
8588

@@ -220,7 +223,7 @@ As specified in [§29.5](https://www.w3.org/TR/WebCryptoAPI/#hmac-keygen-params)
220223

221224
| Field | Type | Description |
222225
| :---- | :--- | :---------- |
223-
| Hash | `string` | The inner hash function to use. See the supported [hash algorithms](#hash-algorithms). |
226+
| Hash | `string` | The inner hash function to use. See the supported [hash algorithms](#parameter-definitions-4). |
224227
| Length | `uint64` | The length (in bits) of the key to generate. If unspecified, the recommended length will be used, which is the size of the associated hash function's block size. |
225228

226229
##### ImportParams
@@ -229,7 +232,7 @@ As specified in [§29.3](https://www.w3.org/TR/WebCryptoAPI/#hmac-importparams)
229232

230233
| Field | Type | Description |
231234
| :---- | :--- | :---------- |
232-
| Hash | `string` | The inner hash function to use. See the supported [hash algorithms](#hash-algorithms). |
235+
| Hash | `string` | The inner hash function to use. See the supported [hash algorithms](#parameter-definitions-4). |
233236
| Length | `uint64` | The length (in bits) of the key. |
234237

235238
#### Examples
@@ -472,7 +475,7 @@ As specified in [§20.4](https://www.w3.org/TR/WebCryptoAPI/#RsaHashedKeyGenPara
472475

473476
| Field | Type | Description |
474477
| :---- | :--- | :---------- |
475-
| Hash | `string` | The [hash algorithm](#hash-algorithms) to use. |
478+
| Hash | `string` | The [hash algorithm](#parameter-definitions-4) to use. |
476479
| ModulusLength | `uint64` | The length, in bits, of the RSA modulus. |
477480
| PublicExponent | `*big.Int` | The RSA public exponent. |
478481

@@ -482,48 +485,67 @@ As specified in [§20.7](https://www.w3.org/TR/WebCryptoAPI/#RsaHashedImportPara
482485

483486
| Field | Type | Description |
484487
| :---- | :--- | :---------- |
485-
| Hash | `string` | The [hash algorithm](#hash-algorithms) to use. |
488+
| Hash | `string` | The [hash algorithm](#parameter-definitions-4) to use. |
486489

487490

488-
## SHA
491+
### SHA
489492

490493
The **SHA** algorithm is the implementation of operations described in [§30](https://www.w3.org/TR/WebCryptoAPI/#sha) of the W3C specification.
491494

492-
The implementation in this library uses Go's `io.Reader` as the input to the `digest` method.
495+
`import "github.com/armortal/webcrypto-go/algorithms/sha"`
496+
497+
#### Parameter Definitions
498+
499+
Below are the recognized algorithm names for supported SHA operations according to
500+
[§30.2](https://www.w3.org/TR/WebCryptoAPI/#sha-registration).
501+
502+
- `SHA-1`
503+
- `SHA-256`
504+
- `SHA-384`
505+
- `SHA-512`
506+
507+
##### Params
508+
509+
This is an empty struct that we use to register SHA algorithms without using a blank import. If you don't
510+
use this as in `webcrypto.Algorithm.Params` to the `Digest()` call, you can import the algorithm using
511+
a blank import like below:
512+
513+
`import _ "github.com/armortal/webcrypto-go/algorithms/sha"`
514+
515+
#### Examples
516+
517+
Below are the parameters that supported SHA operations will take according to
518+
[§30.2](https://www.w3.org/TR/WebCryptoAPI/#sha-registration).
493519

494520
```go
495521
package main
496522

497523
import (
524+
"encoding/hex"
525+
"fmt"
526+
498527
"github.com/armortal/webcrypto-go"
499528
"github.com/armortal/webcrypto-go/algorithms/sha"
500529
)
501530

502531
func main() {
503-
// digest
532+
// digest something
504533
hash, err := webcrypto.Subtle().Digest(
505-
&sha.Algorithm{
506-
Name: "SHA-256",
507-
}, bytes.NewReader([]byte("helloworld")))
534+
&webcrypto.Algorithm{
535+
Name: "SHA-256",
536+
Params: &sha.Params{}, // we use *sha.Params so we can register the algorithm without using a blank import
537+
}, []byte("test"))
508538

509539
if err != nil {
510540
panic(err)
511541
}
512542

513543
// do something with hash
544+
fmt.Println(hex.EncodeToString(hash))
514545
}
546+
515547
```
516548

517549
## Contributing
518550

519-
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.
520-
521-
## Apendix
522-
523-
### Hash Algorithms
524-
525-
Unless otherwise specified by a particular algorithm, the supported hash algorithms are
526-
- `SHA-1`
527-
- `SHA-256`
528-
- `SHA-384`
529-
- `SHA-512`
551+
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.

algorithms/sha/sha.go

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ func init() {
3535
webcrypto.RegisterAlgorithm(sha_512, &subtleCrypto{name: sha_512})
3636
}
3737

38+
// Params is an empty struct with no values. This is solely created
39+
// so we can import SHA algorithms without using a blank import for registration.
40+
type Params struct{}
41+
3842
type subtleCrypto struct {
3943
// name is the hasher that this crypto implementation uses e.g. SHA-1, SHA-256 etc.
4044
name string

examples/sha/main.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"encoding/hex"
5+
"fmt"
6+
7+
"github.com/armortal/webcrypto-go"
8+
"github.com/armortal/webcrypto-go/algorithms/sha"
9+
)
10+
11+
func main() {
12+
// digest something
13+
hash, err := webcrypto.Subtle().Digest(
14+
&webcrypto.Algorithm{
15+
Name: "SHA-256",
16+
Params: &sha.Params{}, // we use *sha.Params so we can register the algorithm without using a blank import
17+
}, []byte("test"))
18+
19+
if err != nil {
20+
panic(err)
21+
}
22+
23+
// do something with hash
24+
fmt.Println(hex.EncodeToString(hash))
25+
}

0 commit comments

Comments
 (0)