-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(#29): updated readme, added examples, minor fixes
- Loading branch information
1 parent
bd5a97b
commit 35ae19c
Showing
6 changed files
with
268 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "hmac", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"program": "examples/hmac/main.go" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/armortal/webcrypto-go" | ||
"github.com/armortal/webcrypto-go/algorithms/ecdsa" | ||
) | ||
|
||
func main() { | ||
// generate a new P-256 ECDSA key | ||
key, err := webcrypto.Subtle().GenerateKey( | ||
&webcrypto.Algorithm{ | ||
Name: "ECDSA", | ||
Params: &ecdsa.KeyGenParams{ | ||
NamedCurve: "P-256", | ||
}, | ||
}, true, []webcrypto.KeyUsage{ | ||
webcrypto.Sign, | ||
webcrypto.Verify, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// key returned is a webcrypto.CryptoKeyPair | ||
cryptoKey := key.(webcrypto.CryptoKeyPair) | ||
|
||
// sign some data with the private key | ||
sig, err := webcrypto.Subtle().Sign(&webcrypto.Algorithm{ | ||
Name: "ECDSA", | ||
Params: &ecdsa.Params{ | ||
Hash: "SHA-256", | ||
}, | ||
}, cryptoKey.PrivateKey(), []byte("test")) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// verify the signature with the public key | ||
ok, err := webcrypto.Subtle().Verify(&webcrypto.Algorithm{ | ||
Name: "ECDSA", | ||
Params: &ecdsa.Params{ | ||
Hash: "SHA-256", | ||
}, | ||
}, cryptoKey.PublicKey(), sig, []byte("test")) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if !ok { | ||
panic("signature didn't verify") | ||
} | ||
|
||
// export the public/private key as webcrypto.JsonWebKey | ||
out, err := webcrypto.Subtle().ExportKey(webcrypto.Jwk, cryptoKey.PrivateKey()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// do something with jwk | ||
jwk := out.(*webcrypto.JsonWebKey) | ||
|
||
// export the key as PKCS8 | ||
out, err = webcrypto.Subtle().ExportKey(webcrypto.PKCS8, cryptoKey.PrivateKey()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// do something with the pkcs8 key | ||
pkcs8 := out.([]byte) | ||
|
||
// import a public/private key from a jwk | ||
in, err := webcrypto.Subtle().ImportKey(webcrypto.Jwk, jwk, &webcrypto.Algorithm{ | ||
Name: "ECDSA", | ||
Params: &ecdsa.KeyImportParams{ | ||
NamedCurve: "P-256", | ||
}, | ||
}, true, []webcrypto.KeyUsage{ | ||
webcrypto.Sign, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// import a public/private key from PKCS8 | ||
in, err = webcrypto.Subtle().ImportKey(webcrypto.PKCS8, pkcs8, &webcrypto.Algorithm{ | ||
Name: "ECDSA", | ||
Params: &ecdsa.KeyImportParams{ | ||
NamedCurve: "P-256", | ||
}, | ||
}, true, []webcrypto.KeyUsage{ | ||
webcrypto.Sign, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// do something with the imported webcrypto.CryptoKey | ||
fmt.Println(in.Type()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/armortal/webcrypto-go" | ||
"github.com/armortal/webcrypto-go/algorithms/hmac" | ||
) | ||
|
||
func main() { | ||
// generate a new key | ||
key, err := webcrypto.Subtle().GenerateKey( | ||
&webcrypto.Algorithm{ | ||
Name: "HMAC", | ||
Params: &hmac.KeyGenParams{ | ||
Hash: "SHA-256", | ||
}, | ||
}, true, []webcrypto.KeyUsage{ | ||
webcrypto.Sign, | ||
webcrypto.Verify, | ||
}) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// the generated key returns a webcrypto.CryptoKey or | ||
// more specifically, a *hmac.CryptoKey | ||
cryptoKey := key.(webcrypto.CryptoKey) | ||
|
||
// sign some data - no params required. | ||
sig, err := webcrypto.Subtle().Sign(&webcrypto.Algorithm{ | ||
Name: "HMAC", | ||
}, cryptoKey, []byte("test")) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// verify the signature | ||
ok, err := webcrypto.Subtle().Verify(&webcrypto.Algorithm{ | ||
Name: "HMAC", | ||
}, cryptoKey, sig, []byte("test")) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if !ok { | ||
panic("signature didn't verify") | ||
} | ||
|
||
// export the key as *webcrypto.JsonWebKey | ||
out, err := webcrypto.Subtle().ExportKey(webcrypto.Jwk, cryptoKey) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
jwk := out.(*webcrypto.JsonWebKey) | ||
// do something with jwk | ||
|
||
// export the key as raw bytes | ||
out, err = webcrypto.Subtle().ExportKey(webcrypto.Raw, cryptoKey) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
raw := out.([]byte) | ||
// do something with raw bytes | ||
|
||
// import a key from a jwk | ||
in, err := webcrypto.Subtle().ImportKey( | ||
webcrypto.Jwk, | ||
jwk, | ||
&webcrypto.Algorithm{ | ||
Name: "HMAC", | ||
Params: &hmac.ImportParams{ | ||
Hash: "SHA-256", | ||
}, | ||
}, | ||
true, | ||
[]webcrypto.KeyUsage{ | ||
webcrypto.Sign, | ||
webcrypto.Verify, | ||
}) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// import a key from raw bytes | ||
in, err = webcrypto.Subtle().ImportKey( | ||
webcrypto.Raw, | ||
raw, | ||
&webcrypto.Algorithm{ | ||
Name: "HMAC", | ||
Params: &hmac.ImportParams{ | ||
Hash: "SHA-256", | ||
}, | ||
}, | ||
true, | ||
[]webcrypto.KeyUsage{ | ||
webcrypto.Sign, | ||
webcrypto.Verify, | ||
}) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// do something with your imported keys | ||
fmt.Println(in.Type()) | ||
} |