Calculator / app.py
ba456jutt's picture
Update app.py
6d1c342 verified
import gradio as gr
import hashlib
import ipaddress
import base64
import re
# --- Tab 1: Math Logic ---
def calculate(num1, operation, num2):
try:
if operation == "Add (+)": return num1 + num2
if operation == "Subtract (-)": return num1 - num2
if operation == "Multiply (*)": return num1 * num2
if operation == "Divide (/)":
return num1 / num2 if num2 != 0 else "Error: Division by zero"
if operation == "Power (x^y)": return num1 ** num2
except Exception as e:
return f"Error: {str(e)}"
# --- Tab 2: Hashing & Encoding ---
def crypto_tools(text, action):
if not text:
return "Please enter some text."
try:
if action == "Generate MD5 & SHA-256":
md5_hash = hashlib.md5(text.encode()).hexdigest()
sha256_hash = hashlib.sha256(text.encode()).hexdigest()
return f"MD5:\n{md5_hash}\n\nSHA-256:\n{sha256_hash}"
elif action == "Encode to Base64":
return base64.b64encode(text.encode()).decode()
elif action == "Decode from Base64":
return base64.b64decode(text.encode()).decode()
except Exception as e:
return f"Error decoding: Make sure it is valid Base64."
# --- Tab 3: Network Tools ---
def subnet_calculator(ip_with_cidr):
try:
network = ipaddress.IPv4Network(ip_with_cidr, strict=False)
result = (
f"Network Address: {network.network_address}\n"
f"Broadcast Address: {network.broadcast_address}\n"
f"Netmask: {network.netmask}\n"
f"Total Usable Hosts: {network.num_addresses - 2}"
)
return result
except Exception as e:
return "Invalid IP/CIDR format. Example: 192.168.1.0/24"
# --- Tab 4: Password Analyzer ---
def check_password_strength(password):
if not password:
return "Enter a password to analyze."
score = 0
feedback = []
if len(password) >= 8: score += 1
else: feedback.append("- Too short (needs 8+ chars)")
if re.search(r"[A-Z]", password): score += 1
else: feedback.append("- Missing uppercase letter")
if re.search(r"[a-z]", password): score += 1
else: feedback.append("- Missing lowercase letter")
if re.search(r"[0-9]", password): score += 1
else: feedback.append("- Missing number")
if re.search(r"[!@#$%^&*(),.?\":{}|<>]", password): score += 1
else: feedback.append("- Missing special character")
if score == 5:
return "🟢 STRONG PASSWORD"
elif score >= 3:
return f"🟡 MEDIUM PASSWORD\nImprovements needed:\n" + "\n".join(feedback)
else:
return f"🔴 WEAK PASSWORD\nImprovements needed:\n" + "\n".join(feedback)
# --- Build the UI using Gradio Blocks ---
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
gr.Markdown("# 🛡️ Cybersecurity Multi-Tool Toolkit")
gr.Markdown("A specialized calculator and toolkit for cybersecurity analysis.")
# Tab 1
with gr.Tab("Standard Calculator"):
with gr.Row():
with gr.Column():
num1 = gr.Number(label="First Number")
operation = gr.Radio(["Add (+)", "Subtract (-)", "Multiply (*)", "Divide (/)", "Power (x^y)"], label="Operation")
num2 = gr.Number(label="Second Number")
calc_btn = gr.Button("Calculate")
with gr.Column():
calc_result = gr.Textbox(label="Result", interactive=False)
calc_btn.click(fn=calculate, inputs=[num1, operation, num2], outputs=calc_result)
# Tab 2
with gr.Tab("Cryptography & Encoding"):
with gr.Row():
with gr.Column():
text_input = gr.Textbox(label="Enter text or payload")
crypto_action = gr.Radio(["Generate MD5 & SHA-256", "Encode to Base64", "Decode from Base64"], label="Action")
crypto_btn = gr.Button("Execute")
with gr.Column():
crypto_result = gr.Textbox(label="Output", interactive=False, lines=6)
crypto_btn.click(fn=crypto_tools, inputs=[text_input, crypto_action], outputs=crypto_result)
# Tab 3
with gr.Tab("Network Tools"):
with gr.Row():
with gr.Column():
ip_input = gr.Textbox(label="Enter IP with CIDR (e.g., 192.168.1.5/24)")
ip_btn = gr.Button("Analyze Subnet")
with gr.Column():
ip_result = gr.Textbox(label="Subnet Details", interactive=False, lines=5)
ip_btn.click(fn=subnet_calculator, inputs=ip_input, outputs=ip_result)
# Tab 4
with gr.Tab("Password Analyzer"):
with gr.Row():
with gr.Column():
pwd_input = gr.Textbox(label="Enter Password", type="password")
pwd_btn = gr.Button("Check Strength")
with gr.Column():
pwd_result = gr.Textbox(label="Analysis Result", interactive=False, lines=5)
pwd_btn.click(fn=check_password_strength, inputs=pwd_input, outputs=pwd_result)
# Launch the app
if __name__ == "__main__":
demo.launch()