# Author: Andrieiev Danil Maksimovich # danssg08@gmail.com | https://github.com/DanilAndreev import string def reduce(hash_string: str, iteration: int, alphabet=None, word_length: int = 6) -> str: """ reduce - reduces input hash to next chain password. :param hash_string: Input hash string. :param iteration: Current iteration step. :param alphabet: Input alphabet. :param word_length: Target password length. :returns: Reduced password. """ if alphabet is None: alphabet = list(string.ascii_letters) # Shifting input hash value by iteration and modulo by 2^40. value = (int(hash_string, 16) + iteration) % (2 ** 40) result = [] for i in range(word_length): # Getting modulo by alphabet length. Result number will be between 0 and len(alphabet). mod = value % len(alphabet) # Dividing value by alphabet length. value //= len(alphabet) # Getting symbol from input alphabet by calculated value in range from 0 to len(alphabet). result.append(alphabet[mod]) # Generating word from calculated symbols list. return "".join(result)