Python练习题答案: 真正的密码破解者【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战

真正的密码破解者【难度:2级】:

答案1:

import hashlib
import itertools

def password_cracker(hash):
    for length in range(6):
        for candidate in map("".join, itertools.product("abcdefghijklmnopqrstuvwxyz", repeat=length)):
            if hashlib.sha1(candidate.encode()).hexdigest() == hash:
                return candidate​

答案2:

from binascii import unhexlify
from hashlib import sha1
from itertools import product
from string import ascii_lowercase

def password_cracker(hash):
    b = unhexlify(hash)
    for i in range(6):
        for xs in product(ascii_lowercase.encode(), repeat=i):
            if sha1(bytes(xs)).digest() == b:
                return ''.join(bytes(xs).decode())​

答案3:

import hashlib
import itertools
def password_cracker(hash):
    q='abcdefghijklmnopqrstuvwxyz'
    for g in range(0,5):
        for test in itertools.product(q,repeat=g):
            if hashlib.sha1(''.join(test).encode('utf-8')).hexdigest()==hash: return ''.join(test)
    for s in q:
      for test in itertools.product(q,repeat=4):
        if hashlib.sha1((s+''.join(test)).encode('utf-8')).hexdigest()==hash: return s+''.join(test)
​

答案4:

from hashlib import sha1
from itertools import product
from string import ascii_lowercase as alc

hash_gen = ((code, sha1(code.encode()).hexdigest()) for code in (''.join(p) for r in range(6) for p in product(alc, repeat=r)))
rainbow = {}

def password_cracker(hash):
    code = rainbow.get(hash, None)
    if code is not None:
        return code
    for code, h in hash_gen:
        rainbow[h] = code
        if h == hash:
            return code​

答案5:

import string
import itertools
import hashlib

def password_cracker(hash):
    chars = string.ascii_lowercase
    for password_length in range(0, 6):
        for guess in itertools.product(chars, repeat=password_length):
            guess = ''.join(guess)
            result = hashlib.sha1(guess.encode()) 
            if result.hexdigest() == hash:
                return guess​

答案6:

import itertools
import string
import hashlib

def password_cracker(hash):
    generating = True
    minlength = 0
    while generating:
        for subset in itertools.product(string.ascii_lowercase, repeat=minlength):
            word = ''.join(subset)
            if hash == gethash(word.encode()):
                return word
        minlength +=1
        if minlength > 5:
            generating = False
    return ''
                 
def gethash(word):
    hash_object = hashlib.sha1(word)
    pbHash = hash_object.hexdigest()
    return str(pbHash)​

答案7:

import string
import hashlib
from itertools import product

def password_cracker(hash):
    alphabet = string.ascii_lowercase
    
    for long in range(6):
    
        cases = product(alphabet, repeat = long)
    
        for case in cases:
        
            temp_hash = hashlib.sha1(''.join(case).encode('utf-8')).hexdigest()
            
            if(hash == temp_hash):
                return ''.join(case)​

答案8:

from itertools import product as P
from hashlib import sha1 as S
def password_cracker(h,A='abcdefghijklmnopqrstuvwxyz'):
    for i in range(6):
        for x in map(''.join,P(A,repeat=i)):
            if S(x.encode('ascii')).hexdigest()==h:return x​

答案9:

import hashlib
from itertools import product
from string import ascii_lowercase


def password_cracker(hash):
    for n in range(0,6):
        all = (''.join(i) for i in product(ascii_lowercase, repeat = n))
        for item in all:
            hash_object = hashlib.sha1(bytearray(item, 'utf-8'))
            check = hash_object.hexdigest()
            if check == hash:
                return item
                break​

答案10:

import hashlib
from itertools import product
from string import ascii_lowercase


def password_cracker(hash):
    for n in range(0,6):
        all = (''.join(i) for i in product(ascii_lowercase, repeat = n))
        for item in all:
            hash_object = hashlib.sha1(bytearray(item, 'utf-8'))
            check = hash_object.hexdigest()
            if check == hash:
                return item
                break
#     return "Password Not Found"
    
print(password_cracker('e6fb06210fafc02fd7479ddbed2d042cc3a5155e'))​

上一篇:python – 生成零和b的所有可能组合


下一篇:使用Python中的itertools / more-itertools将多列列表的项目组合并组合