Skip to main content

Flask extension based on simple-crypt that allows simple, secure encryption and decryption for Python.

Project description

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.

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

Flask-Simple-Crypt-0.3.3.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Flask_Simple_Crypt-0.3.3-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file Flask-Simple-Crypt-0.3.3.tar.gz.

File metadata

  • Download URL: Flask-Simple-Crypt-0.3.3.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for Flask-Simple-Crypt-0.3.3.tar.gz
Algorithm Hash digest
SHA256 0d4033b6c9a03ac85d10f0fd213914390217dc53b2d41d153fa050fee9723594
MD5 c95a761a59b50059063d9154b7e20286
BLAKE2b-256 a7d589f14cc97347f7a3b3a017c4ea1bde3e22531a79e6a10437aeb3ca1bdc20

See more details on using hashes here.

File details

Details for the file Flask_Simple_Crypt-0.3.3-py3-none-any.whl.

File metadata

File hashes

Hashes for Flask_Simple_Crypt-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 08c3fcad955ac148bb885b1de4798c1cfce8512452072beee414bacf1552e8ef
MD5 542fa20e3e19e7df141e3a87e99de435
BLAKE2b-256 4b6d61440a3f4a3655470a45cbf38aa0bfcffbcef1188b9ce30dc54d13a72a78

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page