Keeping Secrets Secret in Python
Based on an article by originally published by Jason Goldberger but unfortunately the original is not online anymore so I have rewritten a short version, since this is something I have been looking for a while.
Fernet has one classmethod called generate_key()
and two instance methods encrypt(plaintext_binary)
and decrypt(cipher_binary)
.
Step 1 : Generate a key
Generate a key and save it to the OS’s environment variables:
|
|
Will output something like 'BP04_l7C3wByNiEaEiseNiP0ZrqZ7s3qL-mkG8eHlJY='
Add the key to your environment variables
|
|
Keeping secrets as environment variables is more secure than putting them into source control.
But it’s not fool proof
Step 2 : Functions
|
|
Step 3: Use it
|
|
output something like : “gAAAAABVLTmsBrJWKMzjn4pDlRwtk1j3TFcF1lrRlXo8_ASbR42vEiAcagNKR3”
|
|
output the original string.