corelibs-encryption (1.0.0)
Installation
pip install --index-url --extra-index-url https://pypi.org/simple corelibs-encryptionAbout this package
CoreLibs Encryption Module
CoreLibs Python Encryption Tools
This is part of the Python CoreLibs
Overview
Various encryption tools for python: Symmetric Encryption
Install
uv add --index opj-pypi=https://git.egplusww.jp/api/packages/PyPI/pypi/simple/ corelibs-<project_name>
Usage
The following modules exist
- corelibs_encryption.symmetric
Symmetric Encryption usage
from corelibs_encryption.symmetric import SymmetricEncryption, PackageData
SymmetricEncryption init
Init the class with a passord, if not set it will throw a ValueError
encr = SymmetricEncryption('my_password')
encrypt_with_metadata
Encry any string of data or bytes of data and returns a string, bytes or a Package of data. See for return detail below. All function will call this method. On default or invalid return_as returns as str
def encrypt_with_metadata(self, data: str | bytes, return_as: str = 'str') -> str | bytes | PackageData:
All returns hold the following information:
- encrypted_data: encrypted data, string, base64 encoded
- salt: for above encrypted data, base64 encoded
- key_hash: hash for the key to check that key given will match (sha256)
encrypt_with_metadata_return_dict
Returns a full dictionary
def encrypt_with_metadata_return_dict(self, data: str | bytes) -> PackageData:
Example:
{
"encrypted_data": "Z0FBQUFBQnBnWFA3MjBvVkNiXzFUUW4wUGotdGlIVUZJQ05FZGlQMmdQaTVadWxTSjlEaXZlR1VnVzdmSC1CM1p1MFJpQ2ZORUhJcm9DVHZxMW9jMndlcFUtVHRMMWVGeXc9PQ==",
"salt": "dzJuAn7Tyqlz-ex57hIVGA==",
"key_hash": "3a80f3c0bf778cc440db67fb91e3daa120586d61eb11194cd7fb65578aaeece4"
}
encrypt_with_metadata_return_str
Returns the json string, can be loaded with "json.loads", this is the recommended call
def encrypt_with_metadata_return_str(self, data: str | bytes) -> str:
Example:
"{\"encrypted_data\": \"Z0FBQUFBQnBnWFA3c0VyTF9KNzktQkpxdGZORkZEVGt5aDJ6bjNFWHFWMEQtUXl6c0JwY2NSaW1aUG1MZXJvRHF1MDdnWURmNEw4eVFKQU1sNFJEY19IeXVSUFI2T2pleWc9PQ==\", \"salt\": \"ble1PFIqffFvMZgX_SiWKA==\", \"key_hash\": \"af518d0d7374f04bb61d05d189fe733542f874ede3f2129bf9c7de348c47fd6e\"}"
encrypt_with_metadata_return_bytes
Returns the json string, encoded in utf-8
def encrypt_with_metadata_return_bytes(self, data: str | bytes) -> bytes:
Example:
"b'{\"encrypted_data\": \"Z0FBQUFBQnBnWFA3cFY0T1huUUxOSXRNVDZlbWZNRTh4LXp3c0NTT2laY3o1dzhRTjNQaVhQaUZFbTdRbng1aU5hME52MW9GaDc5TUZhX1dTcU5UeGU2UjBrNTVvTWhtaVE9PQ==\", \"salt\": \"UQdJ8GzPR4fzPNXXsJtrYw==\", \"key_hash\": \"28f3d5949043635ba0437be93bd1c3bbdd28c33d7b4100ebb46c44ed59aa0983\"}'"
decrypt_with_metadata
Will decrypt any package data that was encrypted with any of the above. Optional sets the password, if not set uses the class set Password.
Returns the data as a string value
def decrypt_with_metadata(self, encrypted_package: str | bytes | PackageData, password: str | None = None) -> str:
SymmetricEncryption.encrypt_data
static method for encrypt data, will return string only. for return data see "encrypt_with_metadata_return_str"
def encrypt_data(data: str | bytes, password: str) -> str:
SymmetricEncryption.decrypt_data
static method to encrypt any encrypted data from any of the above encrypt methods. Returns the data as a string
def decrypt_data(data: str | bytes | PackageData, password: str) -> str:
Development
UV setup
uv must be installed
Python venv setup
After clone, run the command below to install all dependenciss
uv sync
Build and Publish
uv build
uv publish --index opj-pypi --token <gitea token>
Python tests
All python tests are the tests/ folder. They are structured by the source folder layout
run them with
uv run pytest
Get a coverate report
uv run pytest --cov=<project>
uv run pytest --cov=<project> --cov-report=term-missing
Other tests
In the test-run folder usage and run tests are located, runt them below
uv run test-run/<script>