-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecure_locker.py
More file actions
99 lines (80 loc) · 3.22 KB
/
secure_locker.py
File metadata and controls
99 lines (80 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os, json, hashlib, time
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Protocol.SecretSharing import Shamir
from local_cloud import LocalCloudManager
# ========== AES + Shamir Secret Sharing + Simple Blockchain ==========
class Block:
def __init__(self, index, timestamp, data, previous_hash):
self.index = index
self.timestamp = timestamp
self.data = data
self.previous_hash = previous_hash
self.hash = self.calculate_hash()
def calculate_hash(self):
block_str = json.dumps({
"index": self.index,
"timestamp": self.timestamp,
"data": self.data,
"previous_hash": self.previous_hash
}, sort_keys=True)
return hashlib.sha512(block_str.encode()).hexdigest()
class SimpleBlockchain:
def __init__(self):
self.chain = [self.create_genesis_block()]
def create_genesis_block(self):
return Block(0, time.time(), {"event": "Genesis"}, "0")
def add_block(self, data):
prev = self.chain[-1]
new_block = Block(len(self.chain), time.time(), data, prev.hash)
self.chain.append(new_block)
print(f" Added Block #{new_block.index} | Hash: {new_block.hash[:16]}...")
# ========== Core Secure Locker ==========
class SecureLocker:
def __init__(self, threshold=2, num_shares=3):
self.cloud = LocalCloudManager()
self.blockchain = SimpleBlockchain()
self.threshold = threshold
self.num_shares = num_shares
def encrypt_file(self, filepath):
with open(filepath, "rb") as f:
data = f.read()
key = get_random_bytes(16) # 128-bit AES key (required by Shamir)
cipher = AES.new(key, AES.MODE_GCM)
ciphertext, tag = cipher.encrypt_and_digest(data)
enc_data = cipher.nonce + ciphertext + tag
file_hash = hashlib.sha512(data).hexdigest()
file_id = file_hash[:16]
# Split key
shares = Shamir.split(self.threshold, self.num_shares, key)
print(f"AES key split into {self.num_shares} shares (need {self.threshold} to recover)")
# “Upload” chunks & shares locally
chunk_paths = self.cloud.upload_file_chunks(file_id, enc_data)
share_paths = self.cloud.upload_key_shares(file_id, shares)
# Log on blockchain
self.blockchain.add_block({
"event": "UPLOAD",
"file_id": file_id,
"file_hash_sha512": file_hash,
"chunks": list(chunk_paths.values()),
"shares": list(share_paths.values())
})
# Save metadata
meta = {
"file_id": file_id,
"file_hash": file_hash,
"nonce": cipher.nonce.hex(),
"chunks": chunk_paths,
"shares": share_paths
}
with open("file_metadata.json", "w") as f:
json.dump(meta, f, indent=2)
print("Metadata saved → file_metadata.json")
return key, enc_data
# ========== Run ==========
if __name__ == "__main__":
locker = SecureLocker()
locker.encrypt_file("sensitive_document.pdf")
print("\nBlockchain:")
for b in locker.blockchain.chain:
print(f"Block #{b.index} → {b.hash[:32]} {b.data}")