|
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 |
|
} |