Hone logo
Hone
Problems

Secure Data Storage with Encryption in Python

This challenge focuses on implementing a secure storage mechanism in Python using encryption. Many applications need to store sensitive data (passwords, API keys, personal information) securely. This challenge will guide you in creating a simple, yet effective, encrypted storage solution using the cryptography library.

Problem Description

You are tasked with creating a Python class called SecureStorage that allows users to store and retrieve data securely. The data should be encrypted before being stored and decrypted upon retrieval. The encryption key should be generated and managed internally by the class, and not exposed to the user. The class should provide the following functionalities:

  • Initialization: The constructor should generate a random encryption key and store it securely (in memory for this challenge - a more robust solution would persist the key).
  • Store: A method to store data. The data (a string) should be encrypted using the generated key and stored in a dictionary (simulating a database).
  • Retrieve: A method to retrieve data. The data should be retrieved from the dictionary, decrypted using the key, and returned to the user.
  • Delete: A method to delete data. The data should be removed from the dictionary.

Key Requirements:

  • Use the cryptography library for encryption and decryption. Install it using pip install cryptography.
  • The encryption should be symmetric (using the same key for encryption and decryption).
  • The encryption key should be randomly generated upon initialization.
  • Error handling: The Retrieve method should raise a KeyError if the requested data is not found. The Delete method should also raise a KeyError if the data is not found.
  • Data should be stored and retrieved as strings.

Expected Behavior:

The SecureStorage class should provide a secure way to store and retrieve sensitive string data. The encryption process should prevent unauthorized access to the data if the class instance is compromised.

Edge Cases to Consider:

  • Empty data strings: The encryption and decryption process should handle empty strings gracefully.
  • Large data strings: While not a primary focus, consider the potential impact of very large data strings on performance.
  • Key management: For this challenge, the key is stored in memory. A real-world application would require a more robust key management strategy.

Examples

Example 1:

Input:
storage = SecureStorage()
storage.store("my_secret_password", "password123")
retrieved_password = storage.retrieve("my_secret_password")
print(retrieved_password)
Output:
password123
Explanation: The password "password123" is stored, encrypted, and then retrieved and decrypted successfully.

Example 2:

Input:
storage = SecureStorage()
storage.store("api_key", "abcdef123456")
try:
    storage.retrieve("nonexistent_key")
except KeyError as e:
    print(f"KeyError: {e}")
Output:
KeyError: 'nonexistent_key'
Explanation: Attempting to retrieve a non-existent key raises a KeyError as expected.

Example 3:

Input:
storage = SecureStorage()
storage.store("sensitive_data", "very important info")
storage.delete("sensitive_data")
try:
    storage.retrieve("sensitive_data")
except KeyError as e:
    print(f"KeyError: {e}")
Output:
KeyError: 'sensitive_data'
Explanation: Data is stored, then deleted.  Attempting to retrieve it after deletion raises a KeyError.

Constraints

  • The encryption key must be at least 32 bytes long.
  • Data to be stored must be a string.
  • The store, retrieve, and delete methods should execute within 0.1 seconds for data strings up to 1024 characters long. (This is a soft constraint, focusing on correctness is more important).
  • The cryptography library is the only allowed library for encryption.

Notes

  • The cryptography library provides various encryption algorithms. AES (Advanced Encryption Standard) is a good choice for this challenge.
  • Consider using a salt when encrypting data to further enhance security. While not required for this challenge, it's a good practice.
  • Focus on the core encryption and decryption logic. Error handling and edge case management are also important aspects of secure storage.
  • Remember that this is a simplified example. Real-world secure storage solutions involve more complex key management, authentication, and authorization mechanisms.
Loading editor...
python