如何在字典中存储哈希字符串

问题描述 投票:0回答:1

我是python的新手,正在尝试将哈希字符串存储在字典中。我不知道该怎么做,也没有运气来搜索它,有人可以帮助我吗?这是我的代码:

'''

  import hashlib

  has_account = input('Do you have an account already (Y or N)?: ')
  has_account = str.title(has_account)


  if has_account == 'N':
 new_user = {}
 new_username = input('Please create a username: ')
 new_password = input('Please enter a password at least 6 digits long: ')
 while len(new_password) < 6:
     new_password = input('Please enter a password at least 6 digits long: ')
  reentered_password = input('Please reenter you password: ')
  while new_password != reentered_password:
     print('Passwords are different, please renter both passwords')
      new_password = input('Please enter a password at least 6 digits long: ')
      while len(new_password) < 6:
         new_password = input('Please enter a password at least 6 digits long: ')
      reentered_password = input('Please reenter you password: ')

   buffer = new_password.encode('utf-8')
  hash_object = hashlib.sha1(buffer)
  buffer = hash_object.hexdigest()
  hashed_password = buffer
  del new_password, buffer, reentered_password
  new_user[new_user] = hashed_password
  del new_user, hashed_password

'''

它仅输出:“ new_user [new_user] = hashed_pa​​sswordTypeError:无法散列的类型:“ dict””通过在此处粘贴空格可能弄乱了空格,这是原始代码的屏幕截图:enter image description here感谢您的任何提前帮助

python dictionary hash hashlib
1个回答
0
投票

字典的想法是,您将值与用于查找它的键相关联,就像在真正的词典中如何查找单词(键)以查找其定义(值)一样。] >

您遇到了错误,因为您试图将整个字典本身用作在其中查找内容的键;这没有任何意义,因此不起作用。您可能想做更多类似的事情:

new_user["password"] = hashed_password

尚不清楚这有什么用,但是,如果您要做的只是将字符串存储在字典中,那么就等于那个。

© www.soinside.com 2019 - 2024. All rights reserved.