A pure-Python Rijndael (AES) and PBKDF2 library. Python2.7- and Python3-compatible.
Project description
Overview
This package was a remedy to there being no PyPI-published, pure-Python Rijndael (AES) implementations, and that nothing available, in general, was compatible with both Python2 and Python3. The same is true of the PBKDF2 key-expansion algorithm.
The encryptor takes a source generator (which yields individual blocks). There are source-generators provided for both data from a variable and data from a file. It is trivial if you’d like to write your own. The encryptor and decryptor functions are written as generators. Decrypted data has PKCS7 padding. A utility function is provided to trim this (trim_pkcs7_padding).
The implementation includes Python2 and Python3 implementations of both Rijndael and PBKDF2, and chooses the version when loaded.
The default block-size is 128-bits in order to be compatible with AES.
This project is also referred to as pprp, which stands for “Pure Python Rijndael and PBKDF2”.
Installation
Install via pip:
$ sudo pip install pprp
Example
Encrypt and decrypt the data, and compare the results. This example works in both Python 2 and 3.
Top imports and defines:
import sys
import io
import os.path
import hashlib
import pprp
import pprp.config
# Make the strings the right type for the current Python version.
def trans(text):
return text.encode('ASCII') if sys.version_info[0] >= 3 else text
passphrase = trans('password')
salt = trans('salt')
key_size = 32
data = "this is a test" * 100
Do the key-expansion:
key = pprp.pbkdf2(passphrase, salt, key_size)
Create a source from available data:
sg = pprp.data_source_gen(data)
Feed the source into the encryptor:
eg = pprp.rjindael_encrypt_gen(key, sg)
Feed the encryptor into the decryptor:
dg = pprp.rjindael_decrypt_gen(key, eg)
Sink the output to a variable (and automatically trim the padding):
decrypted = pprp.decrypt_sink(dg)
There is also a decrypt_to_file_sink sink that takes a file-object as the first argument.
Check the result:
assert data == decrypted.decode('ASCII')
The following is a portion of the output of the example script (test/example.py). Notice that, due to this being an efficient, generator-based design, the encryption of each block is followed by a decryption:
2014-07-01 12:24:13,182 - pprp.source - DEBUG - Yielding [data] source block: (0)-(0) 2014-07-01 12:24:13,182 - pprp.adapters - DEBUG - Encrypting and yielding encrypted block: (0) 2014-07-01 12:24:13,183 - pprp.adapters - DEBUG - Decrypting and yielding decrypted block: (0) 2014-07-01 12:24:13,183 - pprp.source - DEBUG - Yielding [data] source block: (1)-(16) 2014-07-01 12:24:13,183 - pprp.adapters - DEBUG - Encrypting and yielding encrypted block: (1) 2014-07-01 12:24:13,183 - pprp.adapters - DEBUG - Decrypting and yielding decrypted block: (1) 2014-07-01 12:24:13,183 - pprp.source - DEBUG - Yielding [data] source block: (2)-(32) 2014-07-01 12:24:13,183 - pprp.adapters - DEBUG - Encrypting and yielding encrypted block: (2) 2014-07-01 12:24:13,183 - pprp.adapters - DEBUG - Decrypting and yielding decrypted block: (2) 2014-07-01 12:24:13,184 - pprp.source - DEBUG - Yielding [data] source block: (3)-(48) 2014-07-01 12:24:13,184 - pprp.adapters - DEBUG - Encrypting and yielding encrypted block: (3) 2014-07-01 12:24:13,184 - pprp.adapters - DEBUG - Decrypting and yielding decrypted block: (3) ...
Notes
A different block-size may be passed to each of the generators. The default block-size can be changed via the PPRP_BLOCK_SIZE environment variable.
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
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 pprp-0.2.2.tar.gz.
File metadata
- Download URL: pprp-0.2.2.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0b23d44cd84552411f9b8a753f454c6d1e1ba3deb300f05bd7650294f14c287
|
|
| MD5 |
c99154a11be23bf7744255c8e6437cd8
|
|
| BLAKE2b-256 |
ef98ac005178d2f921fe60fb0eefe4bd36e51a564851f113d81834cf5ffa75f9
|
File details
Details for the file pprp-0.2.2-py2-none-any.whl.
File metadata
- Download URL: pprp-0.2.2-py2-none-any.whl
- Upload date:
- Size: 20.8 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0ac4c4d6d968cbce990ac670ca1d8a1287a01c278ccc33d1e93fe722454fd54
|
|
| MD5 |
4b5d3165d82b37189d2e6b28a8034229
|
|
| BLAKE2b-256 |
a7bd768e60a1675f882760dea3a2f4e99c2194fca0750df6ea2b83e88e913a27
|