Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1 +1,98 @@
|
|
| 1 |
-
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
import hashlib
|
| 5 |
+
|
| 6 |
+
# --- 1. NẠP LINH HỒN THUẬT TOÁN ---
|
| 7 |
+
SECRET_CODE = os.getenv("ES")
|
| 8 |
+
exec_context = {}
|
| 9 |
+
encrypt_fn = None
|
| 10 |
+
decrypt_fn = None
|
| 11 |
+
|
| 12 |
+
if SECRET_CODE:
|
| 13 |
+
try:
|
| 14 |
+
exec(SECRET_CODE, exec_context)
|
| 15 |
+
encrypt_fn = exec_context.get('transform_byte')
|
| 16 |
+
decrypt_fn = exec_context.get('untransform_byte')
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"Lỗi nạp Secret ES: {e}")
|
| 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 SHA-512 liên tục 10,000 lần để bảo vệ mật khẩu """
|
| 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Ý FILE ---
|
| 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 nạp Secret ES trong Settings!"
|
| 32 |
+
if not file or not password:
|
| 33 |
+
return None, "⚠️ Điền đủ file và mật khẩu nhé Minh Đức!"
|
| 34 |
+
|
| 35 |
+
# Khởi tạo Master Key
|
| 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 |
+
# Xử lý tên file
|
| 42 |
+
is_encrypt = (mode == "Mã hóa")
|
| 43 |
+
if is_encrypt:
|
| 44 |
+
output_name = original_name + ".impossible"
|
| 45 |
+
else:
|
| 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 |
+
file_size = os.path.getsize(input_path)
|
| 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 |
+
while True:
|
| 57 |
+
chunk = f_in.read(chunk_size)
|
| 58 |
+
if not chunk:
|
| 59 |
+
break
|
| 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 / file_size, desc=f"Đang {mode.lower()}...")
|
| 71 |
+
|
| 72 |
+
return output_path, f"✅ Thành công! File lưu tại: {output_name}"
|
| 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 (THEME DARK/MONOCHROME) ---
|
| 78 |
+
with gr.Blocks(theme=gr.themes.Monochrome(), title="Titanium4S Vault") as demo:
|
| 79 |
+
gr.Markdown("# 🛡️ Titanium4S - Professional Vault")
|
| 80 |
+
gr.Markdown("Hệ thống bảo mật dữ liệu cấp cao dành riêng cho Minh Đức.")
|
| 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="Kết quả")
|
| 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 |
+
# Giới hạn 1 người dùng xử lý 1 lúc để tránh crash RAM
|
| 95 |
+
demo.queue(default_concurrency_limit=1)
|
| 96 |
+
|
| 97 |
+
if __name__ == "__main__":
|
| 98 |
+
demo.launch()
|