Encryption
Encrypt, decrypt, sign, and verify messages using PGP.
Encryption ¶
Encryption and decryption helper using PGP (Pretty Good Privacy).
This class provides methods to sign, encrypt, decrypt, and verify messages using PGP encryption with private/public key pairs.
Attributes:
| Name | Type | Description |
|---|---|---|
private_key |
PGPKey
|
The unlocked PGP private key. |
passphrase |
Optional[str]
|
Passphrase used to unlock the private key. |
Initialize an Encryption instance with a private key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
private_key_armored
|
str
|
Armored representation of the PGP private key. |
required |
passphrase
|
Optional[str]
|
Passphrase to unlock the private key if it's locked. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the private key is invalid, cannot be unlocked with the passphrase, or is locked and no passphrase is provided. |
Example
decrypt ¶
Decrypt a message using the private key.
Decrypts an encrypted message and optionally verifies the signature using the sender's public key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Armored PGP message to decrypt. |
required |
public_key
|
Optional[str]
|
Optional armored public key to verify the message signature. |
None
|
Returns:
| Type | Description |
|---|---|
bytes
|
Decrypted message as bytes. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the private key cannot be unlocked, decryption fails, or signature verification fails when a public key is provided. |
Example
from human_protocol_sdk.encryption import Encryption
encryption = Encryption("-----BEGIN PGP PRIVATE KEY BLOCK-----...", "passphrase")
decrypted_message = encryption.decrypt(encrypted_message)
# Or with signature verification:
decrypted_message = encryption.decrypt(
encrypted_message,
public_key="-----BEGIN PGP PUBLIC KEY BLOCK-----..."
)
sign ¶
Sign a message with the private key.
Creates a cleartext signed message that can be verified by anyone with the corresponding public key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
Union[str, bytes]
|
Message content to sign. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Armored signed PGP message in cleartext format. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the private key cannot be unlocked or signing fails. |
sign_and_encrypt ¶
Sign and encrypt a message with recipient public keys.
Signs the message with the private key and encrypts it for all specified recipients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
Union[str, bytes]
|
Message content to sign and encrypt. |
required |
public_keys
|
List[str]
|
List of armored PGP public keys of the recipients. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Armored, signed, and encrypted PGP message. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the private key cannot be unlocked or encryption fails. |