ValueError:格式不正确的sha1_crypt哈希(校验和必须恰好是28个字符)

时间:2018-09-24 14:36:56

标签: python python-3.x passlib

我是编程新手,为了学习,我尝试制作一个认证程序,该程序使用passlib将密码和用户名存储在.txt文件中:

但是当我尝试验证我的password.txt文件中的哈希密码时,出现此错误:追踪(最近一次通话):

  File "C:/Users/PycharmProjects/Apprentissage/Authentification.py", line 107, in <module>
    get_line_number_pseudo()
  File "C:/Users/PycharmProjects/Apprentissage/Authentification.py", line 84, in get_line_number_pseudo
    get_line_number_password()
  File "C:/Users/PycharmProjects/Apprentissage/Authentification.py", line 67, in get_line_number_password
    print(sha1_crypt.verify(password, password_file_lines[0]))
  File "C:\Users\AppData\Roaming\Python\Python37\site-packages\passlib\utils\handlers.py", line 757, in verify
    self = cls.from_string(hash, **context)
  File "C:\Users\AppData\Roaming\Python\Python37\site-packages\passlib\handlers\sha1_crypt.py", line 86, in from_string
    return cls(rounds=rounds, salt=salt, checksum=chk)
  File "C:\Users\AppData\Roaming\Python\Python37\site-packages\passlib\utils\handlers.py", line 1761, in __init__
    super(HasRounds, self).__init__(**kwds)
  File "C:\UsersAppData\Roaming\Python\Python37\site-packages\passlib\utils\handlers.py", line 1376, in __init__
    super(HasSalt, self).__init__(**kwds)
  File "C:\Users\AppData\Roaming\Python\Python37\site-packages\passlib\utils\handlers.py", line 593, in __init__
    self.checksum = self._norm_checksum(checksum)
  File "C:\Users\AppData\Roaming\Python\Python37\site-packages\passlib\utils\handlers.py", line 623, in _norm_checksum
    raise exc.ChecksumSizeError(self, raw=raw)
ValueError: malformed sha1_crypt hash (checksum must be exactly 28 chars)`

注册我做得很好,但部分签收不好。 代码:`

from passlib.hash import sha1_crypt
tries = 0

pseudonyme = open("pseudo.txt")
password_file = open("password.txt")
password_file_lines = password_file.readlines()
found = False


def new_hash_password():
    global pass2
    pass2 = sha1_crypt.hash()
    new_valid_password()


def new_player_password():
    global tries, pass2
    while True:
        pass1 = input('Please enter a password: ')

        pass2 = input('Now please enter the password again to check: ')

        if pass1 == pass2:
            print('You are now logged in welcome!')
            new_hash_password()
        else:
            print('I am sorry but the password does not match')
            tries += 1
        if tries == 3:
            quit()


def new_valid_password():
    global password, pass2
    lines_list = open("password.txt").read()
    password_list = []
    password_list.append(pass2)
    password_list.append(lines_list)
    with open('password.txt', 'w') as filehandle:
        filehandle.writelines("%s\n" % place for place in password_list)
    print(password_list)
    password_file.close()

    new_valid_pseudo()


def new_valid_pseudo():
    global pseudo2
    lines_list = open('pseudo.txt').read()
    new_pseudo = pseudo2
    pseudo_list= []
    pseudo_list.append(new_pseudo)
    pseudo_list.append(lines_list)

    with open('pseudo.txt', 'w') as filehandle:
        filehandle.writelines("%s\n" % place for place in pseudo_list)
    print("Hi", pseudo2, ",Welcome on pylilgame!")
    pseudonyme.close()
    # connection to server
    quit()


def get_line_number_password():
    global password_check, i
    password = input("Please enter your password:")
    with open("password.txt"):
        print(sha1_crypt.verify(password, password_file_lines[0]))
        lookup = password_check
        for num, line in enumerate(password_file):
            if lookup in line:
                print(num)


def get_line_number_pseudo():
    global pseudo, i

    with open('pseudo.txt', 'r') as f:
        lines = f.read().split("\n")

    for i, line in enumerate(lines):
        if pseudo in line.split():  # or word in line.split() to search for full words
            print("Word \"{}\" found in line {}".format(pseudo, i + 1))

            get_line_number_password(


if __name__ == "__main__":

    hello = input("Would you like to sign in or sign up(SI or SU)")

    if hello == "SI":
        pseudo = input("PSEUDO:")
        if pseudo in pseudonyme.read():
            print("The pseudo exists")
            get_line_number_pseudo()
        else:
            print("This pseudonyme doesn't exist!")
    if hello == "SU":
        while True:
            pseudo = input("PSEUDO:")
            if pseudo in pseudonyme.read():
                print("This pseudonyme is already taken!")

            else:
                pseudo2 = input("TYPE YOUR PSEUDO AGAIN:")
                if pseudo2 == pseudo:
                    new_player_password()

                else:
                    print("QUIT")

在此先感谢您提供的帮助!

0 个答案:

没有答案