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 encrypter expects a source generator (which yields individual blocks). The encrypter and decrypter 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.
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 io
import os.path
import hashlib
import pprp
# 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')
block_size = 16
key_size = 32
data = "this is a test" * 100
Do the key-expansion:
key = pprp.pbkdf2(passphrase, salt, key_size)
Define the source generator (we’ll give this to the encrypter):
def source_gen():
for i in range(0, len(data), block_size):
block = data[i:i + block_size]
len_ = len(block)
if len_ > 0:
yield block.encode('ASCII')
if len_ < block_size:
break
Connect the encryptor to the decryptor:
encrypted_gen = pprp.rjindael_encrypt_gen(key, source_gen(), block_size)
Run, sink the output into an IO stream, and trim the padding off the last block:
s = io.BytesIO()
ends_at = 0
for block in pprp.rjindael_decrypt_gen(key, encrypted_gen, block_size):
ends_at += block_size
if ends_at >= len(data):
block = pprp.trim_pkcs7_padding(block)
s.write(block)
decrypted = s.getvalue()
Check the result:
assert data == decrypted.decode('ASCII')
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.0.tar.gz.
File metadata
- Download URL: pprp-0.2.0.tar.gz
- Upload date:
- Size: 12.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
249bdd03228a04b92ace0f0c23ac7f7b2718645f561a81c2c2ab5f4806550327
|
|
| MD5 |
e7e33ee18a5aa590bfedf2d8ffde4526
|
|
| BLAKE2b-256 |
90dc8a02896f58541efb368e6e33b82e7374feb2ad6913595bb4682eade9a55b
|
File details
Details for the file pprp-0.2.0-py2-none-any.whl.
File metadata
- Download URL: pprp-0.2.0-py2-none-any.whl
- Upload date:
- Size: 18.5 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0dec05d742f66663c0a8c81798e509c445db95c52bdda2c400c1669196770960
|
|
| MD5 |
bcec3fed379cf1acd1cf1f2cc1a4a100
|
|
| BLAKE2b-256 |
2130cd62d3ec2247e9aee2454cec288fb9cb712bd61395add4bfa3c8ae0de551
|