|
| 1 | +# tokenization/token_generator.py |
| 2 | + |
| 3 | +import hashlib |
| 4 | +import os |
| 5 | +import json |
| 6 | +from cryptography.fernet import Fernet |
| 7 | +import logging |
| 8 | + |
| 9 | +# Configure logging |
| 10 | +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
| 11 | + |
| 12 | +class TokenGenerator: |
| 13 | + def __init__(self, encryption_key=None): |
| 14 | + """ |
| 15 | + Initialize the TokenGenerator with an optional encryption key. |
| 16 | + |
| 17 | + :param encryption_key: A key for encrypting and decrypting data. If None, a new key will be generated. |
| 18 | + """ |
| 19 | + if encryption_key is None: |
| 20 | + self.encryption_key = Fernet.generate_key() |
| 21 | + logging.info("Generated new encryption key.") |
| 22 | + else: |
| 23 | + self.encryption_key = encryption_key |
| 24 | + |
| 25 | + self.cipher = Fernet(self.encryption_key) |
| 26 | + |
| 27 | + def generate_token(self, data): |
| 28 | + """ |
| 29 | + Generate a unique token for the given data. |
| 30 | + |
| 31 | + :param data: The data to tokenize (e.g., biological data). |
| 32 | + :return: A token representing the data. |
| 33 | + """ |
| 34 | + # Convert data to JSON string |
| 35 | + data_json = json.dumps(data, sort_keys=True).encode() |
| 36 | + |
| 37 | + # Create a hash of the data |
| 38 | + token = hashlib.sha256(data_json).hexdigest() |
| 39 | + logging.info(f"Generated token: {token} for data: {data}") |
| 40 | + return token |
| 41 | + |
| 42 | + def encrypt_data(self, data): |
| 43 | + """ |
| 44 | + Encrypt the given data using the encryption key. |
| 45 | + |
| 46 | + :param data: The data to encrypt. |
| 47 | + :return: Encrypted data. |
| 48 | + """ |
| 49 | + data_json = json.dumps(data).encode() |
| 50 | + encrypted_data = self.cipher.encrypt(data_json) |
| 51 | + logging.info("Data encrypted successfully.") |
| 52 | + return encrypted_data |
| 53 | + |
| 54 | + def decrypt_data(self, encrypted_data): |
| 55 | + """ |
| 56 | + Decrypt the given encrypted data. |
| 57 | + |
| 58 | + :param encrypted_data: The encrypted data to decrypt. |
| 59 | + :return: Decrypted data. |
| 60 | + """ |
| 61 | + decrypted_data = self.cipher.decrypt(encrypted_data) |
| 62 | + logging.info("Data decrypted successfully.") |
| 63 | + return json.loads(decrypted_data) |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + # Example usage |
| 67 | + token_generator = TokenGenerator() |
| 68 | + |
| 69 | + # Sample biological data |
| 70 | + biological_data = { |
| 71 | + "id": "bio_001", |
| 72 | + "type": "DNA", |
| 73 | + "sequence": "ATCGTAGCTAGCTAGCTAGC", |
| 74 | + "timestamp": 1633072800 |
| 75 | + } |
| 76 | + |
| 77 | + # Generate token |
| 78 | + token = token_generator.generate_token(biological_data) |
| 79 | + |
| 80 | + # Encrypt data |
| 81 | + encrypted_data = token_generator.encrypt_data(biological_data) |
| 82 | + |
| 83 | + # Decrypt data |
| 84 | + decrypted_data = token_generator.decrypt_data(encrypted_data) |
| 85 | + |
| 86 | + print(f"Token: {token}") |
| 87 | + print(f"Encrypted Data: {encrypted_data}") |
| 88 | + print(f"Decrypted Data: {decrypted_data}") |
0 commit comments