Skip to main content

Fast password hashing library powered by Rust

Project description

Sarhash

Fast and secure password hashing library powered by Rust with Python bindings.

Features

  • 🚀 Blazing Fast: Rust-powered performance (via sarhash-core)
  • 🔒 Secure: Industry-standard strength
  • 🎯 Simple API: Easy-to-use Python interface
  • 💪 Type Safe: Complete type hints with .pyi stubs
  • 📊 Password Strength: Built-in strength checking
  • 🔄 Batch Operations: Efficient multi-password hashing

Installation

Prerequisites

  • Python 3.11+
  • Rust toolchain
  • uv (recommended) or pip

Step-by-Step Setup

1. Install Rust

Sarhash uses Rust for its core implementation. Install Rust using rustup:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

After installation, restart your terminal and verify:

rustc --version
cargo --version

2. Install uv

uv is a fast Python package installer and resolver.

macOS/Linux:

curl -LsSf https://astral.sh/uv/install.sh | sh

Or using pip:

pip install uv

3. Setup Project

# Create virtual environment
uv venv --python 3.11
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install maturin
uv pip install maturin

# Build and install
maturin develop --release

Quick Start

import sarhash

# Hash a password
hashed = sarhash.hash_password("my_secure_password")

# Verify a password
is_valid = sarhash.verify_password("my_secure_password", hashed)
print(is_valid)  # True

Tutorial

Basic Usage

Hashing a Password

Hash a password using the default strong algorithm:

import sarhash

password = "user_password_123"
hashed = sarhash.hash_password(password)

print(hashed)
# Output: $argon2id$v=19$m=19456,t=2,p=1$...

Verifying a Password

To verify a password against a stored hash:

import sarhash

# Stored hash from database
stored_hash = "$argon2id$v=19$m=19456,t=2,p=1$..."

# User input
user_input = "user_password_123"

# Verify
if sarhash.verify_password(user_input, stored_hash):
    print("Password is correct!")
else:
    print("Invalid password")

Password Strength Checking

Check password strength before hashing:

import sarhash

password = "MyP@ssw0rd2024"
strength = sarhash.check_password_strength(password)

print(f"Score: {strength.score}/4")
print(f"Is strong: {strength.is_strong}")
print(f"Feedback: {strength.feedback}")

Batch Operations

Hash multiple passwords efficiently:

import sarhash

passwords = ["user1_pass", "user2_pass", "user3_pass"]
hashes = sarhash.hash_multiple(passwords)

for pwd, hash in zip(passwords, hashes):
    print(f"{pwd} -> {hash[:50]}...")

API Reference

Core Functions

hash_password(password: str) -> str

Hash a password using the default algorithm.

Parameters:

  • password (str): The password to hash

Returns:

  • str: The hashed password in PHC format

Raises:

  • ValueError: If password is empty or hashing fails

verify_password(password: str, hash: str) -> bool

Verify a password against a hash.

Parameters:

  • password (str): The password to verify
  • hash (str): The hash to verify against

Returns:

  • bool: True if password matches, False otherwise

Raises:

  • ValueError: If hash format is invalid

Enhanced Features

check_password_strength(password: str) -> PasswordStrength

Check password strength with detailed feedback.

Parameters:

  • password (str): Password to evaluate

Returns:

  • PasswordStrength: Object with score (0-4), feedback (list), and is_strong property

hash_multiple(passwords: list[str]) -> list[str]

Hash multiple passwords efficiently.

Parameters:

  • passwords (list[str]): List of passwords to hash

Returns:

  • list[str]: List of hashed passwords

Development

Building a Wheel Package

To build a distributable wheel package:

maturin build --release

The wheel will be created in target/wheels/.

To install the wheel:

uv pip install target/wheels/sarhash-*.whl

Development Workflow

When developing:

  1. Make changes to Rust code in src/
  2. Rebuild: maturin develop
  3. Test your changes: python examples/basic_usage.py
  4. Run type checking: mypy python/sarhash
  5. Format code: black python/

Project Structure

.
├── Cargo.toml              # Rust package configuration
├── pyproject.toml          # Python package configuration
├── .python-version         # Python version for uv
├── src/
│   └── lib.rs             # Rust implementation binding to sarhash-core
├── python/
│   └── sarhash/
│       ├── __init__.py    # Python wrapper with enhanced features
│       ├── __init__.pyi   # Type stubs
│       └── py.typed       # PEP 561 marker
└── examples/
    └── basic_usage.py     # Usage examples

Security Best Practices

  1. Always use the latest version - Keep sarhash updated
  2. Never store plain text passwords - Always hash before storing
  3. Check password strength - Use check_password_strength() before hashing
  4. Use HTTPS - Always transmit passwords over encrypted connections
  5. Implement rate limiting - Prevent brute-force attacks

License

MIT License - see LICENSE for details.

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

sarhash-0.1.0.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

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

sarhash-0.1.0-cp311-cp311-macosx_11_0_arm64.whl (220.7 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

Details for the file sarhash-0.1.0.tar.gz.

File metadata

  • Download URL: sarhash-0.1.0.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for sarhash-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7a473d86c798e9779b50a3b13289c839c1a8cba8fa2f9d93c2bb39c968b9a482
MD5 2f7d43c9a85be1fb35f06f3e80ff1bb6
BLAKE2b-256 f3ad9da4ce938071f4aa35d219041e3bffa6ce088f30f819db95de1ffe96cfff

See more details on using hashes here.

File details

Details for the file sarhash-0.1.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sarhash-0.1.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0ef503c9a7d611c6b16ccd919d960e2082a05ed3eb21c7a8a8bb879d2e41ea87
MD5 f511e4289ecb3891bc13d9090712e9a8
BLAKE2b-256 d0b43dbd7ef6d8f55cedf93d4b7ce0e2a96ecc1ecc016964a3c2044e768196c8

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