I\'m using crypto/rsa
, and trying to find a way to properly save and load a key. Is there a correct way to create a []byte
from an rsa.P
Since the public key part of your question wasn't answered and I just ran into the same problem and solved it, here it is:
Note the &
in front of the Argument to MarshalPKIXPublicKey
Priv := rsa.GenerateKey(rand.Reader, 4096)
pubASN1, err := x509.MarshalPKIXPublicKey(&Priv.PublicKey)
if err != nil {
// do something about it
}
pubBytes := pem.EncodeToMemory(&pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: pubASN1,
})
ioutil.WriteFile("key.pub", pubBytes, 0644)
Relevant reads:
PS: MarshalPKIXPublicKey also accepts ECDSA keys, ajust the pem header appropriately.