import tkinter as tk import hashlib import difflib class HashLab: def __init__(self, root): self.root = root self.root.title("Lab 2: Efecto Avalancha (SHA-256)") self.root.geometry("800x450") # Inputs tk.Label(root, text="Entrada A:").pack(anchor="w", padx=20) self.input_a = tk.Entry(root, width=80, font=("Consolas", 10)) self.input_a.pack(padx=20, pady=5) self.input_a.bind("", self.update) tk.Label(root, text="Entrada B:").pack(anchor="w", padx=20) self.input_b = tk.Entry(root, width=80, font=("Consolas", 10)) self.input_b.pack(padx=20, pady=5) self.input_b.bind("", self.update) # Diferencia Textual self.lbl_text_diff = tk.Label(root, text="Diferencia de texto: 0.00%", font=("Arial", 10, "italic"), fg="#555") self.lbl_text_diff.pack(pady=5) # Visualización Hashes tk.Label(root, text="Hash A (SHA-256):").pack(anchor="w", padx=20, pady=(20,0)) self.lbl_hash_a = tk.Label(root, text="", font=("Consolas", 9), fg="blue", wraplength=750) self.lbl_hash_a.pack(padx=20) tk.Label(root, text="Hash B (SHA-256):").pack(anchor="w", padx=20) self.lbl_hash_b = tk.Label(root, text="", font=("Consolas", 9), fg="blue", wraplength=750) self.lbl_hash_b.pack(padx=20) # Estadísticas self.lbl_stats = tk.Label(root, text="Diferencia de bits: 0 / 256 (0.00%)", font=("Arial", 12, "bold")) self.lbl_stats.pack(pady=30) # Canvas para bits self.canvas_bits = tk.Canvas(root, width=768, height=20, bg="white") self.canvas_bits.pack(pady=10) def update(self, event=None): text_str_a = self.input_a.get() text_str_b = self.input_b.get() # Calcular diferencia textual matcher = difflib.SequenceMatcher(None, text_str_a, text_str_b) ratio = matcher.ratio() # 1.0 es igual, 0.0 es diferente diff_pct = (1.0 - ratio) * 100 self.lbl_text_diff.config(text=f"Diferencia de texto: {diff_pct:.2f}%") text_a = text_str_a.encode() text_b = text_str_b.encode() hash_a_hex = hashlib.sha256(text_a).hexdigest() hash_b_hex = hashlib.sha256(text_b).hexdigest() self.lbl_hash_a.config(text=hash_a_hex) self.lbl_hash_b.config(text=hash_b_hex) # Convertir a binario bin_a = bin(int(hash_a_hex, 16))[2:].zfill(256) bin_b = bin(int(hash_b_hex, 16))[2:].zfill(256) # Comparar bits diffs = 0 self.canvas_bits.delete("all") w = 3 # ancho por bit visual for i in range(256): color = "#eee" # igual if bin_a[i] != bin_b[i]: diffs += 1 color = "red" # Diferente self.canvas_bits.create_rectangle(i*w, 0, (i*w)+w, 20, fill=color, outline="") pct = (diffs / 256) * 100 self.lbl_stats.config(text=f"Diferencia de bits: {diffs} / 256 ({pct:.2f}%)") # Color semántico if diffs == 0: self.lbl_stats.config(fg="green") elif pct < 40: self.lbl_stats.config(fg="orange") # Avalancha pobre (raro en sha256) else: self.lbl_stats.config(fg="red") # gran avalancha del hash if __name__ == "__main__": root = tk.Tk() app = HashLab(root) root.mainloop()