Skip to content

Commit a9303c1

Browse files
authored
Merge pull request #30 from armortal/feature/2-ecdsa-implementation
Feature/2 ecdsa implementation
2 parents 035b878 + 8de5106 commit a9303c1

File tree

8 files changed

+1010
-5
lines changed

8 files changed

+1010
-5
lines changed

README.md

+59
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
An implementation of the W3C Web Cryptography API specification (https://www.w3.org/TR/WebCryptoAPI/) for Go using Go's standard `crypto` library.
44

5+
> [!IMPORTANT]
6+
> Whilst we try to ensure that we don't commit breaking changes until we release our first stable version, there
7+
> may be times where decisions made during early development no longer make sense and therefore require
8+
> breaking changes. Please be mindful of this when updating your version of this library until we hit v1.0.0.
9+
510
## Contents
611

712
- [Background](#background)
813
- [Implementation status](#implementation-status)
914
- [Getting started](#getting-started)
1015
- [Algorithms](#algorithms)
16+
- [ECDSA](#ecdsa)
1117
- [HMAC](#hmac)
1218
- [RSA-OAEP](#rsa-oaep)
1319
- [SHA](#sha)
@@ -29,6 +35,7 @@ This library is still in active development and all algorithms are not yet suppo
2935

3036
| Algorithm | encrypt | decrypt | sign | verify | digest | generateKey | deriveKey | deriveBits | importKey | exportKey | wrapKey | unwrapKey |
3137
| :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: |
38+
| [ECDSA](#ecdsa) |||:white_check_mark:|:white_check_mark:||:white_check_mark:|||:white_check_mark:|:white_check_mark:|||
3239
| [HMAC](#hmac) |||:white_check_mark:|:white_check_mark:||:white_check_mark:|||:white_check_mark:|:white_check_mark:|||
3340
| [RSA-OAEP](#rsa-oaep) |:white_check_mark:|:white_check_mark:||||:white_check_mark:|||:white_check_mark:|:white_check_mark:|||
3441
| [SHA](#sha) |||||:white_check_mark:||||||||
@@ -39,6 +46,52 @@ This library is still in active development and all algorithms are not yet suppo
3946

4047
## Algorithms
4148

49+
### ECDSA
50+
51+
The **ECDSA** algorithm is the implementation of operations described in [§23](https://www.w3.org/TR/WebCryptoAPI/#ecdsa) of the W3C specification.
52+
53+
```go
54+
package main
55+
56+
import (
57+
"github.com/armortal/webcrypto-go"
58+
"github.com/armortal/webcrypto-go/algorithms/ecdsa"
59+
)
60+
61+
func main() {
62+
// generate a new ECDSA key
63+
key, err := webcrypto.Subtle().GenerateKey(
64+
&ecdsa.Algorithm{
65+
NamedCurve: "P-256",
66+
}, true, webcrypto.Sign, webcrypto.Verify)
67+
if err != nil {
68+
panic(err)
69+
}
70+
71+
ckp := key.(webcrypto.CryptoKeyPair)
72+
73+
// sign some data with the private key
74+
sig, err := webcrypto.Subtle().Sign(&ecdsa.Algorithm{
75+
Hash: "SHA-256",
76+
}, ckp.PrivateKey(), []byte("test"))
77+
if err != nil {
78+
panic(err)
79+
}
80+
81+
// verify the signature with the public key
82+
ok, err := webcrypto.Subtle().Verify(&ecdsa.Algorithm{
83+
Hash: "SHA-256",
84+
}, ckp.PublicKey(), sig, []byte("test"))
85+
if err != nil {
86+
panic(err)
87+
}
88+
89+
if !ok {
90+
// didn't verify - do something
91+
}
92+
}
93+
```
94+
4295
### HMAC
4396

4497
The **HMAC** algorithm is the implementation of operations described in [§29](https://www.w3.org/TR/WebCryptoAPI/#hmac) of the W3C specification.
@@ -54,6 +107,12 @@ import (
54107
func main() {
55108
// Generate a new key. A *hmac.CryptoKey is returned which implements webcrypto.CryptoKey
56109
key, err := webcrypto.Subtle().GenerateKey(
110+
&Algorithm{
111+
Name: "ECDSA",
112+
Params: ecdsa.KeyGenParams{
113+
114+
}
115+
}
57116
&hmac.Algorithm{
58117
KeyGenParams: &hmac.KeyGenParams{
59118
Hash: "SHA-256",

algorithm.go

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package webcrypto
1616

17+
import "fmt"
18+
1719
var algorithms = map[string]func() SubtleCrypto{}
1820

1921
// Algorithm implements the Algorithm dictionary type as specified at
@@ -33,6 +35,10 @@ type KeyAlgorithm interface {
3335
// RegisterAlgorithm will register SubtleCrypto implementations referenced by the algorithm
3436
// name provided. When fn gets called, it should return a NEW instance of the implementation.
3537
func RegisterAlgorithm(name string, fn func() SubtleCrypto) {
38+
_, ok := algorithms[name]
39+
if ok {
40+
panic(fmt.Sprintf("%s algorithm already registered", name))
41+
}
3642
algorithms[name] = fn
3743
}
3844

0 commit comments

Comments
 (0)