Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,128 +2,128 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
import hashlib
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# ---
|
| 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
|
| 19 |
-
decrypt_fn = None
|
| 20 |
-
|
| 21 |
if SECRET_CODE:
|
| 22 |
try:
|
| 23 |
exec(SECRET_CODE, exec_context)
|
| 24 |
-
encrypt_fn = exec_context.get('transform_byte')
|
| 25 |
-
|
| 26 |
-
except Exception as e:
|
| 27 |
-
print(f"Lỗi nạp Secret ES: {e}")
|
| 28 |
|
| 29 |
-
# ---
|
| 30 |
-
def
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# --- 3. LOGIC XỬ LÝ CHÍNH ---
|
| 38 |
-
def
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
return None, "⚠️ Minh Đức ơi, hãy chọn file và nhập mật khẩu nhé!"
|
| 43 |
|
| 44 |
-
|
|
|
|
| 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"
|
| 55 |
-
else:
|
| 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 |
-
|
|
|
|
| 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 |
-
|
| 66 |
-
|
| 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 /
|
| 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 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
# --- 4. GIAO DIỆN
|
| 105 |
-
with gr.Blocks(title="Titanium4S Vault") as demo:
|
| 106 |
-
gr.Markdown("# 🛡️ Titanium4S -
|
| 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="
|
| 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 |
-
|
| 117 |
status = gr.Textbox(label="Trạng thái", interactive=False)
|
|
|
|
| 118 |
|
| 119 |
-
|
| 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 |
-
)
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
import hashlib
|
| 5 |
+
from fpdf import FPDF
|
| 6 |
+
from datetime import datetime
|
| 7 |
|
| 8 |
+
# --- 1. NẠP THUẬT TOÁN (Giữ nguyên) ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
SECRET_CODE = os.getenv("ES")
|
| 10 |
exec_context = {}
|
| 11 |
+
encrypt_fn = decrypt_fn = None
|
|
|
|
|
|
|
| 12 |
if SECRET_CODE:
|
| 13 |
try:
|
| 14 |
exec(SECRET_CODE, exec_context)
|
| 15 |
+
encrypt_fn, decrypt_fn = exec_context.get('transform_byte'), exec_context.get('untransform_byte')
|
| 16 |
+
except Exception as e: print(f"Error: {e}")
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# --- HÀM TÍNH SHA-256 FILE ---
|
| 19 |
+
def get_file_sha256(file_path):
|
| 20 |
+
h = hashlib.sha256()
|
| 21 |
+
with open(file_path, 'rb') as f:
|
| 22 |
+
while chunk := f.read(8192): h.update(chunk)
|
| 23 |
+
return h.hexdigest()
|
| 24 |
+
|
| 25 |
+
# --- 2. THIẾT KẾ CHỨNG CHỈ NÂNG CAO ---
|
| 26 |
+
def create_security_pdf(file_name, sha_hash, password, output_path):
|
| 27 |
+
pdf = FPDF(orientation="L", unit="mm", format="A4")
|
| 28 |
+
pdf.add_page()
|
| 29 |
+
|
| 30 |
+
# Khung viền sang trọng
|
| 31 |
+
pdf.set_line_width(2); pdf.set_draw_color(184, 134, 11); pdf.rect(5, 5, 287, 200)
|
| 32 |
+
pdf.set_line_width(0.5); pdf.rect(8, 8, 281, 194)
|
| 33 |
+
|
| 34 |
+
# Tiêu đề
|
| 35 |
+
pdf.set_font("Helvetica", "B", 30); pdf.set_text_color(44, 62, 80)
|
| 36 |
+
pdf.cell(0, 35, "TITANIUM4S DATA PROTECTION AUDIT", ln=True, align="C")
|
| 37 |
+
|
| 38 |
+
# Thông tin file
|
| 39 |
+
pdf.set_font("Helvetica", "B", 12); pdf.set_text_color(0, 0, 0)
|
| 40 |
+
pdf.ln(10)
|
| 41 |
+
pdf.cell(0, 10, f"FILE IDENTITY: {file_name}", ln=True, align="C")
|
| 42 |
+
|
| 43 |
+
# Hiển thị SHA-256 (Bằng chứng số)
|
| 44 |
+
pdf.set_font("Courier", "B", 10)
|
| 45 |
+
pdf.set_fill_color(240, 240, 240)
|
| 46 |
+
pdf.cell(0, 10, f"SHA-256 HASH: {sha_hash}", ln=True, align="C", fill=True)
|
| 47 |
+
|
| 48 |
+
# Hiển thị Mật khẩu (Bạn có thể chọn ẩn bớt ký tự nếu muốn bảo mật hơn)
|
| 49 |
+
pdf.ln(5)
|
| 50 |
+
pdf.set_font("Helvetica", "B", 13)
|
| 51 |
+
pdf.set_text_color(192, 57, 43)
|
| 52 |
+
pdf.cell(0, 10, f"ENCRYPTION KEY RECORD: {password}", ln=True, align="C")
|
| 53 |
+
|
| 54 |
+
# Chính sách bảo mật
|
| 55 |
+
pdf.ln(10)
|
| 56 |
+
pdf.set_font("Helvetica", "I", 10); pdf.set_text_color(100, 100, 100)
|
| 57 |
+
policy = (
|
| 58 |
+
"This document serves as the sole recovery record for the encrypted asset. "
|
| 59 |
+
"The SHA-256 hash above verifies the integrity of the original source. "
|
| 60 |
+
"Keep this certificate in a disconnected, physical or hidden digital location."
|
| 61 |
+
)
|
| 62 |
+
pdf.multi_cell(220, 6, policy, align="C", center=True)
|
| 63 |
+
|
| 64 |
+
# QR Code giả lập & Chữ ký
|
| 65 |
+
pdf.ln(15)
|
| 66 |
+
pdf.set_font("Times", "I", 14); pdf.set_text_color(44, 62, 80)
|
| 67 |
+
pdf.cell(0, 10, "Verified by Titanium4S Zero-Trust Protocol", ln=True, align="C")
|
| 68 |
+
|
| 69 |
+
pdf.output(output_path)
|
| 70 |
+
return output_path
|
| 71 |
|
| 72 |
# --- 3. LOGIC XỬ LÝ CHÍNH ---
|
| 73 |
+
def stretch_key(password):
|
| 74 |
+
key = password.encode() + b"MinhDuc_Zero_Trust_Protocol"
|
| 75 |
+
for _ in range(10000): key = hashlib.sha512(key).digest()
|
| 76 |
+
return key[:32]
|
|
|
|
| 77 |
|
| 78 |
+
def process_file(file, password, mode, progress=gr.Progress()):
|
| 79 |
+
if not encrypt_fn: return None, "❌ Secret ES Error!"
|
| 80 |
master_key = np.frombuffer(stretch_key(password), dtype=np.uint8)
|
|
|
|
| 81 |
input_path = file.name
|
| 82 |
original_name = os.path.basename(input_path)
|
|
|
|
|
|
|
|
|
|
| 83 |
is_encrypt = (mode == "Mã hóa")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
output_name = original_name + ".impossible" if is_encrypt else (original_name.replace(".impossible", "") if original_name.endswith(".impossible") else "rebuilt_" + original_name)
|
| 86 |
output_path = os.path.join(os.path.dirname(input_path), output_name)
|
| 87 |
+
file_size = os.path.getsize(input_path)
|
| 88 |
+
chunk_size = 4 * 1024 * 1024
|
| 89 |
bytes_processed = 0
|
| 90 |
fn = encrypt_fn if is_encrypt else decrypt_fn
|
| 91 |
|
| 92 |
try:
|
| 93 |
+
# Tính Hash trước khi mã hóa
|
| 94 |
+
file_hash = get_file_sha256(input_path) if is_encrypt else "N/A"
|
| 95 |
+
|
| 96 |
with open(input_path, 'rb') as f_in, open(output_path, 'wb') as f_out:
|
| 97 |
+
while True:
|
| 98 |
+
chunk = f_in.read(chunk_size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
if not chunk: break
|
|
|
|
| 100 |
data_arr = np.frombuffer(chunk, dtype=np.uint8)
|
| 101 |
indices = np.arange(bytes_processed, bytes_processed + len(data_arr), dtype=np.uint64)
|
| 102 |
key_chunk = master_key[indices % 32]
|
| 103 |
+
f_out.write(fn(data_arr, key_chunk, indices).astype(np.uint8).tobytes())
|
|
|
|
|
|
|
|
|
|
| 104 |
bytes_processed += len(chunk)
|
| 105 |
+
progress(bytes_processed / file_size, desc=f"Processing {mode}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
if is_encrypt:
|
| 108 |
+
pdf_path = os.path.join(os.path.dirname(input_path), f"Cert_{original_name}.pdf")
|
| 109 |
+
create_security_pdf(original_name, file_hash, password, pdf_path)
|
| 110 |
+
return [output_path, pdf_path], "✅ Đã mã hóa & Cấp chứng chỉ SHA!"
|
| 111 |
+
|
| 112 |
+
return [output_path], "✅ Giải mã thành công!"
|
| 113 |
+
except Exception as e: return None, f"❌ Lỗi: {str(e)}"
|
| 114 |
|
| 115 |
+
# --- 4. GIAO DIỆN (Giữ nguyên bản trước) ---
|
| 116 |
+
with gr.Blocks(theme=gr.themes.Monochrome(), title="Titanium4S Vault") as demo:
|
| 117 |
+
gr.Markdown("# 🛡️ Titanium4S - Audit Edition")
|
|
|
|
|
|
|
| 118 |
with gr.Row():
|
| 119 |
with gr.Column():
|
| 120 |
inp_file = gr.File(label="Tải file lên")
|
| 121 |
+
inp_pass = gr.Textbox(label="Secret Key", type="password")
|
| 122 |
inp_mode = gr.Radio(["Mã hóa", "Giải mã"], label="Chế độ", value="Mã hóa")
|
| 123 |
btn = gr.Button("KÍCH HOẠT", variant="primary")
|
| 124 |
with gr.Column():
|
| 125 |
+
out_files = gr.File(label="Chứng chỉ & Dữ liệu", file_count="multiple")
|
| 126 |
status = gr.Textbox(label="Trạng thái", interactive=False)
|
| 127 |
+
btn.click(process_file, [inp_file, inp_pass, inp_mode], [out_files, status])
|
| 128 |
|
| 129 |
+
demo.queue(1).launch(ssr_mode=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|