Skip to content

Instantly share code, notes, and snippets.

@sojohnnysaid
Last active January 19, 2021 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sojohnnysaid/a1a36586943fca7919aa8e5c1fadc24f to your computer and use it in GitHub Desktop.
Save sojohnnysaid/a1a36586943fca7919aa8e5c1fadc24f to your computer and use it in GitHub Desktop.
encoding/decoding data in python using the cryptography, dotenv libraries
import os
import json
import time
from dotenv import load_dotenv
from cryptography.fernet import Fernet
load_dotenv()
timestamp = time.time()
my_secret_key = str.encode(os.getenv('email_confirmation_secret_key'))
crypto = Fernet(my_secret_key)
user_dict_start = {'email':'john@gmail.com','first_name':'John', 'timestamp': timestamp}
json_bytearray = str.encode(json.dumps(user_dict_start))
token = crypto.encrypt(json_bytearray)
crypto = Fernet(my_secret_key)
returned_bytearray = crypto.decrypt(token)
user_dict_end = json.loads(returned_bytearray.decode())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment