2

Logic

There are different lists with different function names in each of them like this:

md5 = [gamma, alpha, beta, theta, delta] sha1 = [alpha, beta, theta, delta] sha256 = [alpha, beta, theta] sha384 = [alpha, beta, theta] sha512 = [alpha, beta, theta] 

That means, if a function for is to call md5 list, then all the functions inside the md5 list will run.

One of these lists will be called according to a dictionary of list. But in order to determine which list of functions to take, there is a dictionary created like this:

hashdict = {} hashdict['md5'] = [ 'md5' , 32 , md5 ] hashdict['sha1'] = [ 'sha1' , 40 , sha1 ] hashdict['sha256'] = [ 'sha256' , 64 , sha256 ] hashdict['sha384'] = [ 'sha384' , 96 , sha384 ] hashdict['sha512'] = [ 'sha512' , 128 , sha512 ] 

Function

This function takes the hash submitted by the user, checks if the string length macthes the length in any dictionary lists inside hashdict{}. If the length matches for example: 32 characters, then it will be md5 hash.

After the script knows the correct list in the dictionary, it will take the function (the third list item - second index of the list) from the perticular list in hashdict and pass it into a function call named api.

def crack(hashvalue): result = False hashInDict = False for hashList in list(hashdict.values()): if len(hashvalue) in hashList: hashInDict = True if not file: print(f'{info} Hash Function : {hashList[0].upper()}') for api in hashList[2]: r = api(hashvalue, hashList[0]) if r: return r else: if hashInDict == False: if not file: print(f'{bad} This hash type is not supported.') quit() else: return False 

Problem

It works only if the hash is md5 and any other hash passed results in the message "This hash type is not supported". Somehow. if I remove the dictionary and use if-else instead to compare the hash string length, it all works fine. But using dictionary lists has resulted in all other hashes ignored and only the first dicitionary list working.

If I replace the first dictionary list with for example sha1, then only sha1 hashes will be detected and all others will result in hash not supported print.

I have no idea why it's ignoring anything after the first dictionary list item.

4
  • what is hashdict? Please provide all relevant code, there seems to not be enough here to make a logical assumption as to what's going on and what expected result is supposed to be. Also, you can't "call" a list... What do you mean by this? Commented May 28, 2019 at 3:52
  • Please add an input and the desired output as well. Makes it a lot easier to help. Commented May 28, 2019 at 13:48
  • @Jab >> Thanks for the reply. I have edited the entire question with relevant code. The main idea is for the user to enter a hash, then the script detects the type of hash using the string length and accordingly runs different list of functions for cracking each type of hash. Commented May 30, 2019 at 5:52
  • @Error-SyntacticalRemorse >> Thanks for reply, I have edited the entire question with relevant code. Commented May 30, 2019 at 5:53

1 Answer 1

1

It seems you’re really over complicating this, all you seem to be doing is if the length of the input matches a predefined number then call api passing the inputed string and a list of funcs then returning it’s value otherwise returning False.

Just make a dict that looks like this:

hash_dict = { 32: [gamma, alpha, beta, theta, delta], 40: [alpha, beta, theta, delta], 64: [alpha, beta, theta], 96: [alpha, beta, theta], 128: [alpha, beta, theta] } 

Then in the crack function just call .get with len(hash_value) and of its in the dict it returns the value, otherwise returns None or a specified default.

def crack(hash_value): hash_list = hash_dict.get(len(hash_value)) if hash_list: return api(hash_value, hash_list) return False 

I’m making an assumption here as to what api does but if it’s just piping the data through each function in the list then it’s easy to do this in crack. Instead of return api(hash_value, hash_list) use:

for func in hash_list: hash_value = func(hash_value) return hash_value 

Or even better just put actual methods in the dict like this (I’m using lambdas here for the sake of space, although, I’d suggest actual functions for readability):

hashdict = { 32: lambda x: gamma(alpha(beta(theta(delta(x))))) 40: lambda x: alpha(beta(theta, delta(x))), 64: lambda x: alpha(beta(theta(x))), 96: lambda x: alpha(beta(theta(x))), 128: lambda x: alpha(beta(theta(x))) } 

Then just do something like this:

def crack(hash_value): def _default(_): False hash_func = hash_dict.get(len(hash_value), _default) return hash_func(hash_value) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.