timelock/timelock.py

51 lines
1.5 KiB
Python

#!/usr/bin/env python3
import hashlib
import time
import getpass
def get_input():
total_count = 0
while not total_count:
try:
total_count = int(float(input("Enter count: ")))
except ValueError:
print("Enter a positive integer.")
seed, seed_check = "a", "b"
while seed != seed_check:
seed = getpass.getpass()
seed_check = getpass.getpass(prompt="Confirm password: ")
if seed != seed_check:
print("Passwords must match.")
return total_count, seed
def print_progress(total_count, passed_time, current_count):
projected_time = total_count * passed_time / current_count
progress_time = "[{:.2f} / {:.2f}s".format(passed_time, projected_time)
progress_percent = current_count / total_count * 100
progress_percent = "- {:.2f}%]".format(progress_percent)
print(progress_time, progress_percent, " ", end='\r')
if __name__ == "__main__":
total_count, seed = get_input()
start_time = time.time()
current_time = start_time
m = hashlib.sha256()
m.update(seed.encode())
for current_count in range(total_count):
if time.time() - current_time > 1:
current_time = time.time()
passed_time = current_time - start_time
print_progress(total_count, passed_time, current_count)
m.update(m.digest())
time_passed = time.time() - start_time
print(f"{total_count} hashes done in {time_passed:.2f}s. Final result:")
print(m.hexdigest())