【问题标题】:How to Store a Hashed String in a Dictionary如何在字典中存储散列字符串
【发布时间】:2020-03-07 18:13:58
【问题描述】:

我是 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​​ssword 类型错误:不可散列的类型:'dict'" 粘贴在这里可能会弄乱间距,这是原始代码的屏幕截图: enter image description here 提前感谢您的任何帮助

【问题讨论】:

    标签: python dictionary hash hashlib


    【解决方案1】:

    字典的概念是将值与用于查找它的键相关联,就像在真实字典中查找单词(键)以找到其定义(值)一样。

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

    new_user["password"] = hashed_password
    

    这有什么用还不清楚,但如果你想做的只是“在字典中存储一个字符串”,那么这就符合条件。

    【讨论】:

      猜你喜欢
      • 2012-02-20
      • 2018-10-31
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 2017-05-03
      相关资源
      最近更新 更多