Last active
December 21, 2018 14:36
-
-
Save tom-code/e3bf4b14e5e21bc66dc650f60b873eec to your computer and use it in GitHub Desktop.
convert jwk to pem (EC only)
This file contains hidden or 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
| package main | |
| import( | |
| "fmt" | |
| "gopkg.in/square/go-jose.v2" | |
| "encoding/json" | |
| "io/ioutil" | |
| "crypto/ecdsa" | |
| "crypto/x509" | |
| "encoding/pem" | |
| "bytes" | |
| ) | |
| func writePem(der []byte, fname string, name string) { | |
| block := &pem.Block{ | |
| Type: name, | |
| Bytes: der, | |
| } | |
| var out bytes.Buffer | |
| pem.Encode(&out, block) | |
| ioutil.WriteFile(fname, out.Bytes(), 0666) | |
| } | |
| func main() { | |
| fmt.Println("start") | |
| body, err := ioutil.ReadFile("key.json") | |
| if err != nil { | |
| panic(err) | |
| return | |
| } | |
| var key jose.JSONWebKey | |
| if err := json.Unmarshal(body, &key); err != nil { | |
| panic(err) | |
| return | |
| } | |
| privateKey := key.Key.(*ecdsa.PrivateKey) | |
| publicKey := privateKey.PublicKey | |
| derPublic, err := x509.MarshalPKIXPublicKey(&publicKey) | |
| if err != nil { | |
| panic(err) | |
| return | |
| } | |
| writePem(derPublic, "t_public.pem", "PUBLIC") | |
| derPrivate, err := x509.MarshalECPrivateKey(privateKey) | |
| if err != nil { | |
| panic(err) | |
| return | |
| } | |
| writePem(derPrivate, "t_private.pem", "PRIVATE") | |
| } |
This file contains hidden or 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
| { | |
| "kty":"EC", | |
| "crv":"P-256", | |
| "x":"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU", | |
| "y":"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0", | |
| "d":"jpsQnnGQmL-YBIffH1136cspYG6-0iY7X1fCE9-E9LI" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment