22 lines
349 B
Go
22 lines
349 B
Go
package crypto
|
|
|
|
import (
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"errors"
|
|
)
|
|
|
|
func BytesToPrivateKey(b []byte) (*rsa.PrivateKey, error) {
|
|
block, _ := pem.Decode(b)
|
|
if block == nil {
|
|
return nil, errors.New("PEM decode failed")
|
|
}
|
|
|
|
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return key, nil
|
|
}
|