Skip to content

Instantly share code, notes, and snippets.

@redtrillix
Last active April 25, 2023 12:37
Show Gist options
  • Save redtrillix/780fcc8b6b79672d754f65286bd775f5 to your computer and use it in GitHub Desktop.
Save redtrillix/780fcc8b6b79672d754f65286bd775f5 to your computer and use it in GitHub Desktop.
A python script to generate a list of file hashes (made by chatgpt and tweaked by me cause im dumb and lazy)
# A Hash Calculator script written in python for a comma seperated value (csv) file output
# Made by redtrillix
# Version: v0.2.2
import hashlib
import os
import csv
import tkinter as tk
from tkinter import filedialog
# Select files using file explorer
root = tk.Tk()
root.withdraw()
files = filedialog.askopenfilenames(title="Select files")
# Calculate hash for each file
hashes = {}
for file_path in files:
with open(file_path, "rb") as f:
file_hash = hashlib.sha256(f.read()).hexdigest()
hashes[file_path] = file_hash
# Write hashes to a CSV file
csv_path = "hashes.csv"
file_exists = os.path.isfile(csv_path)
with open(csv_path, "a", newline="") as f:
writer = csv.writer(f)
if not file_exists:
writer.writerow(["File Path", "Hash Value"])
for file_path, file_hash in hashes.items():
writer.writerow([file_path, file_hash])
# A Hash Calculator script written in python for a sqlite database output
# Made by redtrillix
# Version: v0.1.0
import hashlib
import os
import sqlite3
import tkinter as tk
from tkinter import filedialog
# Select files using file explorer
root = tk.Tk()
root.withdraw()
files = filedialog.askopenfilenames(title="Select files")
# Calculate hash for each file
hashes = {}
for file_path in files:
with open(file_path, "rb") as f:
file_hash = hashlib.sha256(f.read()).hexdigest()
hashes[file_path] = file_hash
# Store hashes in SQLite database
db_path = "hashes.db"
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS hashes (file_path TEXT PRIMARY KEY, file_hash TEXT)")
for file_path, file_hash in hashes.items():
cursor.execute("INSERT OR REPLACE INTO hashes VALUES (?, ?)", (file_path, file_hash))
conn.commit()
conn.close()
# A Hash Calculator script written in python for a plain text file output
# Made by redtrillix
# Version: v0.3.0
import hashlib
import os
import tkinter as tk
from tkinter import filedialog
# Select files using file explorer
root = tk.Tk()
root.withdraw()
files = filedialog.askopenfilenames(title="Select files")
# Calculate hash for each file
hashes = {}
for file_path in files:
with open(file_path, "rb") as f:
file_hash = hashlib.sha256(f.read()).hexdigest()
hashes[file_path] = file_hash
# Write hashes to a text file
txt_path = "hashes.txt"
header_written = os.path.isfile(txt_path) and os.path.getsize(txt_path) > 0
with open(txt_path, "a") as f:
if not header_written:
f.write("File Path\tHash Value\n")
for file_path, file_hash in hashes.items():
f.write(f"{file_path}\t{file_hash}\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment