Skip to content

The new styling applies starting with version 5.2.0. For earlier versions, visit legacy-sdk.humanprotocol.org.

Encryption

Encrypt, decrypt, sign, and verify messages using PGP.

Encryption

Encryption(private_key_armored, passphrase=None)

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
from human_protocol_sdk.encryption import Encryption

encryption = Encryption(
    "-----BEGIN PGP PRIVATE KEY BLOCK-----...",
    "your-passphrase"
)

decrypt

decrypt(message, public_key=None)

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(message)

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.

Example
from human_protocol_sdk.encryption import Encryption

encryption = Encryption("-----BEGIN PGP PRIVATE KEY BLOCK-----...", "passphrase")
signed_message = encryption.sign("MESSAGE")

sign_and_encrypt

sign_and_encrypt(message, public_keys)

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.

Example
from human_protocol_sdk.encryption import Encryption

encryption = Encryption("-----BEGIN PGP PRIVATE KEY BLOCK-----...", "passphrase")
encrypted_message = encryption.sign_and_encrypt(
    "your message",
    ["-----BEGIN PGP PUBLIC KEY BLOCK-----..."],
)