1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2025-04-08 15:59:05 +05:30
gists/python-programming/hash_to_password.py

40 lines
1020 B
Python

#!/usr/bin/env python3
# hash_to_password.py
#
# Author: Intel A80486DX2-66
# License: Creative Commons Zero 1.0 Universal or Unlicense
# SPDX-License-Identifier: CC0-1.0 OR Unlicense
from hashlib import sha3_512
divide_chunks = lambda x, n: [x[i:i + n] for i in range(0, len(x), n)]
hash_file = lambda file_path: sha3_512(open(file_path, "rb").read()).hexdigest()
hash_to_password = lambda hash_string, start_from, end_before: ''.join(
[chr((int(''.join(byte), 16) % (end_before - start_from)) + start_from)
for byte in divide_chunks(hash_string, 2)]
)
if __name__ == "__main__":
from os.path import basename
from sys import argv
if len(argv) < 2:
print(f"Usage: {basename(__file__)} <file to hash as password>")
raise SystemExit
ASCII_RANGE = {
"start_from": 0x21,
"end_before": 0x7F
}
print(
hash_to_password(
hash_file(argv[1]),
ASCII_RANGE["start_from"],
ASCII_RANGE["end_before"]
)
)