|
| 1 | +import os |
| 2 | +import base64 |
| 3 | +import secrets |
| 4 | + |
| 5 | +from cryptography.fernet import Fernet |
| 6 | +from cryptography.hazmat.primitives.kdf.scrypt import Scrypt |
| 7 | +import cryptography |
| 8 | + |
| 9 | + |
| 10 | +class EncryptionHelper: |
| 11 | + """ |
| 12 | + A class to represent Encryption. |
| 13 | +
|
| 14 | + Methods |
| 15 | + ------- |
| 16 | + load_salt(self, filename): |
| 17 | + A method to read and return a generated salt saved in file. |
| 18 | + derive_key(self, salt, password): |
| 19 | + A method to derive key. |
| 20 | + generate_key(self, password, filename, load_existing_salt=False, save_salt=False): |
| 21 | + A method to generate key. |
| 22 | + encrypt(self, filename, key): |
| 23 | + A method to encrypt file. |
| 24 | + decrypt(self, filename, key): |
| 25 | + A method to decrypt file. |
| 26 | + """ |
| 27 | + |
| 28 | + @staticmethod |
| 29 | + def generate_salt(size: int): |
| 30 | + """ |
| 31 | + A method to generate a salt. |
| 32 | +
|
| 33 | + Parameters |
| 34 | + ---------- |
| 35 | + size : int |
| 36 | + The size of the bytes strings to be generated. |
| 37 | +
|
| 38 | + Returns |
| 39 | + ------- |
| 40 | + bytes |
| 41 | + The method returns bytes strings containing the secret token. |
| 42 | + """ |
| 43 | + |
| 44 | + return secrets.token_bytes(size) |
| 45 | + |
| 46 | + @staticmethod |
| 47 | + def load_salt(filename: str): |
| 48 | + """ |
| 49 | + A method to read and return a save salt file. |
| 50 | +
|
| 51 | + Parameters |
| 52 | + ---------- |
| 53 | + filename : str |
| 54 | + The file name to read from. |
| 55 | +
|
| 56 | + Returns |
| 57 | + ------- |
| 58 | + bytes |
| 59 | + The method returns bytes containing the salt. |
| 60 | + """ |
| 61 | + |
| 62 | + # load salt from salt file |
| 63 | + return open(filename.replace(".envs", ".salt"), "rb").read() |
| 64 | + |
| 65 | + @staticmethod |
| 66 | + def derive_key(salt: bytes, password: str): |
| 67 | + """ |
| 68 | + A method to derive a key using password and salt token. |
| 69 | +
|
| 70 | + Parameters |
| 71 | + ---------- |
| 72 | + salt : bytes |
| 73 | + The bytes strings containing the salt token. |
| 74 | + password : str |
| 75 | + The strings of characters containing the password. |
| 76 | +
|
| 77 | + Returns |
| 78 | + ------- |
| 79 | + bytes |
| 80 | + The method returns bytes string containing the derived key. |
| 81 | + """ |
| 82 | + |
| 83 | + # derive key using salt and password |
| 84 | + key = Scrypt(salt=salt, length=32, n=2**14, r=8, p=1) |
| 85 | + return key.derive(password.encode()) |
| 86 | + |
| 87 | + @staticmethod |
| 88 | + def generate_key(password: str, filename: str, load_existing_salt=False, save_salt=False): |
| 89 | + """ |
| 90 | + A method to generate key. |
| 91 | +
|
| 92 | + Parameters |
| 93 | + ---------- |
| 94 | + password : str |
| 95 | + The strings of characters containing the password. |
| 96 | + filename : str |
| 97 | + The strings of characters containing file name. |
| 98 | + load_existing_salt : bool, optional |
| 99 | + A boolean value determining existing salt exists. |
| 100 | + save_salt : bool, optional |
| 101 | + A boolean value determining save salt exists. |
| 102 | +
|
| 103 | + Returns |
| 104 | + ------- |
| 105 | + bytes |
| 106 | + The method returns bytes string containing the generated key. |
| 107 | + """ |
| 108 | + |
| 109 | + # check existing salt file |
| 110 | + if load_existing_salt: |
| 111 | + try: |
| 112 | + with open(filename.replace(".envs", ".salt"), "rb") as salt_file: |
| 113 | + salt_file.readline() |
| 114 | + except FileNotFoundError: |
| 115 | + return base64.urlsafe_b64encode(os.urandom(32)) |
| 116 | + # load existing salt |
| 117 | + salt = EncryptionHelper.load_salt(filename) |
| 118 | + if save_salt: |
| 119 | + # generate new salt/token and save it to file |
| 120 | + salt = EncryptionHelper.generate_salt(16) |
| 121 | + with open(f"{filename}.salt", "wb") as salt_file: |
| 122 | + salt_file.write(salt) |
| 123 | + |
| 124 | + # generate the key from the salt and the password |
| 125 | + derived_key = EncryptionHelper.derive_key(salt, password) |
| 126 | + # encode it using Base 64 and return it |
| 127 | + return base64.urlsafe_b64encode(derived_key) |
| 128 | + |
| 129 | + @staticmethod |
| 130 | + def encrypt(filename: str, key: bytes): |
| 131 | + """ |
| 132 | + A method to encrypt file. |
| 133 | +
|
| 134 | + Parameters |
| 135 | + ---------- |
| 136 | + filename : str |
| 137 | + The strings of characters containing file name. |
| 138 | + key : bytes |
| 139 | + A bytes of stings containing the encryption key. |
| 140 | + |
| 141 | + Returns |
| 142 | + ------- |
| 143 | + None |
| 144 | + The method returns a none value. |
| 145 | + """ |
| 146 | + |
| 147 | + fernet = Fernet(key) |
| 148 | + |
| 149 | + try: |
| 150 | + with open(filename, "rb") as file: |
| 151 | + file_data = file.read() |
| 152 | + except FileNotFoundError: |
| 153 | + print("File not found") |
| 154 | + return |
| 155 | + |
| 156 | + # encrypting file_data |
| 157 | + encrypted_data = fernet.encrypt(file_data) |
| 158 | + |
| 159 | + # writing to a new file with the encrypted data |
| 160 | + with open(f"{filename}.envs", "wb") as file: |
| 161 | + file.write(encrypted_data) |
| 162 | + |
| 163 | + print("File encrypted successfully...") |
| 164 | + return "File encrypted successfully..." |
| 165 | + |
| 166 | + @staticmethod |
| 167 | + def decrypt(filename: str, key: bytes): |
| 168 | + """ |
| 169 | + A method to decrypt file. |
| 170 | +
|
| 171 | + Parameters |
| 172 | + ---------- |
| 173 | + filename : str |
| 174 | + The strings of characters containing file name. |
| 175 | + key : bytes |
| 176 | + A bytes of stings containing the encryption key. |
| 177 | + |
| 178 | + Returns |
| 179 | + ------- |
| 180 | + None |
| 181 | + The method returns a none value. |
| 182 | + """ |
| 183 | + |
| 184 | + fernet = Fernet(key) |
| 185 | + try: |
| 186 | + with open(filename, "rb") as file: |
| 187 | + encrypted_data = file.read() |
| 188 | + except FileNotFoundError: |
| 189 | + print("File not found.") |
| 190 | + return |
| 191 | + # decrypt data using the Fernet object |
| 192 | + try: |
| 193 | + decrypted_data = fernet.decrypt(encrypted_data) |
| 194 | + except cryptography.fernet.InvalidToken: |
| 195 | + print("Invalid token, likely the password is incorrect.") |
| 196 | + return |
| 197 | + # write the original file with decrypted content |
| 198 | + with open(filename.replace(".envs", ""), "wb") as file: |
| 199 | + file.write(decrypted_data) |
| 200 | + # delete salt file after decrypting file |
| 201 | + f = open(filename.replace(".envs", ".salt"), 'w') |
| 202 | + f.close() |
| 203 | + os.remove(f.name) |
| 204 | + # delete decrypted file |
| 205 | + os.remove(filename) |
| 206 | + print("File decrypted successfully...") |
| 207 | + |
| 208 | + return "File decrypted successfully..." |
0 commit comments