NameError: 名称' sha1 '未定义

-2 投票
1 回答
8994 浏览
提问于 2025-04-18 04:56

你好,当我选择选项时,总是会出现这个错误。

错误追踪(最近的调用在最前面): 文件 "ran.py",第 26 行,在 sha1() 名字错误:'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 个回答

3

代码里有多个错误

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块里面声明并使用字典。

撰写回答