mirror of
https://github.com/libp2p/go-libp2p-core.git
synced 2025-01-16 02:40:08 +08:00
52a4260be5
* event: Add autonat events (#25) * add events for identify (#26) * implement caching for rsaKey.Bytes() * store marshalled protobuf in cache for RsaPublicKey.Bytes() * fix(crypto): fix build when openssl is enabled * add godocs to routability events. Co-authored-by: Łukasz Magiera <magik6k@users.noreply.github.com> Co-authored-by: Whyrusleeping <why@ipfs.io> Co-authored-by: Adin Schmahmann <adin.schmahmann@gmail.com> Co-authored-by: Steven Allen <steven@stebalien.com>
48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
// +build openssl
|
|
|
|
package crypto
|
|
|
|
import (
|
|
"crypto"
|
|
"crypto/ecdsa"
|
|
"crypto/ed25519"
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
|
|
btcec "github.com/btcsuite/btcd/btcec"
|
|
openssl "github.com/libp2p/go-openssl"
|
|
)
|
|
|
|
// KeyPairFromStdKey wraps standard library (and secp256k1) private keys in libp2p/go-libp2p-core/crypto keys
|
|
func KeyPairFromStdKey(priv crypto.PrivateKey) (PrivKey, PubKey, error) {
|
|
if priv == nil {
|
|
return nil, nil, ErrNilPrivateKey
|
|
}
|
|
|
|
switch p := priv.(type) {
|
|
case *rsa.PrivateKey:
|
|
pk, err := openssl.LoadPrivateKeyFromDER(x509.MarshalPKCS1PrivateKey(p))
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
return &opensslPrivateKey{pk}, &opensslPublicKey{key: pk}, nil
|
|
|
|
case *ecdsa.PrivateKey:
|
|
return &ECDSAPrivateKey{p}, &ECDSAPublicKey{&p.PublicKey}, nil
|
|
|
|
case *ed25519.PrivateKey:
|
|
pubIfc := p.Public()
|
|
pub, _ := pubIfc.(ed25519.PublicKey)
|
|
return &Ed25519PrivateKey{*p}, &Ed25519PublicKey{pub}, nil
|
|
|
|
case *btcec.PrivateKey:
|
|
sPriv := Secp256k1PrivateKey(*p)
|
|
sPub := Secp256k1PublicKey(*p.PubKey())
|
|
return &sPriv, &sPub, nil
|
|
|
|
default:
|
|
return nil, nil, ErrBadKeyType
|
|
}
|
|
}
|