MD5 Python Brute Force Error

I've been programming python for a while now and decided to make an MD5 firewall. This is my code here

import hashlib
import sys
import os
import time
import urllib2
import urllib
import re
import string 
import random
from itertools import islice

string = raw_input("HASH TO DECRYPT: ")

size = input("PASSWORD LENGTH: ")

char_list = ("a")

def random_char(size):
    selection = iter(lambda: random.choice(char_list),object())
    while True:
        yield ''.join(islice(selection, size))
random_gen = random_char(size) 

def encrypt(random_gen):
    algorithim = hashlib.md5()
    algorithim.update(random_gen)
    encrypted=algorithim.hexdigest()



def dosethestringexisit():
    if encrypt(random_gen) == string :
        print next(random_char)
    else:
        print("pass not found")

print dosethestringexisit()

      

I had several problems like this error

  algorithim.update(random_gen)
TypeError: must be string or buffer, not generator

      

I don't know how to change an arbitrary string to match the .update () algorithm.

Note that the char list is only "a" because I used md5 for the "a" hash to check if the code worked.

Also I was wondering how to make a while loop to repeat the code if the random hash is not equal to the hash to be decrypted. Many thanks.

+3


source to share


1 answer


To get an element from a generator, you need to use a function next

:

def encrypt(random_gen):
    algorithim = hashlib.md5()
    algorithim.update(next(random_gen))
    encrypted=algorithim.hexdigest()

      

You can also use the method next

(or __next__

in Python 3.x):

algorithim.update(random_gen.next())

      

The above will resolve the exception.



BTW, the code is using string

as a variable name. This shades the module string

; prevent using the module. Use a different variable name.


Brute force means using all possible inputs; instead of using random using itertools.product

, you can get all lines:

>>> import hashlib
>>> import itertools
>>> import string
>>>
>>> def dosethestringexisit(target, size):
...     for xs in itertools.product(string.ascii_letters, repeat=size):
...         s = ''.join(xs)
...         if hashlib.md5(s).hexdigest() == target:
...             return s
...
>>> dosethestringexisit('912ec803b2ce49e4a541068d495ab570', 4)
'asdf'

      

+3


source







All Articles