Skip to content

Instantly share code, notes, and snippets.

@chonlatee
Last active June 25, 2019 07:59
Show Gist options
  • Save chonlatee/b6e85e8e3f0138ccca9cc0a1b24df580 to your computer and use it in GitHub Desktop.
Save chonlatee/b6e85e8e3f0138ccca9cc0a1b24df580 to your computer and use it in GitHub Desktop.
show ecdsa private key, public key to console
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"fmt"
)
func main() {
priv, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
if err != nil {
panic(err)
}
ecder, err := x509.MarshalECPrivateKey(priv)
if err != nil {
panic(err)
}
privPem := &pem.Block{Type: "EC PRIVATE KEY", Bytes: ecder}
pk := pem.EncodeToMemory(privPem)
fmt.Println(string(pk))
pubKey := &priv.PublicKey
ecdsaPub, err := x509.MarshalPKIXPublicKey(pubKey)
if err != nil {
panic(err)
}
pubPem := &pem.Block{Type: "EC PUBLIC KEY", Bytes: ecdsaPub}
pbk := pem.EncodeToMemory(pubPem)
fmt.Println(string(pbk))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment