LightDSA
LightDSA (version 0.0.3) is a lightweight Python library providing Digital Signature Algorithm functionalities. Currently, it primarily supports the RSA algorithm for generating keys, signing data, and verifying signatures. Its development appears to be inactive since 2021, and new releases are infrequent.
Common errors
-
ModuleNotFoundError: No module named 'Crypto'
cause The `lightdsa` library uses `pycryptodome` internally, which often exposes its modules under the `Crypto` namespace. This error indicates `pycryptodome` is not installed.fixInstall the `pycryptodome` package: `pip install pycryptodome`. -
TypeError: object of type 'str' has no len()
cause Cryptographic functions for signing and verification expect byte-like objects (e.g., `bytes`), not strings. You are likely passing a Python `str` directly.fixConvert your string data to bytes before passing it to `sign()` or `verify()`. For example, `my_string.encode('utf-8')`.
Warnings
- gotcha The LightDSA library appears to be unmaintained. The last commit on GitHub was in 2021, and the version 0.0.3 suggests early development. Users should be aware that it may not receive future updates or security fixes.
- gotcha Despite its name, LightDSA currently only supports the RSA algorithm for digital signatures. The `key_size` parameter is specifically for RSA key lengths.
- gotcha LightDSA relies on the `pycryptodome` library. Incorrect or incomplete installation of `pycryptodome` (which might require C build tools on some systems) can lead to runtime errors.
Install
-
pip install lightdsa
Imports
- LightDSA
from lightdsa import LightDSA
Quickstart
from lightdsa import LightDSA
# 1. Generate keys
# For RSA, key_size must be between 1024 and 4096 (inclusive)
# and a multiple of 256.
dsa = LightDSA(key_size=2048) # Default is 2048
private_key_pem = dsa.private_key_to_pem().decode('utf-8')
public_key_pem = dsa.public_key_to_pem().decode('utf-8')
print("Private Key (PEM format):")
print(private_key_pem)
print("\nPublic Key (PEM format):")
print(public_key_pem)
# 2. Sign data
message_to_sign = "This is a secret message."
signature = dsa.sign(message_to_sign.encode('utf-8'))
print(f"\nMessage: {message_to_sign}")
print(f"Signature: {signature.hex()}")
# 3. Verify signature using the current object's keys
is_valid = dsa.verify(message_to_sign.encode('utf-8'), signature)
print(f"Signature valid: {is_valid}")
# You can also load keys from PEM strings into a new LightDSA instance
dsa_loaded = LightDSA(key_size=2048) # key_size required for initialization
dsa_loaded.load_public_key_from_pem(public_key_pem.encode('utf-8'))
dsa_loaded.load_private_key_from_pem(private_key_pem.encode('utf-8')) # Optional for verification
is_valid_loaded = dsa_loaded.verify(message_to_sign.encode('utf-8'), signature)
print(f"Signature valid (loaded keys): {is_valid_loaded}")