Skip to content

Commit

Permalink
Set default buffer size
Browse files Browse the repository at this point in the history
  • Loading branch information
marcobellaccini committed May 9, 2021
1 parent 727b1f9 commit 2448d3a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
5 changes: 3 additions & 2 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
History
==================

Next release
6.0.0 (May 2021)
~~~~~~~~~~~~~~~~~~
* Updated password complexity check
* Updated password complexity check
* Set a default buffer size

5.0.0 (Gen 2021)
~~~~~~~~~~~~~~~~~~
Expand Down
26 changes: 19 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,37 @@ Here is an example showing encryption and decryption of a file:
.. code:: python
import pyAesCrypt
# encryption/decryption buffer size - 64K
bufferSize = 64 * 1024
password = "foopassword"
password = "please-use-a-long-and-random-password"
# encrypt
pyAesCrypt.encryptFile("data.txt", "data.txt.aes", password, bufferSize)
pyAesCrypt.encryptFile("data.txt", "data.txt.aes", password)
# decrypt
pyAesCrypt.decryptFile("data.txt.aes", "dataout.txt", password, bufferSize)
pyAesCrypt.decryptFile("data.txt.aes", "dataout.txt", password)
**This is the most straightforward way to use pyAesCrypt, and should be preferred.**

If you need to specify a custom buffer size (default is 64KB), you can pass it as an optional argument:

.. code:: python
import pyAesCrypt
# custom encryption/decryption buffer size (default is 64KB)
bufferSize = 128 * 1024
password = "please-use-a-long-and-random-password"
# encrypt
pyAesCrypt.encryptFile("data.txt", "data.txt.aes", password, bufferSize)
# decrypt
pyAesCrypt.decryptFile("data.txt.aes", "dataout.txt", password, bufferSize)
In case you need it, you can work with binary streams too:

.. code:: python
import pyAesCrypt
from os import stat, remove
# encryption/decryption buffer size - 64K
# with stream-oriented functions, setting buffer size is mandatory
bufferSize = 64 * 1024
password = "foopassword"
password = "please-use-a-long-and-random-password"
# encrypt
with open("data.txt", "rb") as fIn:
Expand Down Expand Up @@ -73,7 +85,7 @@ you can also perform in-memory encryption/decryption (using BytesIO):
import io
bufferSize = 64 * 1024
password = "foopassword"
password = "please-use-a-long-and-random-password"
# binary data to be encrypted
pbdata = b"This is binary plaintext \x00\x01"
Expand Down
4 changes: 4 additions & 0 deletions THANKS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Thanks

* Thanks to `Dead4W`_ for improving CI.

* Thanks to `MySixSenses`_ for pointing out that there was no default buffer size.

.. _Ben Fisher: https://downpoured.github.io/

.. _Thomas O'Neill: https://github.com/toneill818
Expand All @@ -19,3 +21,5 @@ Thanks
.. _sander76: https://github.com/sander76

.. _Dead4W: https://github.com/Dead4W

.. _MySixSenses: https://github.com/MySixSenses
16 changes: 9 additions & 7 deletions pyAesCrypt/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
from os import stat, remove, path

# pyAesCrypt version - now semver
version = "5.0.0"
version = "6.0.0"

# encryption/decryption buffer size - 64K
bufferSize = 64 * 1024
# default encryption/decryption buffer size - 64KB
bufferSizeDef = 64 * 1024

# maximum password length (number of chars)
maxPassLen = 1024
Expand Down Expand Up @@ -74,11 +74,12 @@ def stretch(passw, iv1):
# infile: plaintext file path
# outfile: ciphertext file path
# passw: encryption password
# bufferSize: encryption buffer size, must be a multiple of
# bufferSize: optional buffer size, must be a multiple of
# AES block size (16)
# using a larger buffer speeds up things when dealing
# with big files
def encryptFile(infile, outfile, passw, bufferSize):
# Default is 64KB.
def encryptFile(infile, outfile, passw, bufferSize = bufferSizeDef):
try:
with open(infile, "rb") as fIn:
# check that output file does not exist
Expand Down Expand Up @@ -241,10 +242,11 @@ def encryptStream(fIn, fOut, passw, bufferSize):
# infile: ciphertext file path
# outfile: plaintext file path
# passw: encryption password
# bufferSize: decryption buffer size, must be a multiple of AES block size (16)
# bufferSize: optional buffer size, must be a multiple of AES block size (16)
# using a larger buffer speeds up things when dealing with
# big files
def decryptFile(infile, outfile, passw, bufferSize):
# Default is 64KB.
def decryptFile(infile, outfile, passw, bufferSize = bufferSizeDef):
try:
with open(infile, "rb") as fIn:
# check that output file does not exist
Expand Down
10 changes: 10 additions & 0 deletions pyAesCrypt/test_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ def test_enc_pyAesCrypt_dec_pyAesCrypt(self):
pyAesCrypt.decryptFile(ct, ou, password, bufferSize)
# check that the original file and the output file are equal
self.assertTrue(filecmp.cmp(pt, ou))

# test pyAesCrypt encryption/decryption with default buffer size
def test_enc_pyAesCrypt_dec_pyAesCrypt_defbufsize(self):
for pt, ct, ou in zip(filenames, encfilenames, decfilenames):
# encrypt file
pyAesCrypt.encryptFile(pt, ct, password)
# decrypt file
pyAesCrypt.decryptFile(ct, ou, password)
# check that the original file and the output file are equal
self.assertTrue(filecmp.cmp(pt, ou))

# test encryption with pyAesCrypt and decryption with AES Crypt
def test_enc_pyAesCrypt_dec_AesCrypt(self):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
README = readme.read()

setup(name='pyAesCrypt',
version='5.0.0',
version='6.0.0',
packages = find_packages(),
include_package_data=True,
description='Encrypt and decrypt files and streams in AES Crypt format (version 2)',
Expand Down

0 comments on commit 2448d3a

Please sign in to comment.