Skip to content

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

Encryption Utils

Utility helpers for PGP encryption tasks.

EncryptionUtils

Utility class providing static methods for PGP encryption operations.

This class offers helper methods for encrypting messages, verifying signatures, extracting signed data, and checking message encryption status without requiring a private key instance.

encrypt staticmethod

encrypt(message, public_keys)

Encrypt a message using recipient public keys.

Encrypts a message so that only holders of the corresponding private keys can decrypt it. Does not sign the message.

Parameters:

Name Type Description Default
message str

Plain text message to encrypt.

required
public_keys List[str]

List of armored PGP public keys of the recipients.

required

Returns:

Type Description
str

Armored encrypted PGP message.

Raises:

Type Description
PGPError

If encryption fails or public keys are invalid.

Example
from human_protocol_sdk.encryption import EncryptionUtils

encrypted_message = EncryptionUtils.encrypt(
    "MESSAGE",
    ["-----BEGIN PGP PUBLIC KEY BLOCK-----..."],
)

get_signed_data staticmethod

get_signed_data(message)

Extract the signed data from an armored signed message.

Retrieves the original message content from a PGP signed message without verifying the signature.

Parameters:

Name Type Description Default
message str

Armored PGP signed message.

required

Returns:

Type Description
str

Extracted message content, or False if extraction fails.

Example
from human_protocol_sdk.encryption import EncryptionUtils

original_message = EncryptionUtils.get_signed_data(signed_message)

is_encrypted staticmethod

is_encrypted(message)

Check whether a message is armored and encrypted.

Determines if the provided text is a valid PGP encrypted message by checking the message header.

Parameters:

Name Type Description Default
message str

Text to check for encryption.

required

Returns:

Type Description
bool

True if the message is a PGP encrypted message, False otherwise.

Example
from human_protocol_sdk.encryption import EncryptionUtils

if EncryptionUtils.is_encrypted(some_text):
    print("Message is encrypted")

verify staticmethod

verify(message, public_key)

Verify the signature of a message.

Checks if a signed message has a valid signature from the holder of the private key corresponding to the provided public key.

Parameters:

Name Type Description Default
message str

Armored PGP message to verify.

required
public_key str

Armored PGP public key to verify the signature against.

required

Returns:

Type Description
bool

True if the signature is valid, False otherwise.

Example
from human_protocol_sdk.encryption import EncryptionUtils

is_valid = EncryptionUtils.verify(
    signed_message,
    "-----BEGIN PGP PUBLIC KEY BLOCK-----..."
)