| |
| |
| |
| |
| |
| |
| |
| |
|
|
| """This File Provides Cryptography.""" |
|
|
| import base64 |
| import hashlib |
| import os |
|
|
| from cryptography.hazmat.backends import default_backend |
| from cryptography.hazmat.primitives.ciphers import Cipher |
| from cryptography.hazmat.primitives.ciphers.algorithms import AES |
| from cryptography.hazmat.primitives.ciphers.modes import CFB8 |
|
|
| padding_string = b'}' |
| iv_size = AES.block_size // 8 |
|
|
|
|
| def encrypt(plaintext, key): |
| """ |
| Encrypt the plaintext with AES method. |
| |
| Parameters: |
| plaintext -- String to be encrypted. |
| key -- Key for encryption. |
| """ |
|
|
| iv = os.urandom(iv_size) |
| cipher = Cipher(AES(pad(key)), CFB8(iv), default_backend()) |
| encryptor = cipher.encryptor() |
|
|
| |
| |
| if isinstance(plaintext, str): |
| plaintext = plaintext.encode() |
|
|
| return base64.b64encode(iv + encryptor.update(plaintext) + |
| encryptor.finalize()) |
|
|
|
|
| def decrypt(ciphertext, key): |
| """ |
| Decrypt the AES encrypted string. |
| |
| Parameters: |
| ciphertext -- Encrypted string with AES method. |
| key -- key to decrypt the encrypted string. |
| """ |
|
|
| ciphertext = base64.b64decode(ciphertext) |
| iv = ciphertext[:iv_size] |
|
|
| cipher = Cipher(AES(pad(key)), CFB8(iv), default_backend()) |
| decryptor = cipher.decryptor() |
| return decryptor.update(ciphertext[iv_size:]) + decryptor.finalize() |
|
|
|
|
| def pad(key): |
| """Add padding to the key.""" |
|
|
| if isinstance(key, str): |
| key = key.encode() |
|
|
| |
| key = key[:32] |
|
|
| |
| if len(key) in (16, 24, 32): |
| return key |
|
|
| |
| return key.ljust(32, padding_string) |
|
|
|
|
| def pqencryptpassword(password, user): |
| """ |
| pqencryptpassword -- to encrypt a password |
| This is intended to be used by client applications that wish to send |
| commands like ALTER USER joe PASSWORD 'pwd'. The password need not |
| be sent in cleartext if it is encrypted on the client side. This is |
| good because it ensures the cleartext password won't end up in logs, |
| pg_stat displays, etc. We export the function so that clients won't |
| be dependent on low-level details like whether the enceyption is MD5 |
| or something else. |
| |
| Arguments are the cleartext password, and the SQL name of the user it |
| is for. |
| |
| Return value is "md5" followed by a 32-hex-digit MD5 checksum.. |
| |
| Args: |
| password: |
| user: |
| |
| Returns: |
| |
| """ |
|
|
| m = hashlib.md5() |
|
|
| |
| |
|
|
| m.update(password.encode()) |
| m.update(user.encode()) |
|
|
| return "md5" + m.hexdigest() |
|
|