libp2p.crypto package

Subpackages

Submodules

libp2p.crypto.authenticated_encryption module

class libp2p.crypto.authenticated_encryption.EncryptionParameters(cipher_type: str, hash_type: str, iv: bytes, mac_key: bytes, cipher_key: bytes)

Bases: object

cipher_key: bytes
cipher_type: str
hash_type: str
iv: bytes
mac_key: bytes
exception libp2p.crypto.authenticated_encryption.InvalidMACException

Bases: Exception

class libp2p.crypto.authenticated_encryption.MacAndCipher(parameters: EncryptionParameters)

Bases: object

authenticate(data: bytes) bytes
decrypt_if_valid(data_with_tag: bytes) bytes
encrypt(data: bytes) bytes
libp2p.crypto.authenticated_encryption.initialize_pair(cipher_type: str, hash_type: str, secret: bytes) Tuple[EncryptionParameters, EncryptionParameters]

Return a pair of Keys for use in securing a communications channel with authenticated encryption derived from the secret and using the requested cipher_type and hash_type.

libp2p.crypto.ecc module

class libp2p.crypto.ecc.ECCPrivateKey(impl: int, curve: <MagicMock name='mock.curve.Curve' id='140432502522880'>)

Bases: PrivateKey

get_public_key() PublicKey
get_type() KeyType

Returns the KeyType for self.

classmethod new(curve: str) ECCPrivateKey
sign(data: bytes) bytes
to_bytes() bytes

Returns the byte representation of this key.

class libp2p.crypto.ecc.ECCPublicKey(impl: <MagicMock name='mock.point.Point' id='140432502538976'>, curve: <MagicMock name='mock.curve.Curve' id='140432502522880'>)

Bases: PublicKey

classmethod from_bytes(data: bytes, curve: str) ECCPublicKey
get_type() KeyType

Returns the KeyType for self.

to_bytes() bytes

Returns the byte representation of this key.

verify(data: bytes, signature: bytes) bool

Verify that signature is the cryptographic signature of the hash of data.

libp2p.crypto.ecc.create_new_key_pair(curve: str) KeyPair

Return a new ECC keypair with the requested curve type, e.g. “P-256”.

libp2p.crypto.ecc.infer_local_type(curve: str) <MagicMock name='mock.curve.Curve' id='140432502522880'>

Convert a str representation of some elliptic curve to a representation understood by the backend of this module.

libp2p.crypto.ed25519 module

class libp2p.crypto.ed25519.Ed25519PrivateKey(impl: PrivateKey)

Bases: PrivateKey

classmethod from_bytes(data: bytes) Ed25519PrivateKey
get_public_key() PublicKey
get_type() KeyType

Returns the KeyType for self.

classmethod new(seed: bytes | None = None) Ed25519PrivateKey
sign(data: bytes) bytes
to_bytes() bytes

Returns the byte representation of this key.

class libp2p.crypto.ed25519.Ed25519PublicKey(impl: PublicKey)

Bases: PublicKey

classmethod from_bytes(key_bytes: bytes) Ed25519PublicKey
get_type() KeyType

Returns the KeyType for self.

to_bytes() bytes

Returns the byte representation of this key.

verify(data: bytes, signature: bytes) bool

Verify that signature is the cryptographic signature of the hash of data.

libp2p.crypto.ed25519.create_new_key_pair(seed: bytes | None = None) KeyPair

libp2p.crypto.exceptions module

exception libp2p.crypto.exceptions.CryptographyError

Bases: BaseLibp2pError

exception libp2p.crypto.exceptions.MissingDeserializerError

Bases: CryptographyError

Raise if the requested deserialization routine is missing for some type of cryptographic key.

libp2p.crypto.key_exchange module

libp2p.crypto.key_exchange.create_ephemeral_key_pair(curve_type: str) Tuple[PublicKey, Callable[[bytes], bytes]]

Facilitates ECDH key exchange.

libp2p.crypto.keys module

