Skip to main content

Encrypt and decrypt Python objects with just one line of code! 🔥🔐🕵️

Project description

FriendlyCrypto

A simple Python module that uses the Fernet secure algorithm to encrypt you Python objects with just one line of code.

Installation

Simply install using the Python package manager:

pip install friendlycrypto

Usage

Encrypting bytes

from friendlycrypto import Cryptographer

with open('img.png', 'rb') as original_f:
    # Loads some file in a 'read-bytes' mode.
    original_data = original_f.read()

# Encoding the password string into a bytes-string.
key = input("Key for encryption: ").encode('utf8')

# Encrypt the data usaing the 'Cryptographer' object
grapher = Cryptographer()
encrypted = grapher.encrypt(original_data, key)

with open('img.png.encrypted', 'wb') as encrypted_f:
    # Write the encrypted data back into a new file.
    encrypted_f.write(encrypted)

Encrypting Python objects

from friendlycrypto import FriendlyCryptographer

# The data can be any Python object!
# For this example, we are using a simple dict with strings.
data = {
    'user1': 'password',
    'user2': 'another-password',
}

# Encoding the password string into a bytes-string.
key = input("Key for encryption: ").encode('utf8')

# Encrypt the data usaing the 'FriendlyCryptographer' object
grapher = FriendlyCryptographer()
encrypted = grapher.encrypt(data, key)

with open('data.encrypted', 'wb') as encrypted_f:
    # Write the encrypted data back into a new file.
    encrypted_f.write(encrypted)

Additional arguments

The Cryptographer and FriendlyCryptographer objects share an init method with two additional arguments:

salt: bytes

Should be a bytes-string (recommended to be at least 16 bytes long). Those bytes are added to the encrypted data to add additional randomness and uniqueness to your database. The salt shouldn't be stored with the database. generate using the os.urandom function.

Read more about the salt in hashing.

kdf_iterations: int

The module uses a key derivation function to convert the given password bytes into a fixed-length bytes string. Each iteration of the function takes time to compute, and thus a larger number of iterations makes it harder for attackers to just guess every option.

The recommended and default value is 100_000, and on my computer it takes approximately 0.1 seconds to compute.

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

friendlycrypto-1.0.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

friendlycrypto-1.0.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file friendlycrypto-1.0.0.tar.gz.

File metadata

  • Download URL: friendlycrypto-1.0.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.4.0 pkginfo/1.7.0 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.2

File hashes

Hashes for friendlycrypto-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9611c65c11e705190e6a924f821536ea5d41a4f6c97344362d34856fe566971f
MD5 714171a63bf067d1b3f3fd93c831bd40
BLAKE2b-256 7c87caa18ce2effda5acde727d801d0a06bdb18126aa45a3659b96179bc82815

See more details on using hashes here.

File details

Details for the file friendlycrypto-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: friendlycrypto-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.4.0 pkginfo/1.7.0 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.2

File hashes

Hashes for friendlycrypto-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 48ee69dde3b362802a231cd7c879c0b6881a64394760188b788364ada23b88ed
MD5 76ccb372cc103c10edc38eb084970556
BLAKE2b-256 510246121f1ace7122cfc42faad7c12404816db4602c3b710d16651c3008497f

See more details on using hashes here.

Supported by

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