2017-05-02 23:33:51 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"io/ioutil"
|
|
|
|
)
|
|
|
|
|
2017-07-04 18:29:11 +00:00
|
|
|
const (
|
|
|
|
InvalidPrivateKeyFileError = Error("InvalidPrivateKeyFileError")
|
|
|
|
)
|
|
|
|
|
2017-05-02 23:33:51 +00:00
|
|
|
// LoadPrivateKeyFromFile loads a private key from a file...
|
|
|
|
func LoadPrivateKeyFromFile(filename string) (*rsa.PrivateKey, error) {
|
|
|
|
pemData, err := ioutil.ReadFile(filename)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
block, _ := pem.Decode(pemData)
|
|
|
|
if block == nil || block.Type != "RSA PRIVATE KEY" {
|
2017-07-04 18:29:11 +00:00
|
|
|
return nil, InvalidPrivateKeyFileError
|
2017-05-02 23:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return x509.ParsePKCS1PrivateKey(block.Bytes)
|
|
|
|
}
|