Skip to content

Instantly share code, notes, and snippets.

@wuriyanto48
Last active March 1, 2022 14:46
Show Gist options
  • Save wuriyanto48/b0842bbf4444a18f17f8ddd1178fa80e to your computer and use it in GitHub Desktop.
Save wuriyanto48/b0842bbf4444a18f17f8ddd1178fa80e to your computer and use it in GitHub Desktop.
Golang Parsing RSA Private Key and Public Key

generate private key:

openssl genrsa -out private_key.pem 4096

generate public key:

openssl rsa -pubout -in private_key.pem -out public_key.pem

convert private key to pkcs8 format:

openssl pkcs8 -topk8 -inform PEM -in private_key.pem -out private_key_pkcs8.pem -nocrypt

convert public key to DER format:

openssl rsa -inform PEM -in private_key.pem -outform DER -pubout -out public_key.der

convert private key to DER format:

openssl rsa -inform PEM -in private_key.pem -outform DER -out private_key.der

convert private key to PKCS8 DER format:

openssl pkcs8 -topk8 -inform PEM -in private_key.pem -outform DER -nocrypt -out private_key.der
package main
import (
"crypto/dsa"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
)
func main() {
// privateKey, err := ParsePkcs8PrivateKey("private_key_pkcs8.pem")
// privateKey, err := ParsePrivateKeyDer("private_key.der")
privateKey, err := ParsePrivateKey("private_key.pem")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(privateKey)
// publicKey, err := ParsePublicKey("public_key.pem")
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// fmt.Println(publicKey)
}
func ParsePkcs8PrivateKey(fileName string) (interface{}, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer func() { f.Close() }()
privateKeyData, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
block, _ := pem.Decode(privateKeyData)
privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
switch privateKey.(type) {
case *rsa.PrivateKey:
fmt.Println("priv is of type RSA")
case *dsa.PrivateKey:
fmt.Println("priv is of type DSA")
case *ecdsa.PrivateKey:
fmt.Println("priv is of type ECDSA")
case ed25519.PrivateKey:
fmt.Println("pub is of type Ed25519")
default:
panic("unknown type of private key")
}
return privateKey, nil
}
func ParsePrivateKeyDer(fileName string) (*rsa.PrivateKey, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer func() { f.Close() }()
privateKeyData, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
privateKey, err := x509.ParsePKCS1PrivateKey(privateKeyData)
if err != nil {
return nil, err
}
return privateKey, nil
}
func ParsePublicKeyDer(fileName string) (interface{}, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer func() { f.Close() }()
publicKeyData, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
publicKey, err := x509.ParsePKIXPublicKey(publicKeyData)
if err != nil {
return nil, err
}
switch publicKey.(type) {
case *rsa.PublicKey:
fmt.Println("pub is of type RSA")
case *dsa.PublicKey:
fmt.Println("pub is of type DSA")
case *ecdsa.PublicKey:
fmt.Println("pub is of type ECDSA")
case ed25519.PublicKey:
fmt.Println("pub is of type Ed25519")
default:
panic("unknown type of public key")
}
return publicKey, nil
}
func ParsePrivateKey(fileName string) (*rsa.PrivateKey, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer func() { f.Close() }()
privateKeyData, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
block, _ := pem.Decode(privateKeyData)
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return privateKey, nil
}
func ParsePublicKey(fileName string) (interface{}, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer func() { f.Close() }()
publicKeyData, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
block, _ := pem.Decode(publicKeyData)
publicKey, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
switch publicKey.(type) {
case *rsa.PublicKey:
fmt.Println("pub is of type RSA")
case *dsa.PublicKey:
fmt.Println("pub is of type DSA")
case *ecdsa.PublicKey:
fmt.Println("pub is of type ECDSA")
case ed25519.PublicKey:
fmt.Println("pub is of type Ed25519")
default:
panic("unknown type of public key")
}
return publicKey, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment