Created
May 27, 2015 20:51
-
-
Save simcap/2f523dadd168da07a623 to your computer and use it in GitHub Desktop.
Generate DER encoding from known ECDSA private key in Golang
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" | |
| "flag" | |
| "fmt" | |
| "io/ioutil" | |
| "math/big" | |
| "path/filepath" | |
| ) | |
| func main() { | |
| seed := flag.String("s", "", "seed to generate private key") | |
| flag.Parse() | |
| var priv *ecdsa.PrivateKey | |
| if *seed != "" { | |
| k := new(big.Int) | |
| k.SetString(*seed, 16) | |
| priv = new(ecdsa.PrivateKey) | |
| curve := elliptic.P256() | |
| priv.PublicKey.Curve = curve | |
| priv.D = k | |
| priv.PublicKey.X, priv.PublicKey.Y = curve.ScalarBaseMult(k.Bytes()) | |
| } else { | |
| priv, _ = ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | |
| } | |
| fmt.Printf("%#v\n", priv) | |
| pubkey_bytes, _ := x509.MarshalPKIXPublicKey(&priv.PublicKey) | |
| ioutil.WriteFile( | |
| filepath.Join("./", "go.der"), | |
| pubkey_bytes, | |
| 0644, | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment