This repository has been archived by the owner on Aug 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
128 additions
and
9 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
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 @@ | ||
�k���Bڂ��"S EsgAAAAABlaDR2T-uPvCsPQbKFi6vi4fQ9n7tUtrToOTGkzhuV02K3LierP6epbEnUnuAB2eUgK0ltJWnzT1nArxSHJxl6GqTcgcM7eLU-_6HrAZXpa9PxoVrnV_OThtSFd5if1k0xPHwFIiwTgoJhG_bARwKTnGJgvw== |
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 |
---|---|---|
|
@@ -12,4 +12,5 @@ dependencies: | |
- pytest-mock | ||
- python-dotenv | ||
- colorlog | ||
- tzlocal | ||
- tzlocal | ||
- cryptography |
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,81 @@ | ||
import base64 | ||
import os | ||
import argparse | ||
from cryptography.hazmat.primitives import hashes | ||
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC | ||
from cryptography.fernet import Fernet | ||
from cryptography.hazmat.backends import default_backend | ||
|
||
|
||
def derive_key(passphrase, salt=None): | ||
"""Derives a key using the given passphrase and salt.""" | ||
if salt is None: | ||
salt = os.urandom(16) # Generate a new salt for encryption | ||
# Key Derivation Function | ||
kdf = PBKDF2HMAC( | ||
algorithm=hashes.SHA256(), | ||
length=32, | ||
salt=salt, | ||
iterations=100000, | ||
backend=default_backend(), | ||
) | ||
key = base64.urlsafe_b64encode(kdf.derive(passphrase.encode())) | ||
return key, salt | ||
|
||
|
||
def encrypt_data(data, passphrase): | ||
key, salt = derive_key(passphrase) | ||
cipher_suite = Fernet(key) | ||
encrypted_data = cipher_suite.encrypt(data.encode()) | ||
return salt + encrypted_data # Prepend salt to encrypted data | ||
|
||
|
||
def decrypt_data(encrypted_data_with_salt, passphrase): | ||
salt = encrypted_data_with_salt[:16] # Extract the salt | ||
encrypted_data = encrypted_data_with_salt[16:] | ||
key, _ = derive_key(passphrase, salt) | ||
cipher_suite = Fernet(key) | ||
return cipher_suite.decrypt(encrypted_data) | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description="Encrypt/Decrypt data.") | ||
parser.add_argument( | ||
"--mode", | ||
choices=["encrypt", "decrypt"], | ||
required=True, | ||
help="Operation mode: encrypt or decrypt", | ||
) | ||
parser.add_argument( | ||
"--input", required=True, help="Input data to encrypt or path to input file" | ||
) | ||
parser.add_argument("--output", required=True, help="Output file path") | ||
parser.add_argument( | ||
"--passphrase", required=True, help="Passphrase for encryption/decryption" | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
if args.mode == "encrypt": | ||
try: | ||
# Attempt to open the input as a file | ||
with open(args.input, "r") as file: | ||
data = file.read() | ||
except FileNotFoundError: | ||
# If file not found, treat input as a raw string | ||
data = args.input | ||
|
||
encrypted_data = encrypt_data(data, args.passphrase) | ||
with open(args.output, "wb") as file: | ||
file.write(encrypted_data) | ||
|
||
elif args.mode == "decrypt": | ||
with open(args.input, "rb") as file: | ||
encrypted_data_with_salt = file.read() | ||
decrypted_data = decrypt_data(encrypted_data_with_salt, args.passphrase) | ||
with open(args.output, "wb") as file: | ||
file.write(decrypted_data) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
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,16 @@ | ||
import pytest | ||
from crypto_script import encrypt_data, decrypt_data | ||
|
||
|
||
def test_encrypt_decrypt(): | ||
original_string = "Test String" | ||
passphrase = "StrongPassphrase" | ||
|
||
# Encrypt the string | ||
encrypted_data = encrypt_data(original_string, passphrase) | ||
|
||
# Decrypt the data | ||
decrypted_string = decrypt_data(encrypted_data, passphrase).decode() | ||
|
||
# Assert that the decrypted string matches the original | ||
assert decrypted_string == original_string |