Package: bcl
Python library that provides a simple interface for symmetric (i.e., secret-key) and asymmetric (i.e., public-key) encryption/decryption primitives.PurposeThis library provides simple and straightforward methods for symmetric (i.e., secret-key) and asymmetric (i.e., public-key) cryptographic encryption and decryption capabilities. The library’s interface is designed for ease of use and therefore hides from users some of the flexibilities and performance trade-offs that can be leveraged via direct use of the underlying cryptographic libraries.The library’s name is a reference toboron trichloride, as it is a wrapper and binding for a limited set of capabilities found inlibsodium. However, it can also be an acronym forbasic cryptographic library.Installation and UsageThis library is available as apackage on PyPI:python -m pip install bclThe library can be imported in the usual ways:import bcl
from bcl import *ExamplesThis library provides concise methods for implementingsymmetricencryption workflows:>>> from bcl import symmetric
>>> s = symmetric.secret() # Generate a secret key.
>>> c = symmetric.encrypt(s, 'abc'.encode())
>>> symmetric.decrypt(s, c).decode('utf-8')
'abc'Asymmetricencryption workflows are also supported:>>> from bcl import asymmetric
>>> s = asymmetric.secret() # Generate a secret key.
>>> p = asymmetric.public(s) # Generate a corresponding public key.
>>> c = asymmetric.encrypt(p, 'abc'.encode())
>>> asymmetric.decrypt(s, c).decode('utf-8')
'abc'This library also provides a number of classes for representingkeys(secretandpublic),nonces,plaintexts, andciphertexts. All methods expect and return instances of the appropriate classes:>>> from bcl import secret, public, cipher
>>> s = asymmetric.secret()
>>> isinstance(s, secret)
True
>>> p = asymmetric.public(s)
>>> isinstance(p, public)
True
>>> c = symmetric.encrypt(s, 'abc'.encode())
>>> type(c)
<class 'bcl.bcl.cipher'>
>>> symmetric.decrypt(bytes(s), c)
Traceback (most recent call last):
  ...
TypeError: can only decrypt using a symmetric secret key
>>> symmetric.decrypt(s, bytes(c))
Traceback (most recent call last):
  ...
TypeError: can only decrypt a ciphertextFurthermore, the above classes are derived frombytes, soall methods and other operatorssupported bybytesobjects are supported:>>> p.hex()
'0be9cece7fee92809908bd14666eab96b77deebb488c738445d842a6613b7b48'In addition, Base64 conversion methods are included for all of the above classes to support concise encoding and decoding of objects:>>> p.to_base64()
'C+nOzn/ukoCZCL0UZm6rlrd97rtIjHOERdhCpmE7e0g='
>>> b = 'C+nOzn/ukoCZCL0UZm6rlrd97rtIjHOERdhCpmE7e0g='
>>> type(public.from_base64(b))
<class 'bcl.bcl.public'>Development, Build, and Manual Installation InstructionsAll development and installation dependencies are managed usingsetuptoolsand are fully specified insetup.py. Theextras_requireparameter is used tospecify optional requirementsfor various development tasks. This makes it possible to specify additional options (such asdocs,lint, and so on) when performing installation usingpip:python -m pip install .[docs,lint]Building from SourceThe library can be built manually from sourcewithin Linux and macOSusing the sequence of commands below:python -m pip install .[build]
python setup.py bdist_wheelDeveloping the library further in a local environment and/or building the library from source requireslibsodium. The steppython setup.py bdist_wheelin the above attempts to automatically locate a copy of the libsodium source archivesrc/bcl/libsodium.tar.gz. If the archive corresponding to the operating system is not found, the build process attempts to download it. To support building offline, it is necessary to first download the appropriate libsodium archive to its designated location:wget -O src/bcl/libsodium.tar.gz https://github.com/jedisct1/libsodium/releases/download/1.0.18-RELEASE/libsodium-1.0.18.tar.gzThe process for building manually from source within a Windows environment is not currently documented, but an example of one sequence of steps can be found in the Windows job entry within the GitHub Actions workflow defined in the file.github/workflows/lint-test-cover-docs-build-upload.yml.Preparation for Local DevelopmentBeforedocumentation can be generatedortests can be executed, it is necessary torun the build processand then to use the command below to move the compiled libsodium shared/dynamic library file into its designated location (so that the module filesrc/bcl/bcl.pyis able to import it):cp build/lib*/bcl/_sodium*.* src/bclManual InstallationOnce the package isbuilt, it can be installed manually using the command below:python -m pip install -f dist . --upgradeDocumentationOnce the libsodium shared library file is compiled and moved into its designated location (as described inthe relevant subsection above), the documentation can be generated automatically from the source files usingSphinx:python -m pip install .[docs]
cd docs
sphinx-apidoc -f -E --templatedir=_templates -o _source .. ../setup.py ../src/bcl/sodium_ffi.py && make htmlTesting and ConventionsBefore unit tests can be executed, it is first necessary to prepare for local development by compiling and moving into its designated location the libsodium shared library file (as described inthe relevant subsection above).All unit tests are executed and their coverage is measured when usingpytest(seepyproject.tomlfor configuration details):python -m pip install .[test]
python -m pytestAlternatively, all unit tests are included in the module itself and can be executed usingdoctest:python src/bcl/bcl.py -vStyle conventions are enforced usingPylint:python -m pip install .[lint]
python -m pylint src/bclContributionsIn order to contribute to the source code, open an issue or submit a pull request on theGitHub pagefor this library.VersioningThe version number format for this library and the changes to the library associated with version number increments conform withSemantic Versioning 2.0.0.PublishingThis library can be published as apackage on PyPIby a package maintainer. First, install the dependencies required for packaging and publishing:python -m pip install .[publish]Ensure that the correct version number appears insetup.pyand in.github/workflows/lint-test-cover-docs-build-upload.yml, and that any links in this README document to the Read the Docs documentation of this package (or its dependencies) have appropriate version numbers. Also ensure that the Read the Docs project for this library has anautomation rulethat activates and sets as the default all tagged versions. Create and push a tag for this version (replacing?.?.?with the version number):git tag ?.?.?
git push origin ?.?.?Remove any old build/distribution files. Then, package the source into a distribution archive:rm -rf build dist src/*.egg-info
python setup.py sdistNext, navigate to the appropriate GitHub Actions run of the workflow defined inlint-test-cover-docs-build-upload.yml. Click on the workflow and scroll down to theArtifactspanel. Download the archive files to thedistdirectory. Unzip all the archive files so that only the*.whlfiles remain:cd dist && for i in `ls *.zip`; do unzip $i; done && rm *.zip && cd ..Finally, upload the package distribution archive toPyPI:python -m twine upload dist/*