Last active
June 25, 2019 07:59
-
-
Save chonlatee/b6e85e8e3f0138ccca9cc0a1b24df580 to your computer and use it in GitHub Desktop.
show ecdsa private key, public key to console
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 ( | |
"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