Python Fernet Key Generation From Password

Restart the webserver. For existing connections (the ones that you had defined before installing airflowcrypto and creating a Fernet key), you need to open each connection in the connection admin UI, re-type the password, and save the change. Nov 11, 2019  def checkkey(keyfile): if not exists(keyfile): key = Fernet.generatekey with open(keyfile, 'wb') as file: file.write(key) promptcredential This function prompts user to enter username and password, getpass gets the password from user, when user types the password the password is obscured on the console.

Python Fernet Encryption

You may wish to enroll the course by Ardit Sulce, I will not reveal the username and password on his, to be fair to him in order to use his resource for practice and learning data science you got to enroll to his course.

The code presented here has 5 parts:

  1. security.crypto, for encrypting and decrypting credentials
  2. ftp_cmd, for downloading file from ftp.
  3. init_cred, to first put in username and password for the ftp server, the json file will be returned that stores the encrypted credential filename and the key name
  4., this will execute the codes of ftp_cmd
  5. network_threads.get_files uses threading to download all files

Python Fernet Key Generation From Password Key



Contains functions of decrypting, and encrypting, this is to protect the username and password and store as encrypted text file, this is used when there is no vault available. You will notice I have been using a lot of cryptography.fernet when doing encryption and decryption due to its ease of use, fernet is a recipe that uses AES 128 bit.

This section I will break the functions down to bite size instead of putting the entire code with comments for clearer documentation.

check_key() function

This function is used to check if there is any symmetric key, if there is no encryption key yet create one, and write to a file. The caveat is if the key is lost and the key was used to encrypt the data then the data is lost forever.


This function prompts user to enter username and password, getpass() gets the password from user, when user types the password the password is obscured on the console.
This function will then returns the dictionary of the username and password.


This function read the key binary from the key file, and return the key binary.

encrypt(filename, data, key)

This function encrypts the data to a specified filename with the key generated from the fernet recipe.

def decrypt(filename, key, convert_to_json=False)

This function decrypts the data read from the encrypted file which contains the credential dictionary, the default is not to convert to json.
The binary has to be decoded with utf-8 to become a string.

Python Fernet Key Generation From Password


This function is used to get the username and password, then encrypt the data and saved as an encrypted file, this main function uses the encrypt, and check_key functions.
init_credential() function returns a dictionary of key filename and encrypted filename for future use.

Entire code in action run this first

Run this script to get the username and password from user.
The json.dump function is to convert a string data into json to a file, dump method requires a file pointer which is j and the data. is the collection of ftp command function

Currently this script only has one function for download files.

Threading to download files

This is a subclass to use threading to download files from the ftp server. runs all scripts together

This function runs all scripts together, it creates a thread from the subclass GetFilesFromFTP.

How the concurrent download looks like and how long it takes?