[Python] How to Read from File1, Hash, the Write to File2

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to write a Python script which you use one file as input, read a line, hash that word, the write to one line on the second new file, and continue this loop until you have a list of hashed words from the first file to the second file.

    Here is my code:

    import hashlib
    import sys

    def main():
    global word_list
    word_list = input("Input file name to pull words from: ")
    global hash_list
    hash_list = input("Filename to save as: ")
    hashing_algo = input("Select hashing algorithm: '1' (sha512), '2' (md5): ")

    answer = 'true'
    while answer == 'true':
    if hashing_algo == '1':
    sha512()
    sys.exit("Exiting software...")
    elif hashing_algo == '2':
    md5()
    sys.exit("Exiting software...")
    else:
    print('Select a valid option.')
    hashing_algo = input("Select hashing algorithm: '1' (sha512), '2' (md5): ")


    def sha512():
    h = hashlib.sha512()
    f = open(word_list, 'r')
    g = open(hash_list, 'w')

    for word in f:
    # f.readlines()
    word = word.strip('\n')
    print(word)

    h.update(word.encode())
    hashed_word = h.hexdigest()
    g.write(hashed_word)
    # g.write("\n")


    def md5():
    h = hashlib.md5()
    f = open(word_list, 'r')
    g = open(hash_list, 'w')

    for word in f:
    # f.readlines()
    word = word.strip('\n')
    print(word)

    h.update(word.encode())
    hashed_word = h.hexdigest()
    g.write(hashed_word)
    # g.write("\n")

    if __name__ == "__main__":
    main()


    It looks like it is hashing the first word, but it either does not has the second word or combines them and then it does not print them, hash by hash.

    Continue reading...

Compartilhe esta Página