func GetRsaPublicKeyForPkcs8PublicKeyPath(pubKeyPkcs8Path string) (*rsa.PublicKey, error) { var pubKey *rsa.PublicKey isFileGtZero, err := ioutilmore.IsFileWithSizeGtZero(pubKeyPkcs8Path) if err != nil { return pubKey, err } else if isFileGtZero == false { return pubKey, errors.New("400: key file path is zero size.") } pubKeyPkcs8Bytes, err := ioutil.ReadFile(pubKeyPkcs8Path) if err != nil { return pubKey, err } block, _ := pem.Decode(pubKeyPkcs8Bytes) pubKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes) if err != nil { return pubKey, err } pubKey, ok := pubKeyInterface.(*rsa.PublicKey) if !ok { return pubKey, errors.New("500: Cannot convert pub interface{} to *rsa.PublicKey") } return pubKey, nil }
func GetRsaPrivateKeyForPkcs1PrivateKeyPath(prvKeyPKCS1Path string) (*rsa.PrivateKey, error) { var prvKey *rsa.PrivateKey isFileGtZero, err := ioutilmore.IsFileWithSizeGtZero(prvKeyPKCS1Path) if err != nil { return prvKey, err } else if isFileGtZero == false { return prvKey, errors.New("400: key file path is zero size.") } prvKeyPkcs1Bytes, err := ioutil.ReadFile(prvKeyPKCS1Path) if err != nil { return prvKey, err } block, _ := pem.Decode(prvKeyPkcs1Bytes) return x509.ParsePKCS1PrivateKey(block.Bytes) }
func GetRsaPrivateKeyForPkcs1PrivateKeyPathWithPassword(prvKeyPKCS1Path string, password []byte) (*rsa.PrivateKey, error) { var prvKey *rsa.PrivateKey isFileGtZero, err := ioutilmore.IsFileWithSizeGtZero(prvKeyPKCS1Path) if err != nil { return prvKey, err } else if isFileGtZero == false { return prvKey, errors.New("400: key file path is zero size.") } prvKeyPkcs1BytesEnc, err := ioutil.ReadFile(prvKeyPKCS1Path) if err != nil { return prvKey, err } block, _ := pem.Decode(prvKeyPkcs1BytesEnc) prvKeyBytes, err := x509.DecryptPEMBlock(block, password) if err != nil { return prvKey, err } return x509.ParsePKCS1PrivateKey(prvKeyBytes) }