go-ricochet/utils/crypto.go

29 lines
559 B
Go
Raw Normal View History

2017-05-02 23:33:51 +00:00
package utils
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"io/ioutil"
)
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" {
return nil, InvalidPrivateKeyFileError
2017-05-02 23:33:51 +00:00
}
return x509.ParsePKCS1PrivateKey(block.Bytes)
}