Python Forum
Python3 hashlib - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python3 hashlib (/thread-36769.html)



Python3 hashlib - ogautier - Mar-28-2022

Quote:Hello, I'm trying to read and compare from an online list, through a given hash, but I'm getting this error:
Quote:Does anybody can help me please?

Error:
Traceback (most recent call last): File "c:\Users\Orlando\Documents\SandBox\MasterHckPython\hack\bruteforce1.py", line 23, in <module> main() File "c:\Users\Orlando\Documents\SandBox\MasterHckPython\hack\bruteforce1.py", line 16, in main z = hashlib.md5(p).encode() TypeError: Strings must be encoded before hashing
Quote:Used Hash: 21232f297a57a5a743894a0e4a801fc3

import hashlib
import urllib.request


def main():
    hashpass = input('Hash: ')
    req = urllib.request.urlopen('https://raw.githubusercontent.com/danielmiessler/SecLists/master/Passwords/Common-Credentials/10-million-password-list-top-1000000.txt')
    
    passwordlist = req.read().decode('utf-8')
    print(type(passwordlist))
    for p in passwordlist.split('\n'):        
        z = hashlib.md5(p).hexdigest()
        if z == hashpass:
            print('Password: {}     HASH: {}'.format(p,z))
    
    

if __name__ == '__main__':
    main()
Quote:Regards!



RE: Python3 hashlib - snippsat - Mar-28-2022

hashlib most have bytes as input.
line 12:
z = hashlib.md5(p.encode()).hexdigest()
I would write it like this using Requests then get correct encoding back,and in general i never use urllib.
Can drop read() as just loop over text.
import requests
import hashlib

def hash_test(hashpass):
    req = requests.get('https://raw.githubusercontent.com/danielmiessler/SecLists/master/Passwords/Common-Credentials/10-million-password-list-top-1000000.txt')
    for password in req.text.split('\n'):
        hash_md5 = hashlib.md5(password.encode()).hexdigest()
        if hash_md5 == hashpass:
            print(f'Password: {password:<8} HASH: {hash_md5}')

if __name__ == '__main__':
    hashpass = input('Hash: ')
    hash_test(hashpass)
Test.
G:\div_code\answer
λ python hash_test.py
Hash: 81dc9bdb52d04dc20036dbd8313ed055
Password: 1234     HASH: 81dc9bdb52d04dc20036dbd8313ed055