Add extraction of PrivateKey from pem
This commit is contained in:
parent
b3f7657c1b
commit
69e03da94a
1 changed files with 18 additions and 0 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"crypto/elliptic"
|
"crypto/elliptic"
|
||||||
"crypto/x509/pkix"
|
"crypto/x509/pkix"
|
||||||
"encoding/asn1"
|
"encoding/asn1"
|
||||||
|
"encoding/pem"
|
||||||
|
|
||||||
"github.com/lbryio/lbry.go/v2/extras/errors"
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
||||||
|
|
||||||
|
@ -16,6 +17,14 @@ type publicKeyInfo struct {
|
||||||
PublicKey asn1.BitString
|
PublicKey asn1.BitString
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//This type provides compatibility with the btcec package
|
||||||
|
type ecPrivateKey struct {
|
||||||
|
Version int
|
||||||
|
PrivateKey []byte
|
||||||
|
NamedCurveOID asn1.ObjectIdentifier `asn1:"optional,explicit,tag:0"`
|
||||||
|
PublicKey asn1.BitString `asn1:"optional,explicit,tag:1"`
|
||||||
|
}
|
||||||
|
|
||||||
func PublicKeyToDER(publicKey *btcec.PublicKey) ([]byte, error) {
|
func PublicKeyToDER(publicKey *btcec.PublicKey) ([]byte, error) {
|
||||||
var publicKeyBytes []byte
|
var publicKeyBytes []byte
|
||||||
var publicKeyAlgorithm pkix.AlgorithmIdentifier
|
var publicKeyAlgorithm pkix.AlgorithmIdentifier
|
||||||
|
@ -47,3 +56,12 @@ func GetPublicKeyFromBytes(pubKeyBytes []byte) (*btcec.PublicKey, error) {
|
||||||
pubkeyBytes1 := []byte(PKInfo.PublicKey.Bytes)
|
pubkeyBytes1 := []byte(PKInfo.PublicKey.Bytes)
|
||||||
return btcec.ParsePubKey(pubkeyBytes1, btcec.S256())
|
return btcec.ParsePubKey(pubkeyBytes1, btcec.S256())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Returns a btec.Private key object if provided a correct secp256k1 encoded pem.
|
||||||
|
func ExtractKeyFromPem(pm string) (*btcec.PrivateKey, *btcec.PublicKey) {
|
||||||
|
byta := []byte(pm)
|
||||||
|
blck, _ := pem.Decode(byta)
|
||||||
|
var ecp ecPrivateKey
|
||||||
|
asn1.Unmarshal(blck.Bytes, &ecp)
|
||||||
|
return btcec.PrivKeyFromBytes(btcec.S256(), ecp.PrivateKey)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue