NameError:name' sha1'没有定义

时间:2014-04-30 20:56:06

标签: python function

嗨,当我选择选项时,我会得到这个错误

追踪(最近一次通话):   文件“ran.py”,第26行,in     SHA1() NameError:未定义名称“sha1” 但是我不知道在调用之前我试图把这个函数放到什么问题但是总是一样的

def main():
 print '1 - SHA1  Decrypter'
print '2 - MD5  Decrypter'
select = input("select option :")

if select==1:
  sha1()
elif select==2:
  md5()


def   sha1():
  try:
    sha1 = raw_input("\t\n\nMD5 Hash:")
    dictionary = open("pwds.txt","r")
  except(IOError):
    print "pwds.txt not found!"
for passwd in dictionary.read().split('\n'):
    if hashlib.sha1(passwd).hexdigest() == sha1:

        print("\n\t[OK]"+sha1+" : "+passwd+"\n")
        raw_input("Decrytion Success; Press Enter To Exit")

else:
        print "\n\tFailed; Password not found in dictionary"
        main()

def md5():
  try:
    md5 = raw_input("\t\n\nMD5 Hash:")
    dictionary = open("pwds.txt","r")
  except(IOError):
    print "pwds.txt not found!"
for passwd in dictionary.read().split('\n'):
    if hashlib.md5(passwd).hexdigest() == md5:

        print("\n\t[OK]"+md5+" : "+passwd+"\n")
        raw_input("Decrytion Success; Press Enter To Exit")

else:
        print "\n\tFailed; Password not found in dictionary"
        main()
main()

1 个答案:

答案 0 :(得分:0)

代码中有多个错误

import hashlib

def main():
    print '1 - SHA1  Decrypter'
    print '2 - MD5  Decrypter'
    select = input("select option :")

    if select==1:
      sha1()
    elif select==2:
      md5()


def   sha1():
    try:
        sha1 = raw_input("\t\n\nMD5 Hash:")
        dictionary = open("pwds.txt","r")

        for passwd in dictionary.read().split('\n'):
            if hashlib.sha1(passwd).hexdigest() == sha1:

                print("\n\t[OK]"+sha1+" : "+passwd+"\n")
                raw_input("Decrytion Success; Press Enter To Exit")

        else:
            print "\n\tFailed; Password not found in dictionary"
            main()
    except(IOError):
        print "pwds.txt not found!"

def md5():
    try:
        md5 = raw_input("\t\n\nMD5 Hash:")
        dictionary = open("pwds.txt","r")
        for passwd in dictionary.read().split('\n'):
            if hashlib.md5(passwd).hexdigest() == md5:
                print("\n\t[OK]"+md5+" : "+passwd+"\n")
                raw_input("Decrytion Success; Press Enter To Exit")

        else:
            print "\n\tFailed; Password not found in dictionary"
            main()
    except(IOError):
        print "pwds.txt not found!"
main()
  1. python中的缩进很重要,因为它决定了块的范围。我不知道你是否有错误的缩进或者在发布时犯了错误。
  2. 您必须导入hashlib
  3. 你必须在try块中声明并使用字典。