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.plugins.security.password.argon2 import Argon2PasswordEncoder
from spakky.plugins.security.password.bcrypt import BcryptPasswordEncoder
from spakky.plugins.security.password.scrypt import ScryptPasswordEncoder
from spakky.plugins.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.plugins.security.cryptography.aes import Aes
from spakky.plugins.security.cryptography.gcm import Gcm
from spakky.plugins.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.plugins.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.plugins.security.jwt import JWT
from spakky.plugins.security.hmac_signer import HMACType
from spakky.plugins.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 = jwt.export()
# 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.plugins.security.hmac_signer import HMAC, HMACType
from spakky.plugins.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.plugins.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
- Use Argon2 for passwords: It's the winner of the Password Hashing Competition
- Use AES-GCM for encryption: Provides both confidentiality and integrity
- Generate secure keys: Always use
Key(size=N)for cryptographic keys - Set JWT expiration: Always set an expiration time for tokens
- Store keys securely: Use environment variables or secret managers
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spakky_security-6.3.1.tar.gz.
File metadata
- Download URL: spakky_security-6.3.1.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3208d785b6c4fd798ca17a52e849e61e41601c71bd079e57a5b14b6f607762ec
|
|
| MD5 |
e00ecdbeb89c56f705b8c58bbedb0f0b
|
|
| BLAKE2b-256 |
66f8229aa6cc05a9aa7add17c1ed6db769e6d68f9bd7cd77403d5c8c1112869a
|
Provenance
The following attestation bundles were made for spakky_security-6.3.1.tar.gz:
Publisher:
release.yml on E5presso/spakky-framework
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spakky_security-6.3.1.tar.gz -
Subject digest:
3208d785b6c4fd798ca17a52e849e61e41601c71bd079e57a5b14b6f607762ec - Sigstore transparency entry: 1236503338
- Sigstore integration time:
-
Permalink:
E5presso/spakky-framework@6d8469ad090c00a829421ab05aafc6d10178d93f -
Branch / Tag:
refs/heads/main - Owner: https://github.com/E5presso
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@6d8469ad090c00a829421ab05aafc6d10178d93f -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file spakky_security-6.3.1-py3-none-any.whl.
File metadata
- Download URL: spakky_security-6.3.1-py3-none-any.whl
- Upload date:
- Size: 23.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
97bd6caa5a9c0931ab461d7c4b5f7e0fb95909f663b0b54bbb5bd50154bc3952
|
|
| MD5 |
4bcea1ca55cdfa176a9f3f82c947b405
|
|
| BLAKE2b-256 |
af3f63cb0017266889f620cf381b43b6072eebba0b17336bafe91003ca9089f2
|
Provenance
The following attestation bundles were made for spakky_security-6.3.1-py3-none-any.whl:
Publisher:
release.yml on E5presso/spakky-framework
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spakky_security-6.3.1-py3-none-any.whl -
Subject digest:
97bd6caa5a9c0931ab461d7c4b5f7e0fb95909f663b0b54bbb5bd50154bc3952 - Sigstore transparency entry: 1236503347
- Sigstore integration time:
-
Permalink:
E5presso/spakky-framework@6d8469ad090c00a829421ab05aafc6d10178d93f -
Branch / Tag:
refs/heads/main - Owner: https://github.com/E5presso
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@6d8469ad090c00a829421ab05aafc6d10178d93f -
Trigger Event:
workflow_dispatch
-
Statement type: