You've already forked vk26-onlinehasherapp
76 lines
1.8 KiB
Python
76 lines
1.8 KiB
Python
import streamlit as st
|
|
from pathlib import Path
|
|
import hashlib
|
|
|
|
CONFFILE = Path("config.txt")
|
|
INPUT_DIR = Path("input")
|
|
OUTPUT_DIR = Path("output")
|
|
|
|
HASHMODE = "md5"
|
|
|
|
with open(CONFFILE) as f:
|
|
hash_type = f.read().strip()
|
|
|
|
if hash_type not in ["md5", "sha256"]:
|
|
print("Invalid hash type in config")
|
|
exit(1)
|
|
|
|
HASHMODE = hash_type
|
|
|
|
st.title("Online file hasher")
|
|
|
|
st.text(f"MODE: {HASHMODE}")
|
|
|
|
st.header("Upload a file")
|
|
with st.form("upload", clear_on_submit=True):
|
|
uploaded_file = st.file_uploader("Choose a file to upload")
|
|
file_submitted = st.form_submit_button("Upload")
|
|
|
|
if file_submitted and (uploaded_file is not None):
|
|
content = bytes(uploaded_file.getbuffer())
|
|
dest = INPUT_DIR / uploaded_file.name
|
|
try:
|
|
with open(dest, "wb") as f:
|
|
f.write(content)
|
|
except Exception as ex:
|
|
st.error(f"Failed to upload: {ex}")
|
|
st.stop()
|
|
|
|
if HASHMODE == "md5":
|
|
hash = hashlib.md5(content).hexdigest()
|
|
elif HASHMODE == "sha256":
|
|
hash = hashlib.sha256(content).hexdigest()
|
|
else:
|
|
hash = ""
|
|
|
|
dest = OUTPUT_DIR / (uploaded_file.name + ".md5")
|
|
try:
|
|
with open(dest, "w") as f:
|
|
f.write(hash)
|
|
except Exception as ex:
|
|
st.error(f"Failed to upload: {ex}")
|
|
st.stop()
|
|
|
|
st.success("Uploaded successfully!")
|
|
|
|
st.divider()
|
|
|
|
st.header("Results")
|
|
files = tuple(OUTPUT_DIR.iterdir())
|
|
|
|
if not files:
|
|
st.info("No uploads yet.")
|
|
else:
|
|
for file in files:
|
|
col1, col2, col3 = st.columns([4, 2, 1])
|
|
|
|
with open(file, "r") as f:
|
|
hash = f.read()
|
|
|
|
col1.write(f"**{file.name}**")
|
|
col2.write(hash)
|
|
|
|
if col3.button("Delete", key=f"del_{file.name}"):
|
|
file.unlink()
|
|
st.rerun()
|