Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,16 @@ import gradio as gr
|
|
| 3 |
import numpy as np
|
| 4 |
import hashlib
|
| 5 |
|
| 6 |
-
# ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
SECRET_CODE = os.getenv("ES")
|
| 8 |
exec_context = {}
|
| 9 |
encrypt_fn = None
|
|
@@ -19,26 +28,27 @@ if SECRET_CODE:
|
|
| 19 |
|
| 20 |
# --- 2. CƠ CHẾ BẢO MẬT (KEY STRETCHING) ---
|
| 21 |
def stretch_key(password, salt=b"MinhDuc_Zero_Trust_Protocol"):
|
| 22 |
-
""" Băm
|
| 23 |
key = password.encode() + salt
|
| 24 |
for _ in range(10000):
|
| 25 |
key = hashlib.sha512(key).digest()
|
| 26 |
return key[:32]
|
| 27 |
|
| 28 |
-
# --- 3. LOGIC XỬ LÝ
|
| 29 |
def process_file(file, password, mode, progress=gr.Progress()):
|
| 30 |
if not encrypt_fn or not decrypt_fn:
|
| 31 |
-
return None, "❌ Lỗi: Chưa
|
| 32 |
if not file or not password:
|
| 33 |
-
return None, "⚠️
|
| 34 |
|
| 35 |
-
# Khởi tạo
|
| 36 |
master_key = np.frombuffer(stretch_key(password), dtype=np.uint8)
|
| 37 |
|
| 38 |
input_path = file.name
|
| 39 |
original_name = os.path.basename(input_path)
|
|
|
|
| 40 |
|
| 41 |
-
#
|
| 42 |
is_encrypt = (mode == "Mã hóa")
|
| 43 |
if is_encrypt:
|
| 44 |
output_name = original_name + ".impossible"
|
|
@@ -46,53 +56,74 @@ def process_file(file, password, mode, progress=gr.Progress()):
|
|
| 46 |
output_name = original_name.replace(".impossible", "") if original_name.endswith(".impossible") else "rebuilt_" + original_name
|
| 47 |
|
| 48 |
output_path = os.path.join(os.path.dirname(input_path), output_name)
|
| 49 |
-
|
| 50 |
-
chunk_size = 4 * 1024 * 1024 # 4MB tối ưu
|
| 51 |
bytes_processed = 0
|
| 52 |
fn = encrypt_fn if is_encrypt else decrypt_fn
|
| 53 |
|
| 54 |
try:
|
| 55 |
with open(input_path, 'rb') as f_in, open(output_path, 'wb') as f_out:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
data_arr = np.frombuffer(chunk, dtype=np.uint8)
|
| 62 |
indices = np.arange(bytes_processed, bytes_processed + len(data_arr), dtype=np.uint64)
|
| 63 |
key_chunk = master_key[indices % 32]
|
| 64 |
|
| 65 |
-
# Thực thi biến đổi Vectorized
|
| 66 |
processed = fn(data_arr, key_chunk, indices)
|
| 67 |
-
|
| 68 |
f_out.write(processed.astype(np.uint8).tobytes())
|
|
|
|
| 69 |
bytes_processed += len(chunk)
|
| 70 |
-
progress(bytes_processed /
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
return output_path, f"✅
|
| 73 |
except Exception as e:
|
| 74 |
if os.path.exists(output_path): os.remove(output_path)
|
| 75 |
return None, f"❌ Lỗi: {str(e)}"
|
| 76 |
|
| 77 |
-
# --- 4. GIAO DIỆN (
|
| 78 |
-
with gr.Blocks(
|
| 79 |
-
gr.Markdown("# 🛡️ Titanium4S - Professional Vault")
|
| 80 |
-
gr.Markdown("Hệ thống bảo mật
|
| 81 |
|
| 82 |
with gr.Row():
|
| 83 |
with gr.Column():
|
| 84 |
inp_file = gr.File(label="Tải file lên")
|
| 85 |
-
inp_pass = gr.Textbox(label="Mật khẩu bí mật", type="password")
|
| 86 |
inp_mode = gr.Radio(["Mã hóa", "Giải mã"], label="Chế độ", value="Mã hóa")
|
| 87 |
btn = gr.Button("KÍCH HOẠT", variant="primary")
|
| 88 |
with gr.Column():
|
| 89 |
-
out_file = gr.File(label="
|
| 90 |
status = gr.Textbox(label="Trạng thái", interactive=False)
|
| 91 |
|
| 92 |
btn.click(process_file, [inp_file, inp_pass, inp_mode], [out_file, status])
|
| 93 |
|
| 94 |
-
#
|
| 95 |
demo.queue(default_concurrency_limit=1)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import hashlib
|
| 5 |
|
| 6 |
+
# --- CẤU HÌNH METADATA TITANIUM4S ---
|
| 7 |
+
# Dùng để cảnh báo và định danh file của hệ thống
|
| 8 |
+
T4S_MARKER = b"\n---TITANIUM4S-SECURITY-FOOTER---"
|
| 9 |
+
T4S_POLICY = (
|
| 10 |
+
"\n[WARNING] Encrypted by Titanium4S Protocol."
|
| 11 |
+
"\n[POLICY] Data recovery is IMPOSSIBLE without the secret key."
|
| 12 |
+
"\n[NOTICE] This file contains encrypted binary data. DO NOT MODIFY."
|
| 13 |
+
).encode('utf-8')
|
| 14 |
+
|
| 15 |
+
# --- 1. NẠP THUẬT TOÁN TỪ BIẾN MÔI TRƯỜNG ES ---
|
| 16 |
SECRET_CODE = os.getenv("ES")
|
| 17 |
exec_context = {}
|
| 18 |
encrypt_fn = None
|
|
|
|
| 28 |
|
| 29 |
# --- 2. CƠ CHẾ BẢO MẬT (KEY STRETCHING) ---
|
| 30 |
def stretch_key(password, salt=b"MinhDuc_Zero_Trust_Protocol"):
|
| 31 |
+
""" Băm mật khẩu 10,000 lần để chống Brute-force """
|
| 32 |
key = password.encode() + salt
|
| 33 |
for _ in range(10000):
|
| 34 |
key = hashlib.sha512(key).digest()
|
| 35 |
return key[:32]
|
| 36 |
|
| 37 |
+
# --- 3. LOGIC XỬ LÝ CHÍNH ---
|
| 38 |
def process_file(file, password, mode, progress=gr.Progress()):
|
| 39 |
if not encrypt_fn or not decrypt_fn:
|
| 40 |
+
return None, "❌ Lỗi: Chưa cấu hình Secret ES trong Settings!"
|
| 41 |
if not file or not password:
|
| 42 |
+
return None, "⚠️ Minh Đức ơi, hãy chọn file và nhập mật khẩu nhé!"
|
| 43 |
|
| 44 |
+
# Khởi tạo khóa từ mật khẩu
|
| 45 |
master_key = np.frombuffer(stretch_key(password), dtype=np.uint8)
|
| 46 |
|
| 47 |
input_path = file.name
|
| 48 |
original_name = os.path.basename(input_path)
|
| 49 |
+
file_size = os.path.getsize(input_path)
|
| 50 |
|
| 51 |
+
# Thiết lập chế độ và tên file đầu ra
|
| 52 |
is_encrypt = (mode == "Mã hóa")
|
| 53 |
if is_encrypt:
|
| 54 |
output_name = original_name + ".impossible"
|
|
|
|
| 56 |
output_name = original_name.replace(".impossible", "") if original_name.endswith(".impossible") else "rebuilt_" + original_name
|
| 57 |
|
| 58 |
output_path = os.path.join(os.path.dirname(input_path), output_name)
|
| 59 |
+
chunk_size = 4 * 1024 * 1024 # 4MB chunk
|
|
|
|
| 60 |
bytes_processed = 0
|
| 61 |
fn = encrypt_fn if is_encrypt else decrypt_fn
|
| 62 |
|
| 63 |
try:
|
| 64 |
with open(input_path, 'rb') as f_in, open(output_path, 'wb') as f_out:
|
| 65 |
+
# --- XỬ LÝ KÍCH THƯỚC MỤC TIÊU (LOẠI BỎ METADATA KHI GIẢI MÃ) ---
|
| 66 |
+
target_data_size = file_size
|
| 67 |
+
if not is_encrypt:
|
| 68 |
+
# Kiểm tra 2KB cuối cùng để tìm Marker
|
| 69 |
+
check_range = min(file_size, 2048)
|
| 70 |
+
f_in.seek(file_size - check_range)
|
| 71 |
+
footer_data = f_in.read(check_range)
|
| 72 |
+
marker_pos = footer_data.find(T4S_MARKER)
|
| 73 |
+
if marker_pos != -1:
|
| 74 |
+
# Tính toán lại kích thước thật (bỏ qua phần Footer)
|
| 75 |
+
target_data_size = file_size - (check_range - marker_pos)
|
| 76 |
+
f_in.seek(0) # Quay lại đầu file để bắt đầu giải mã
|
| 77 |
+
|
| 78 |
+
# --- VÒNG LẶP XỬ LÝ DỮ LIỆU ---
|
| 79 |
+
while bytes_processed < target_data_size:
|
| 80 |
+
read_len = min(chunk_size, target_data_size - bytes_processed)
|
| 81 |
+
chunk = f_in.read(read_len)
|
| 82 |
+
if not chunk: break
|
| 83 |
|
| 84 |
data_arr = np.frombuffer(chunk, dtype=np.uint8)
|
| 85 |
indices = np.arange(bytes_processed, bytes_processed + len(data_arr), dtype=np.uint64)
|
| 86 |
key_chunk = master_key[indices % 32]
|
| 87 |
|
|
|
|
| 88 |
processed = fn(data_arr, key_chunk, indices)
|
|
|
|
| 89 |
f_out.write(processed.astype(np.uint8).tobytes())
|
| 90 |
+
|
| 91 |
bytes_processed += len(chunk)
|
| 92 |
+
progress(bytes_processed / target_data_size, desc=f"Đang {mode.lower()}...")
|
| 93 |
+
|
| 94 |
+
# --- CHÈN FOOTER KHI MÃ HÓA XONG ---
|
| 95 |
+
if is_encrypt:
|
| 96 |
+
f_out.write(T4S_MARKER)
|
| 97 |
+
f_out.write(T4S_POLICY)
|
| 98 |
|
| 99 |
+
return output_path, f"✅ {mode} thành công! Metadata Titanium4S đã được xử lý."
|
| 100 |
except Exception as e:
|
| 101 |
if os.path.exists(output_path): os.remove(output_path)
|
| 102 |
return None, f"❌ Lỗi: {str(e)}"
|
| 103 |
|
| 104 |
+
# --- 4. GIAO DIỆN GRADIO (FIXED FOR 6.0+) ---
|
| 105 |
+
with gr.Blocks(title="Titanium4S Vault") as demo:
|
| 106 |
+
gr.Markdown("# 🛡️ Titanium4S - Professional Vault v5.0")
|
| 107 |
+
gr.Markdown("Hệ thống bảo mật thép với cơ chế đóng dấu Metadata bảo toàn dữ liệu.")
|
| 108 |
|
| 109 |
with gr.Row():
|
| 110 |
with gr.Column():
|
| 111 |
inp_file = gr.File(label="Tải file lên")
|
| 112 |
+
inp_pass = gr.Textbox(label="Mật khẩu bí mật", type="password", placeholder="Nhập khóa để kích hoạt...")
|
| 113 |
inp_mode = gr.Radio(["Mã hóa", "Giải mã"], label="Chế độ", value="Mã hóa")
|
| 114 |
btn = gr.Button("KÍCH HOẠT", variant="primary")
|
| 115 |
with gr.Column():
|
| 116 |
+
out_file = gr.File(label="Dữ liệu kết quả")
|
| 117 |
status = gr.Textbox(label="Trạng thái", interactive=False)
|
| 118 |
|
| 119 |
btn.click(process_file, [inp_file, inp_pass, inp_mode], [out_file, status])
|
| 120 |
|
| 121 |
+
# Cấu hình Queue
|
| 122 |
demo.queue(default_concurrency_limit=1)
|
| 123 |
|
| 124 |
if __name__ == "__main__":
|
| 125 |
+
# Launch với theme Monochrome và tắt SSR để ổn định
|
| 126 |
+
demo.launch(
|
| 127 |
+
theme=gr.themes.Monochrome(),
|
| 128 |
+
ssr_mode=False
|
| 129 |
+
)
|