-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add End-to-end encryption support to extract-events and retrieve-cale…
…ndar-file-proxy
- Loading branch information
Showing
7 changed files
with
422 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import os | ||
|
||
from cryptography.hazmat.backends import default_backend | ||
from cryptography.hazmat.primitives import hashes | ||
from cryptography.hazmat.primitives import padding | ||
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes | ||
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC | ||
|
||
|
||
def encrypt(plaintext: str, password: str) -> bytes: | ||
# Derive a key from the password | ||
salt = os.urandom(16) | ||
kdf = PBKDF2HMAC( | ||
algorithm=hashes.SHA256(), | ||
length=32, | ||
salt=salt, | ||
iterations=100000, | ||
backend=default_backend() | ||
) | ||
key = kdf.derive(password.encode()) | ||
|
||
# Generate a random 96-bit nonce | ||
nonce = os.urandom(12) | ||
|
||
# Initialize AES cipher in GCM mode | ||
cipher = Cipher(algorithms.AES(key), modes.GCM(nonce), backend=default_backend()) | ||
encryptor = cipher.encryptor() | ||
|
||
# Pad plaintext to be a multiple of the block size (128 bits for AES) | ||
padder = padding.PKCS7(algorithms.AES.block_size).padder() | ||
padded_data = padder.update(plaintext.encode()) + padder.finalize() | ||
|
||
# Encrypt the padded plaintext | ||
ciphertext = encryptor.update(padded_data) + encryptor.finalize() | ||
|
||
# Return the salt, nonce, ciphertext, and tag | ||
return salt + nonce + ciphertext + encryptor.tag | ||
|
||
|
||
def decrypt(encrypted_data: bytes, password: str) -> str: | ||
# Extract salt, nonce, ciphertext, and tag from the encrypted data | ||
salt = encrypted_data[:16] | ||
nonce = encrypted_data[16:28] | ||
tag = encrypted_data[-16:] | ||
ciphertext = encrypted_data[28:-16] | ||
|
||
# Derive the key from the password and salt | ||
kdf = PBKDF2HMAC( | ||
algorithm=hashes.SHA256(), | ||
length=32, | ||
salt=salt, | ||
iterations=100000, | ||
backend=default_backend() | ||
) | ||
key = kdf.derive(password.encode()) | ||
|
||
# Initialize AES cipher in GCM mode for decryption | ||
cipher = Cipher(algorithms.AES(key), modes.GCM(nonce, tag), backend=default_backend()) | ||
decryptor = cipher.decryptor() | ||
|
||
# Decrypt the ciphertext | ||
padded_plaintext = decryptor.update(ciphertext) + decryptor.finalize() | ||
|
||
# Unpad the plaintext | ||
unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder() | ||
plaintext = unpadder.update(padded_plaintext) + unpadder.finalize() | ||
|
||
return plaintext.decode() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.