Spaces:
Running
Running
benediktstroebl
commited on
Commit
·
45c55a7
1
Parent(s):
c03c7bc
added encrypted uploads
Browse files- utils/encrypt.py +64 -0
- utils/json_encryption.py +168 -0
utils/encrypt.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import zipfile
|
| 3 |
+
import json
|
| 4 |
+
from json_encryption import JsonEncryption
|
| 5 |
+
from tqdm import tqdm
|
| 6 |
+
from huggingface_hub import HfApi
|
| 7 |
+
|
| 8 |
+
# Create an instance with your password
|
| 9 |
+
encryptor = JsonEncryption("hal1234")
|
| 10 |
+
|
| 11 |
+
# Create encrypted_files directory if it doesn't exist
|
| 12 |
+
encrypted_dir = "encrypted_files"
|
| 13 |
+
if not os.path.exists(encrypted_dir):
|
| 14 |
+
os.makedirs(encrypted_dir)
|
| 15 |
+
|
| 16 |
+
# Get all JSON files from evals_live directory
|
| 17 |
+
json_files = [f for f in os.listdir("evals_live") if f.endswith(".json")]
|
| 18 |
+
|
| 19 |
+
if not json_files:
|
| 20 |
+
print("No files found to encrypt and upload")
|
| 21 |
+
exit()
|
| 22 |
+
|
| 23 |
+
# Initialize Hugging Face API
|
| 24 |
+
api = HfApi(token=os.getenv("HF_TOKEN"))
|
| 25 |
+
|
| 26 |
+
# Process each file
|
| 27 |
+
for json_file in tqdm(json_files, desc="Encrypting files", unit="file"):
|
| 28 |
+
input_path = os.path.join("evals_live", json_file)
|
| 29 |
+
|
| 30 |
+
# Read the JSON to get the run_id
|
| 31 |
+
with open(input_path) as f:
|
| 32 |
+
data = json.load(f)
|
| 33 |
+
run_id = data["config"]["run_id"]
|
| 34 |
+
|
| 35 |
+
encrypted_path = os.path.join(encrypted_dir, json_file)
|
| 36 |
+
zip_path = os.path.join(encrypted_dir, f"{run_id}.zip")
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
# Only encrypt and zip if the zip doesn't exist yet
|
| 40 |
+
if not os.path.exists(zip_path):
|
| 41 |
+
# Encrypt the JSON file
|
| 42 |
+
encryptor.encrypt_json_file(input_path, encrypted_path)
|
| 43 |
+
|
| 44 |
+
# Create zip archive containing the encrypted file
|
| 45 |
+
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 46 |
+
zipf.write(encrypted_path, os.path.basename(encrypted_path))
|
| 47 |
+
|
| 48 |
+
# Remove the unzipped encrypted file
|
| 49 |
+
os.remove(encrypted_path)
|
| 50 |
+
|
| 51 |
+
# Upload to Hugging Face Hub (attempt for all files)
|
| 52 |
+
api.upload_file(
|
| 53 |
+
path_or_fileobj=zip_path,
|
| 54 |
+
path_in_repo=os.path.basename(zip_path),
|
| 55 |
+
repo_id="agent-evals/agent_traces",
|
| 56 |
+
repo_type="dataset",
|
| 57 |
+
commit_message=f"Add encrypted trace for {json_file}"
|
| 58 |
+
)
|
| 59 |
+
print(f"Successfully uploaded {zip_path}")
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"Error processing {json_file}: {str(e)}")
|
| 63 |
+
|
| 64 |
+
print(f"Processed {len(json_files)} files")
|
utils/json_encryption.py
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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ryptography.hazmat.primitives import hashes
|
| 4 |
+
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
|
| 5 |
+
import base64
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
class JsonEncryption:
|
| 9 |
+
def __init__(self, password: str, salt: bytes = None):
|
| 10 |
+
"""Initialize encryption with a password.
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
password (str): The password used for encryption/decryption
|
| 14 |
+
salt (bytes): The salt used for key derivation
|
| 15 |
+
"""
|
| 16 |
+
# Store the original password
|
| 17 |
+
self._original_password = password
|
| 18 |
+
# Generate a salt for key derivation or use provided salt
|
| 19 |
+
self.salt = salt if salt is not None else os.urandom(16)
|
| 20 |
+
# Create a key from the password
|
| 21 |
+
self.key = self._generate_key(password.encode(), self.salt)
|
| 22 |
+
# Initialize Fernet cipher
|
| 23 |
+
self.cipher = Fernet(self.key)
|
| 24 |
+
|
| 25 |
+
def _generate_key(self, password: bytes, salt: bytes) -> bytes:
|
| 26 |
+
"""Generate a secure key from password and salt using PBKDF2."""
|
| 27 |
+
kdf = PBKDF2HMAC(
|
| 28 |
+
algorithm=hashes.SHA256(),
|
| 29 |
+
length=32,
|
| 30 |
+
salt=salt,
|
| 31 |
+
iterations=480000,
|
| 32 |
+
)
|
| 33 |
+
key = base64.urlsafe_b64encode(kdf.derive(password))
|
| 34 |
+
return key
|
| 35 |
+
|
| 36 |
+
def encrypt_json(self, json_data: dict) -> dict:
|
| 37 |
+
"""Encrypt JSON data.
|
| 38 |
+
|
| 39 |
+
Args:
|
| 40 |
+
json_data (dict): JSON data to encrypt
|
| 41 |
+
|
| 42 |
+
Returns:
|
| 43 |
+
dict: Dictionary containing the encrypted data and salt
|
| 44 |
+
"""
|
| 45 |
+
# Convert JSON to string
|
| 46 |
+
json_str = json.dumps(json_data)
|
| 47 |
+
|
| 48 |
+
# Encrypt the JSON string
|
| 49 |
+
encrypted_data = self.cipher.encrypt(json_str.encode())
|
| 50 |
+
|
| 51 |
+
# Return encrypted data and salt
|
| 52 |
+
return {
|
| 53 |
+
'encrypted_data': base64.b64encode(encrypted_data).decode('utf-8'),
|
| 54 |
+
'salt': base64.b64encode(self.salt).decode('utf-8')
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
def decrypt_json(self, encrypted_data: str, salt: str) -> dict:
|
| 58 |
+
"""Decrypt JSON data.
|
| 59 |
+
|
| 60 |
+
Args:
|
| 61 |
+
encrypted_data (str): Base64 encoded encrypted data
|
| 62 |
+
salt (str): Base64 encoded salt used for encryption
|
| 63 |
+
|
| 64 |
+
Returns:
|
| 65 |
+
dict: Decrypted JSON data
|
| 66 |
+
"""
|
| 67 |
+
try:
|
| 68 |
+
# Decode the encrypted data and salt from base64
|
| 69 |
+
encrypted_bytes = base64.b64decode(encrypted_data.encode('utf-8'))
|
| 70 |
+
|
| 71 |
+
# Decrypt the data
|
| 72 |
+
decrypted_data = self.cipher.decrypt(encrypted_bytes)
|
| 73 |
+
|
| 74 |
+
# Parse and return the JSON
|
| 75 |
+
return json.loads(decrypted_data.decode('utf-8'))
|
| 76 |
+
except Exception as e:
|
| 77 |
+
raise ValueError(f"Decryption failed: {str(e)}")
|
| 78 |
+
|
| 79 |
+
def encrypt_json_file(self, input_path: str, output_path: str):
|
| 80 |
+
"""Encrypt JSON file.
|
| 81 |
+
|
| 82 |
+
Args:
|
| 83 |
+
input_path (str): Path to input JSON file
|
| 84 |
+
output_path (str): Path to save encrypted file
|
| 85 |
+
"""
|
| 86 |
+
try:
|
| 87 |
+
# Read JSON file
|
| 88 |
+
with open(input_path, 'r') as file:
|
| 89 |
+
json_data = json.load(file)
|
| 90 |
+
|
| 91 |
+
# Encrypt the data
|
| 92 |
+
encrypted = self.encrypt_json(json_data)
|
| 93 |
+
|
| 94 |
+
# Write encrypted data to file
|
| 95 |
+
with open(output_path, 'w') as file:
|
| 96 |
+
json.dump(encrypted, file, indent=2)
|
| 97 |
+
|
| 98 |
+
except Exception as e:
|
| 99 |
+
raise ValueError(f"File encryption failed: {str(e)}")
|
| 100 |
+
|
| 101 |
+
def decrypt_json_file(self, input_path: str, output_path: str):
|
| 102 |
+
"""Decrypt JSON file.
|
| 103 |
+
|
| 104 |
+
Args:
|
| 105 |
+
input_path (str): Path to encrypted JSON file
|
| 106 |
+
output_path (str): Path to save decrypted file
|
| 107 |
+
"""
|
| 108 |
+
try:
|
| 109 |
+
# Read encrypted file
|
| 110 |
+
with open(input_path, 'r') as file:
|
| 111 |
+
encrypted_data = json.load(file)
|
| 112 |
+
|
| 113 |
+
# Create new cipher with the stored salt
|
| 114 |
+
salt = base64.b64decode(encrypted_data['salt'].encode('utf-8'))
|
| 115 |
+
# Fix: Pass the original password instead of the key
|
| 116 |
+
self.__init__(password=self._original_password, salt=salt) # Reinitialize with correct salt
|
| 117 |
+
|
| 118 |
+
# Decrypt the data
|
| 119 |
+
decrypted = self.decrypt_json(
|
| 120 |
+
encrypted_data['encrypted_data'],
|
| 121 |
+
encrypted_data['salt']
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
# Write decrypted data to file
|
| 125 |
+
with open(output_path, 'w') as file:
|
| 126 |
+
json.dump(decrypted, file, indent=2)
|
| 127 |
+
|
| 128 |
+
except Exception as e:
|
| 129 |
+
raise ValueError(f"File decryption failed: {str(e)}")
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
# Example usage
|
| 133 |
+
if __name__ == "__main__":
|
| 134 |
+
# Example JSON data
|
| 135 |
+
sample_data = {
|
| 136 |
+
"username": "john_doe",
|
| 137 |
+
"email": "john@example.com",
|
| 138 |
+
"settings": {
|
| 139 |
+
"theme": "dark",
|
| 140 |
+
"notifications": True
|
| 141 |
+
}
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
try:
|
| 145 |
+
# Initialize encryptor with a password
|
| 146 |
+
encryptor = JsonEncryption("your-secure-password")
|
| 147 |
+
|
| 148 |
+
# Example 1: Encrypt and decrypt data in memory
|
| 149 |
+
print("Example 1: In-memory encryption/decryption")
|
| 150 |
+
encrypted = encryptor.encrypt_json(sample_data)
|
| 151 |
+
print(f"Encrypted data: {encrypted}")
|
| 152 |
+
|
| 153 |
+
decrypted = encryptor.decrypt_json(
|
| 154 |
+
encrypted['encrypted_data'],
|
| 155 |
+
encrypted['salt']
|
| 156 |
+
)
|
| 157 |
+
print(f"Decrypted data: {decrypted}")
|
| 158 |
+
|
| 159 |
+
# Example 2: Encrypt and decrypt files
|
| 160 |
+
print("\nExample 2: File encryption/decryption")
|
| 161 |
+
encryptor.encrypt_json_file("input.json", "encrypted.json")
|
| 162 |
+
print("File encrypted successfully")
|
| 163 |
+
|
| 164 |
+
encryptor.decrypt_json_file("encrypted.json", "decrypted.json")
|
| 165 |
+
print("File decrypted successfully")
|
| 166 |
+
|
| 167 |
+
except Exception as e:
|
| 168 |
+
print(f"Error: {str(e)}")
|