Skip to main content

A library for creating and verifying challenges for ALTCHA.

Project description

ALTCHA Python Library

The ALTCHA Python Library is a lightweight, zero-dependency library designed for creating and verifying ALTCHA challenges, specifically tailored for Python applications.

Compatibility

This library is compatible with:

  • Python 3.9+

Example

Installation

To install the ALTCHA Python Library, use the following command:

pip install altcha

Build

python -m build

Tests

python -m unittest discover tests

Usage

Here’s a basic example of how to use the ALTCHA Python Library:

import datetime
from altcha import ChallengeOptions, create_challenge, verify_solution

def main():
    hmac_key = "secret hmac key"

    # Create a new challenge
    options = ChallengeOptions(
        expires=datetime.datetime.now() + datetime.timedelta(hours=1),
        max_number=100000, # The maximum random number
        hmac_key=hmac_key,
    )
    challenge = create_challenge(options)
    print("Challenge created:", challenge)

    # Example payload to verify
    payload = {
        "algorithm": challenge.algorithm,
        "challenge": challenge.challenge,
        "number": 12345,  # Example number
        "salt": challenge.salt,
        "signature": challenge.signature,
    }

    # Verify the solution
    ok, err = verify_solution(payload, hmac_key, check_expires=True)
    if err:
        print("Error:", err)
    elif ok:
        print("Solution verified!")
    else:
        print("Invalid solution.")

if __name__ == "__main__":
    main()

API

create_challenge(options)

Creates a new challenge for ALTCHA.

Parameters:

  • options (dict):
    • algorithm (str): Hashing algorithm to use ('SHA-1', 'SHA-256', 'SHA-512', default: 'SHA-256').
    • max_number (int): Maximum number for the random number generator (default: 1,000,000).
    • salt_length (int): Length of the random salt in bytes (default: 12).
    • hmac_key (str): Required HMAC key.
    • salt (str): Optional salt string. If not provided, a random salt will be generated.
    • number (int): Optional specific number to use. If not provided, a random number will be generated.
    • expires (datetime): Optional expiration time for the challenge.
    • params (dict): Optional URL-encoded query parameters.

Returns: Challenge

verify_solution(payload, hmac_key, check_expires)

Verifies an ALTCHA solution.

Parameters:

  • payload (dict): The solution payload to verify.
  • hmac_key (str): The HMAC key used for verification.
  • check_expires (bool): Indicates whether to validate the challenge's expiration. If set to True, the function checks the expires field within the salt (if present) to ensure the challenge has not expired. (Note: To use this feature, the expires parameter must be included when creating the challenge.)

Returns: (bool, str or None)

extract_params(payload)

Extracts URL parameters from the payload's salt.

Parameters:

  • payload (dict): The payload containing the salt.

Returns: dict

verify_fields_hash(form_data, fields, fields_hash, algorithm)

Verifies the hash of form fields.

Parameters:

  • form_data (dict): The form data to hash.
  • fields (list): The fields to include in the hash.
  • fields_hash (str): The expected hash value.
  • algorithm (str): Hashing algorithm ('SHA-1', 'SHA-256', 'SHA-512').

Returns: bool

verify_server_signature(payload, hmac_key)

Verifies the server signature.

Parameters:

  • payload (dict or str): The payload to verify (base64 encoded JSON string or dictionary).
  • hmac_key (str): The HMAC key used for verification.

Returns: (bool, ServerSignatureVerificationData, str or None)

solve_challenge(challenge, salt, algorithm, max_number, start, stop_chan)

Finds a solution to the given challenge.

Parameters:

  • challenge (str): The challenge hash.
  • salt (str): The challenge salt.
  • algorithm (str): Hashing algorithm ('SHA-1', 'SHA-256', 'SHA-512').
  • max_number (int): Maximum number to iterate to.
  • start (int): Starting number.

Returns: Solution or None

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

altcha-1.0.0.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

altcha-1.0.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file altcha-1.0.0.tar.gz.

File metadata

  • Download URL: altcha-1.0.0.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for altcha-1.0.0.tar.gz
Algorithm Hash digest
SHA256 33056826a60efdc3d9651d5a13d72d7c8ee1c7f4456b9c9db65838f47fac9082
MD5 85822b0a2f5f043d356f8da81c691239
BLAKE2b-256 fb0eec05af090d38d74e9a6ea55ce606549a4b13c3d62e340387f772ffc634e4

See more details on using hashes here.

Provenance

The following attestation bundles were made for altcha-1.0.0.tar.gz:

Publisher: ci.yml on altcha-org/altcha-lib-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file altcha-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: altcha-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for altcha-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 78cc0fd3ba907d069252e4ea47e0dd981d4d9bd41f360333cc76ca2ce2d2a46c
MD5 5c401435f14e149797f6b77343138ca4
BLAKE2b-256 8819978dac535829b3e9175427d4d91a34b0ec6e120375a38fabd9840acf32ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for altcha-1.0.0-py3-none-any.whl:

Publisher: ci.yml on altcha-org/altcha-lib-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page