class libp2p.crypto.keys.Key

Bases: ABC

A Key represents a cryptographic key.

abstract get_type() KeyType

Returns the KeyType for self.

abstract to_bytes() bytes

Returns the byte representation of this key.

class libp2p.crypto.keys.KeyPair(private_key: libp2p.crypto.keys.PrivateKey, public_key: libp2p.crypto.keys.PublicKey)

Bases: object

private_key: PrivateKey
public_key: PublicKey
class libp2p.crypto.keys.KeyType(value)

Bases: Enum

An enumeration.

ECC_P256 = 4
ECDSA = 3
Ed25519 = 1
RSA = 0
Secp256k1 = 2
class libp2p.crypto.keys.PrivateKey

Bases: Key

A PrivateKey represents a cryptographic private key.

classmethod deserialize_from_protobuf(protobuf_data: bytes) PrivateKey
abstract get_public_key() PublicKey
serialize() bytes

Return the canonical serialization of this Key.

abstract sign(data: bytes) bytes
class libp2p.crypto.keys.PublicKey

Bases: Key

A PublicKey represents a cryptographic public key.

classmethod deserialize_from_protobuf(protobuf_data: bytes) PublicKey
serialize() bytes

Return the canonical serialization of this Key.

abstract verify(data: bytes, signature: bytes) bool

Verify that signature is the cryptographic signature of the hash of data.

libp2p.crypto.rsa module

class libp2p.crypto.rsa.RSAPrivateKey(impl: RsaKey)

Bases: PrivateKey

get_public_key() PublicKey
get_type() KeyType

Returns the KeyType for self.

classmethod new(bits: int = 2048, e: int = 65537) RSAPrivateKey
sign(data: bytes) bytes
to_bytes() bytes

Returns the byte representation of this key.

class libp2p.crypto.rsa.RSAPublicKey(impl: RsaKey)

Bases: PublicKey

classmethod from_bytes(key_bytes: bytes) RSAPublicKey
get_type() KeyType

Returns the KeyType for self.

to_bytes() bytes

Returns the byte representation of this key.

verify(data: bytes, signature: bytes) bool

Verify that signature is the cryptographic signature of the hash of data.

libp2p.crypto.rsa.create_new_key_pair(bits: int = 2048, e: int = 65537) KeyPair

Returns a new RSA keypair with the requested key size (bits) and the given public exponent e.

Sane defaults are provided for both values.

libp2p.crypto.secp256k1 module

class libp2p.crypto.secp256k1.Secp256k1PrivateKey(impl: PrivateKey)

Bases: PrivateKey

classmethod deserialize(data: bytes) Secp256k1PrivateKey
classmethod from_bytes(data: bytes) Secp256k1PrivateKey
get_public_key() PublicKey
get_type() KeyType

Returns the KeyType for self.

classmethod new(secret: bytes | None = None) Secp256k1PrivateKey
sign(data: bytes) bytes
to_bytes() bytes

Returns the byte representation of this key.

class libp2p.crypto.secp256k1.Secp256k1PublicKey(impl: PublicKey)

Bases: PublicKey

classmethod deserialize(data: bytes) Secp256k1PublicKey
classmethod from_bytes(data: bytes) Secp256k1PublicKey
get_type() KeyType

Returns the KeyType for self.

to_bytes() bytes

Returns the byte representation of this key.

verify(data: bytes, signature: bytes) bool

Verify that signature is the cryptographic signature of the hash of data.

libp2p.crypto.secp256k1.create_new_key_pair(secret: bytes | None = None) KeyPair

Returns a new Secp256k1 keypair derived from the provided secret, a sequence of bytes corresponding to some integer between 0 and the group order.

A valid secret is created if None is passed.

libp2p.crypto.serialization module

libp2p.crypto.serialization.deserialize_private_key(data: bytes) PrivateKey
libp2p.crypto.serialization.deserialize_public_key(data: bytes) PublicKey

Module contents