Skip to main content

A small package for safe data serialization and encryption

Project description

encr

Makes serializing safer and easier by encrypting your data with a custom password. You can get a history of all added functionalities between updates in the CHANGELOG.md file. You can get a list of features that should come in the future in the COMING_SOON.MD file.

Dependencies

This package currently depends on cryptography, which at the time of writing depends on cffi, which again depends on pycparser.

Summing up, along with this package other 3 will be installed:

  • cryptography
  • cffi
  • pycparser

Usage

The encr class: safe data encryption and serialization with custom password

Creating an encr instance

from encr import encr
e = encr("Password")

encr takes two arguments:

  • password, the string that will be hashed and converted into a key for encryption
  • clvl (optional), the compression level. Read zlib's documentation for information about that.

Changing the password mid-script: setkey

e.setkey("New password")

setkey changes the key used for serialization. It takes one argument:

  • password, the string that will be hashed and converted into a key for encryption
Warning

Remember: changing the password of an encr instance will make all previously encrypted data unusable, until you create another instance with the original password or reset the existing one with setkey.

Changing the compression level mid-script

e.clvl = 5

clvl is an attribute, which means it is a variable and it can't be called as a function. Just set e.clvl like a normal variable.

Encrypting and decrypting: dumps and loads

serialized_object = e.dumps("foobar")
print(serialized_object)

b'gAAAAABj85Nfc5tnmOSwj_wlSvFUop-E5ifYmEQUa8n2Ld5cNw5mahzkA-OpBDA9ky21OJa7vcLfheRMKA4yJppp7_ot_bWyTJ9lU5-TXCaeSJO8hzy3T7Y=' deserialized_object = e.loads(serialized_object) print(deserialized_object) foobar

dumps serializes the object. It takes one argument:

  • obj, the object to be encrypted

loads deserializes the object. It takes one argument:

  • obj, the encrypted object to be deserialized

Saving a variable to a file: dump and load

e.dump("foobar", "file.encr")
print(e.load("file.encr"))

"foobar"

dump serializes the object and saves it in a file. It takes two arguments:

  • obj, the object to be encrypted
  • file, the file where your object will be saved

load takes a serialized object from a file and deserializes it. It takes one argument:

  • file, the file where the object is stored

Encrypting a file: dumpfile and loadfile

e.dumpfile("MyFile.txt", "MyFile.encr")
e.loadfile("MyFile.encr", "MyFile.txt")

dumpfile reads a file and serializes it's content. It takes two arguments:

  • file, the file to be encrypted
  • dest, where your serialized file is saved

loadfile reads a file and deserializes it's content. It takes two arguments:

  • file, the file to be decrypted
  • dest, where your deserialized file is stored

Encrypting a folder: dumptree and loadtree

e.dumptree("MyFolder", "MyFile.encr")
e.loadtree("MyFile.encr")

dumptree reads all the files contained in a folder and in all it's subfolders and serializes both their content, name and position. It takes two arguments:

  • file, the folder to be encrypted
  • dest, the file where your serialized folder is saved

loadtree reads a file and restores the serialized tree. It takes one argument:

  • file, the file containing the tree

Changing the password: setkey

e = encr("Password")
e.setkey("Password2")

You can change the key used for encryption anytime with the setkey method.

Miscellaneous: dumpfile, dumptree and load

e.dumptree("MyFolder", "MyData.encr")
print(e.load("MyData.encr")["MyFolder\\MyFile"].decode())

You can encrypt a file with dumpfile or a tree with dumptree and access it's content with load without having to decrypt it. dumpfile saves directly the file's contents in binary mode, dumptree saves all the filepaths and the file's contents in a dictionary.

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

encr-0.1.0.1.tar.gz (17.8 kB view hashes)

Uploaded Source

Built Distribution

encr-0.1.0.1-py3-none-any.whl (16.6 kB view hashes)

Uploaded Python 3

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