Skip to main content

Security utilities plugin for Spakky framework

Project description

Spakky Security

Security utilities plugin for Spakky Framework.

Installation

pip install spakky-security

Or install via Spakky extras:

pip install spakky[security]

Features

  • Password Hashing: Argon2, bcrypt, scrypt, PBKDF2
  • Symmetric Encryption: AES-CBC, AES-GCM
  • Asymmetric Encryption: RSA
  • JWT Tokens: Create, sign, verify, and parse JWT tokens
  • HMAC Signing: Secure message authentication
  • Key Generation: Cryptographically secure random keys

Usage

Password Hashing

from spakky_security.password.argon2 import Argon2PasswordEncoder
from spakky_security.password.bcrypt import BcryptPasswordEncoder
from spakky_security.password.scrypt import ScryptPasswordEncoder
from spakky_security.password.pbkdf2 import Pbkdf2PasswordEncoder

# Argon2 (recommended)
encoder = Argon2PasswordEncoder(password="my_password")
hashed = encoder.encode()  # Returns formatted hash string

# Verify password
encoder_verify = Argon2PasswordEncoder(password_hash=hashed)
is_valid = encoder_verify.verify("my_password")

# bcrypt
bcrypt_encoder = BcryptPasswordEncoder(password="my_password")
hashed = bcrypt_encoder.encode()

# scrypt
scrypt_encoder = ScryptPasswordEncoder(password="my_password")
hashed = scrypt_encoder.encode()

# PBKDF2
pbkdf2_encoder = Pbkdf2PasswordEncoder(password="my_password")
hashed = pbkdf2_encoder.encode()

Symmetric Encryption (AES)

from spakky_security.cryptography.aes import Aes
from spakky_security.cryptography.gcm import Gcm
from spakky_security.key import Key

# Generate a 256-bit key
key = Key(size=32)

# AES-CBC
aes = Aes(key)
encrypted = aes.encrypt("Hello, World!")
decrypted = aes.decrypt(encrypted)  # "Hello, World!"

# AES-GCM (authenticated encryption)
gcm = Gcm(key)
encrypted = gcm.encrypt("Hello, World!")
decrypted = gcm.decrypt(encrypted)  # "Hello, World!"

Asymmetric Encryption (RSA)

from spakky_security.cryptography.rsa import Rsa, AsymmetricKey

# Generate RSA key pair (supports 1024, 2048, 4096, 8192 bits)
asymmetric_key = AsymmetricKey(size=2048)
rsa = Rsa(key=asymmetric_key)

# Encrypt with public key
encrypted = rsa.encrypt("Secret message")

# Decrypt with private key
decrypted = rsa.decrypt(encrypted)  # "Secret message"

# Export keys
public_key = asymmetric_key.public_key
private_key = asymmetric_key.private_key  # Returns Key or None

# Import from PEM (passphrase optional)
imported_key = AsymmetricKey(key=private_key_pem, passphrase="optional")
rsa_imported = Rsa(key=imported_key)

JWT Tokens

from spakky_security.jwt import JWT
from spakky_security.hmac_signer import HMACType
from spakky_security.key import Key
from datetime import timedelta

# Create a JWT
jwt = JWT()
jwt.set_payload(user_id=123, role="admin")
jwt.set_expiration(timedelta(hours=1))

# Sign the token (default: HS256)
key = Key(size=32)
jwt.sign(key)
token_string = str(jwt)

# Use different hash algorithm
jwt.set_hash_type(HMACType.HS512)
jwt.sign(key)

# Parse and verify a token
parsed_jwt = JWT(token=token_string)
is_valid = parsed_jwt.verify(key)

# Access claims
user_id = parsed_jwt.payload.get("user_id")
is_expired = parsed_jwt.is_expired

HMAC Signing

from spakky_security.hmac_signer import HMAC, HMACType
from spakky_security.key import Key

key = Key(size=32)

# Sign a message (static method)
signature = HMAC.sign_text(key, HMACType.HS256, "message to sign")

# URL-safe signature
signature_safe = HMAC.sign_text(key, HMACType.HS256, "message", url_safe=True)

# Verify signature (static method)
is_valid = HMAC.verify(key, HMACType.HS256, "message to sign", signature)

Key Generation

from spakky_security.key import Key

# Generate random key
key = Key(size=32)  # 256-bit key

# Access key data
raw_bytes = key.binary
base64_encoded = key.b64
url_safe_base64 = key.b64_urlsafe
hex_encoded = key.hex

# Create key from existing data
key_from_bytes = Key(binary=existing_bytes)
key_from_base64 = Key(base64=encoded_string)

Components

Component Description
Argon2PasswordEncoder Argon2 password hashing (recommended)
BcryptPasswordEncoder bcrypt password hashing
ScryptPasswordEncoder scrypt password hashing
Pbkdf2PasswordEncoder PBKDF2 password hashing
Aes AES-CBC encryption/decryption
Gcm AES-GCM authenticated encryption
Rsa RSA asymmetric encryption
JWT JSON Web Token creation and validation
HMAC HMAC message signing and verification
Key Secure key generation and management

Security Best Practices

  1. Use Argon2 for passwords: It's the winner of the Password Hashing Competition
  2. Use AES-GCM for encryption: Provides both confidentiality and integrity
  3. Generate secure keys: Always use Key(size=N) for cryptographic keys
  4. Set JWT expiration: Always set an expiration time for tokens
  5. Store keys securely: Use environment variables or secret managers

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

spakky_security-4.0.0.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

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

spakky_security-4.0.0-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file spakky_security-4.0.0.tar.gz.

File metadata

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

File hashes

Hashes for spakky_security-4.0.0.tar.gz
Algorithm Hash digest
SHA256 4603510ba8819dd951283905717626761375ce0550c3183f4663d404bae78842
MD5 76686b16b8850fce6bee08c2b46ae22a
BLAKE2b-256 e7c386ce03cd303baf99c7f84cc396f29ab84cd5a8c9ccceca6f0bee395408b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for spakky_security-4.0.0.tar.gz:

Publisher: release.yml on E5presso/spakky-framework

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

File details

Details for the file spakky_security-4.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for spakky_security-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fdb93191fcbe86d114106f8674d0012d9bb25d3f01d02d09dcddf82a9924605f
MD5 7206bdd8a488f0c425143aa804f9c1fe
BLAKE2b-256 110cc4db2e08687cf57970711d2ff7711bbf1dd216c9fdc401b2567765bc2c53

See more details on using hashes here.

Provenance

The following attestation bundles were made for spakky_security-4.0.0-py3-none-any.whl:

Publisher: release.yml on E5presso/spakky-framework

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