Skip to main content

A Python-wrapped Rust Argon2 Hasher

Project description

argon2_hasher

A minimal-surface Python extension that exposes an optimised, memory-hard Argon2id password hasher implemented in Rust


Installation

Quick-start (debug)
pip install maturin            # one-time
maturin develop                # builds & installs in debug mode
Production wheel (recommended)
# 1. Build an optimised, CPU-tuned wheel…
RUSTFLAGS="-C target-cpu=native" \
  maturin build --release --strip

# 2. Install it
pip install target/wheels/argon2_hasher-*.whl

Why two commands? maturin develop is great for dev loops but compiles without optimisations. For benchmarks or production you’ll want the --release wheel (20-50 × faster).


Usage

from argon2_hasher import Argon2Hasher

plain_pw = "correct horse battery staple"

# ① Hash ----------------------------------------------------------------
phc = Argon2Hasher.hash(plain_pw)     # '$argon2id$v=19$m=8192,t=2,p=4$…'

# ② Verify --------------------------------------------------------------
assert Argon2Hasher.verify(phc, plain_pw)          # ✅ True
assert not Argon2Hasher.verify(phc, "wrong pass")  # ❌ False

That’s it – no knobs exposed to Python; all tuning happens inside the Rust crate.


API

Function Signature Description
Argon2Hasher.hash (plain: str, salt: str | None = None) -> str Returns a PHC-encoded Argon2id hash. If salt is None, a cryptographically-secure 16-byte salt is generated.
Argon2Hasher.verify (hash: str, plain: str) -> bool Constant-time verification. Returns True if plain matches hash.

All errors are raised as ValueError (malformed hash / salt) or RuntimeError (internal hashing error).


Tests

# tests/test_argon2_hasher.py
from argon2_hasher import Argon2Hasher

def test_match():
    pw  = "my_secure_password"
    phc = Argon2Hasher.hash(pw)
    assert Argon2Hasher.verify(phc, pw)

def test_mismatch():
    phc = Argon2Hasher.hash("secret")
    assert not Argon2Hasher.verify(phc, "bad")

def test_speed():
    import time, bcrypt
    pw = "pw"
    t0 = time.perf_counter()
    Argon2Hasher.verify(Argon2Hasher.hash(pw), pw)
    argon_time = time.perf_counter() - t0

    t0 = time.perf_counter()
    bcrypt.checkpw(pw.encode(),
                   bcrypt.hashpw(pw.encode(),
                                 bcrypt.gensalt(rounds=12)))
    bcrypt_time = time.perf_counter() - t0

    print(f"argon2 {argon_time:.3f}s  / bcrypt {bcrypt_time:.3f}s")

License

argon2_hasher is released under the MIT License. See LICENSE for the full text.

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

argon2_hasher-0.1.3.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

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

argon2_hasher-0.1.3-cp311-cp311-manylinux_2_28_aarch64.whl (249.5 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ ARM64

File details

Details for the file argon2_hasher-0.1.3.tar.gz.

File metadata

  • Download URL: argon2_hasher-0.1.3.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.7

File hashes

Hashes for argon2_hasher-0.1.3.tar.gz
Algorithm Hash digest
SHA256 ea11bbfcda5c32ed5030cb4d8d9ca7f7ac8e0aa07c2698cf15c2a167a3ad88b4
MD5 afbcff6403287af9afd142a17abec349
BLAKE2b-256 1c80ff432cbadda070a4b0c0d1a754313613465be23ffddc9090cfbdb89bc4d6

See more details on using hashes here.

File details

Details for the file argon2_hasher-0.1.3-cp311-cp311-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for argon2_hasher-0.1.3-cp311-cp311-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 8ab820cad397b3ace786efd8fe6fd6a95b6e2a6590808c9137a50cf02af69d40
MD5 afd0cadf31b4150f27b84ee5b15cbfad
BLAKE2b-256 36a04aae32aeb35348fcf853bc3cc41bd35e010a9200e2cb889571893e9a62fc

See more details on using hashes here.

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