DH¶
- class pyflocker.ciphers.backends.cryptography_.DH.DHParameters(key_size: int | None, generator: int = 2, _params: DHParameters | None = None)[source]¶
Bases:
BaseDHParameters
- private_key() DHPrivateKey [source]¶
Create a DH private key from the parameters.
- Returns:
A private key object.
- serialize(encoding: str = 'PEM', format: str = 'PKCS3') bytes [source]¶
Serialize the DH parameters.
- Parameters:
encoding – The encoding can be
PEM
orDER
. Defaults toPEM
.format – The format. Defaults to
PKCS3
.
- Returns:
The parameters encoded as bytes object.
- Raises:
ValueError – if the encoding of format is invalid.
- classmethod load(data: bytes) DHParameters [source]¶
Deserialize the encoded DH parameters.
- Parameters:
data – The parameters as an encoded bytes object.
- Returns:
DH parameter object.
- classmethod load_from_parameters(p: int, g: int = 2, q: int | None = None) DHParameters [source]¶
Generates a DH parameter group from the parameters.
- Parameters:
p – The prime modulus value.
g – The generator value. Must be 2 or 5. Default is 2.
q – p subgroup order value. Defaults to
None
.
- Returns:
DH Parameter object.
- class pyflocker.ciphers.backends.cryptography_.DH.DHPrivateKey(key: DHPrivateKey)[source]¶
Bases:
BaseDHPrivateKey
- parameters() DHParameters [source]¶
Creates a new DH Parameters object from the key.
- Returns:
The DH parameter object.
- public_key() DHPublicKey [source]¶
Create a public key from the private key.
- Returns:
A public key object.
- exchange(peer_public_key: bytes | DHPublicKey | BaseDHPublicKey) bytes [source]¶
Perform a key exchange.
- Parameters:
peer_public_key – The peer public key can be a bytes or a
BaseDHPublicKey
object.- Returns:
A shared key.
- Raises:
TypeError – if
peer_public_key
is not a bytes-like or DH Public Key object.
- serialize(encoding: str = 'PEM', format: str = 'PKCS8', passphrase: bytes | None = None) bytes [source]¶
Serialize the private key.
- Parameters:
encoding – The encoding to use.
format – The format to use.
passphrase – The passphrase to use to protect the private key.
None
if the private key is not encrypted.
- Returns:
The private key as bytes object.
- Raises:
ValueError – if the encoding or format is invalid.
TypeError – if the passphrase is not a bytes-like object.
Important
The
encoding
andformat
supported by one backend may not be supported by the other. You should check the documentation of the implementation of this method for supported options.
- classmethod load(data: bytes, passphrase: bytes | None = None) DHPrivateKey [source]¶
Deserialize and load the the private key.
- Parameters:
data – The serialized private key as bytes-like object.
passphrase – The passphrase that was used to protect the private key. If key is not protected, passphrase is
None
.
- Returns:
A private key.
- Raises:
ValueError – If the key could not be deserialized.
TypeError – If passphrase is not a bytes-like object.
- class pyflocker.ciphers.backends.cryptography_.DH.DHPublicKey(key: DHPublicKey)[source]¶
Bases:
BaseDHPublicKey
- parameters() DHParameters [source]¶
Creates a new DH parameters object from the key.
- Returns:
The DH parameter object.
- serialize(encoding: str = 'PEM', format: str = 'SubjectPublicKeyInfo') bytes [source]¶
Serialize the public key.
- Parameters:
encoding – The encoding to use. It can be
PEM
orDER
.format – The format can be
SubjectPublicKeyInfo
only.
- Returns:
The public key as bytes object.
- Raises:
ValueError – if the encoding or format is invalid.
- classmethod load(data: bytes) DHPublicKey [source]¶
Deserialize and load the public key.
- Parameters:
data – The serialized public key as bytes-like object.
- Returns:
A public key object.
- Raises:
ValueError – If the key could not be deserialized.
- pyflocker.ciphers.backends.cryptography_.DH.generate(key_size: int, g: int = 2) DHParameters [source]¶
Generate DHE parameter with prime number’s bit size
bits
and generatorg
(default 2). Recommended size ofbits
> 1024.- Parameters:
key_size – The bit length of the prime modulus.
g – The value to use as a generator value. Default is 2.
- Returns:
A DH key exchange paramenter object.
- pyflocker.ciphers.backends.cryptography_.DH.load_from_parameters(p: int, g: int = 2, q: int | None = None) DHParameters [source]¶
Create a DH Parameter object from the given parameters.
- Parameters:
p – The prime modulus p as
int
.g – The generator.
q – p subgroup order value.
- Returns:
A DH key exchange paramenter object.
- pyflocker.ciphers.backends.cryptography_.DH.load_parameters(data: bytes) DHParameters [source]¶
Deserialize the DH parameters and load a parameter object.
- Parameters:
data – Serialized DH Parameter.
- Returns:
A parameter object.
- pyflocker.ciphers.backends.cryptography_.DH.load_public_key(data: bytes) DHPublicKey [source]¶
Loads the public key and returns a Key interface.
- Parameters:
data – The public key (a bytes-like object) to deserialize.
- Returns:
A public key object.
- pyflocker.ciphers.backends.cryptography_.DH.load_private_key(data: bytes, passphrase: bytes | None = None) DHPrivateKey [source]¶
Loads the private key and returns a private key object.
If the private key was not encrypted duting the serialization,
passphrase
must beNone
, otherwise it must be abytes-like
object.- Parameters:
data – The private key (a bytes-like object) to deserialize.
passphrase – The passphrase (in bytes) that was used to encrypt the private key.
None
if the key was not encrypted.
- Returns:
A private key object.