Skip to content

Instantly share code, notes, and snippets.

@simcap
Created May 27, 2015 20:51
Show Gist options
  • Select an option

  • Save simcap/2f523dadd168da07a623 to your computer and use it in GitHub Desktop.

Select an option

Save simcap/2f523dadd168da07a623 to your computer and use it in GitHub Desktop.
Generate DER encoding from known ECDSA private key in Golang
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