Skip to main content

furritos

Flask-Simple-Crypt

Flask extension based on simple-crypt that allows simple, secure encryption and decryption for Python. The original module is available in pypi and github.

Overview

This Flask extension provides two functions, which encrypt and decrypt data, delegating all the hard work to the pycrypto

Dependencies

  • Python 3.7 or greater
  • Flask 2.1.0 or greater
  • PyCryptoDome 3.15.0 or greater

Install from PyPi

pip install flask-simple-crypt

Install from source

git clone https://github.com/furritos/flask-simple-crypt
python setup.py install

Usage

Simple Flask Application

For this extension to work properly, a SECRET_KEY must be defined. It is strongly suggested that one use strong key, especially when working with sensitive data.

from flask import Flask
from flask_simple_crypt import SimpleCrypt

app = Flask(__name__)
app.config['SECRET_KEY'] = "this is my key!"

cipher = SimpleCrypt()
cipher.init_app(app)

enc_data = cipher.encrypt("shhhhhhh!")
print(enc_data)  # returns base64 encoded and encrypted data

dec_data = cipher.decrypt(enc_data)
print(dec_data)  # returns original data

Performance

Both encryption and decryption are can be relatively slow. However, this is a tunable parameter. In the original version of simple-crypt, there is a fixed value of 10,000 iterations.

With flask-simple-crypt, this value is now tunable from the default of 25,000. Obviously, this needs to be adjust for acceptable performance. To override this, simply add FSC_EXPANSION_COUNT into the Flask configuration manifest:

from flask import Flask
from flask_simple_crypt import SimpleCrypt

import time

app = Flask(__name__)
app.config['SECRET_KEY'] = "this is my key!"
app.config['FSC_EXPANSION_COUNT'] = 2048

cipher = SimpleCrypt()
cipher.init_app(app)

start = time.time()
enc_data = cipher.encrypt("shhhhhhh!")
dec_data = cipher.decrypt(enc_data)
end = time.time()
print(end - start)

On an i5, 2.5 Ghz machine, this finished in about .2 seconds. With app.config['FSC_EXPANSION_COUNT'] = 20000, it finished in about 2.1 seconds. Finally, with app.config['FSC_EXPANSION_COUNT'] = 200000, it finished in about 21 seconds.

Generally, the thinking is that this lapse in processing would deter any would be attackers from programmatically brute forcing their way into the passwords. Again, tune to your liking, balancing performance with security, but be cognizant that this library is designed to make the key (the password) hard to guess (it uses a PBKDF, which can take a couple of seconds to run).

To quote the original:

In simple terms, if an attacker tries to decrypt the data by guessing passwords, then they also have to wait for a couple of seconds for each guess. This stops an attacker from trying "thousands" of different passwords every second.

So the pause on encryption and decryption is actually a sign that the library is protecting you. If this is unacceptable for your program then you may need to look for a different solution. I'm sorry, but this is the trade-off I chose when writing simple-crypt.

Algorithms

Notable exceptions from the original implementation are as follows:

  • The password is expanded to two 256 bit keys using PBKDF2 with a 256 bit random salt, SHA256, and 25,000 iterations.

  • An encrypted messages starts with a 5 byte header (fsc in ASCII followed by two bytes containing version data).

  • On top of the above mentioned encryption, the result is then base64 encoded for ease of use with databases.

  • Built against Release 4.1 of simple-crypt.

Warnings

Heed the same as the original.

Credits

Much of the work has been made possible thanks to Andrew Cooke's original work. The purpose (and focus) of this project was to flaskify it.

Metadata

Release files for Flask-Simple-Crypt 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Flask-Simple-Crypt 0.3.3
File Size Uploaded
Flask-Simple-Crypt-0.3.3.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Flask-Simple-Crypt 0.3.3
File Interpreter ABI Platform
Flask_Simple_Crypt-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 kB

Release files / Flask-Simple-Crypt-0.3.3.tar.gz

Download URL Flask-Simple-Crypt-0.3.3.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0d4033b6c9a03ac85d10f0fd213914390217dc53b2d41d153fa050fee9723594
BLAKE2b-256 checksum
How to use checksums
a7d589f14cc97347f7a3b3a017c4ea1bde3e22531a79e6a10437aeb3ca1bdc20
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release files / Flask_Simple_Crypt-0.3.3-py3-none-any.whl

Download URL Flask_Simple_Crypt-0.3.3-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
08c3fcad955ac148bb885b1de4798c1cfce8512452072beee414bacf1552e8ef
BLAKE2b-256 checksum
How to use checksums
4b6d61440a3f4a3655470a45cbf38aa0bfcffbcef1188b9ce30dc54d13a72a78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release history Release notifications | RSS feed

This release

0.3.3 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.2.9

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page