简体   繁体   English

使用 hashlib 进行 Python 密码散列

[英]Python Password Hashing with hashlib

I'm trying to hash a password for a login system I am creating.我正在尝试为我正在创建的登录系统散列密码。 I am using the hashlib import and using the blake2b hash algorithm.我正在使用 hashlib 导入并使用 blake2b 哈希算法。 I can't seem to figure out how to hash a variable such as passwordEntry .我似乎无法弄清楚如何散列诸如passwordEntry 之类的变量。 All the hashlib examples are just of blake2b hashing characters.所有 hashlib 示例都只是 blake2b 散列字符。 For example: blake2b(b'IWantToHashThis') I am quite confused on why the "b" letter has to be included in the hash.例如: blake2b(b'IWantToHashThis')我很困惑为什么“b”字母必须包含在哈希中。 If I try to hash a variable the "b" letter can't be concluded with the variable I want to hash.如果我尝试散列一个变量,则“b”字母不能与我想要散列的变量结束。 Example of me trying to hash a variable: blake2b(passwordEntry) Another example of me trying to hash the variable: blake2b(b passwordEntry) On the second example I just gave hashlib thinks that it is trying to hash the variable "b passwordEntry."我试图对变量进行散列的示例: blake2b(passwordEntry)我试图对变量进行散列的另一个示例: blake2b(b passwordEntry)在第二个示例中,我刚刚给 hashlib 认为它正在尝试对变量“b passwordEntry”进行散列。 Like I said before the "b" letter has to be included in the hashing algorithm for it to preform correctly.就像我之前所说的那样,散列算法中必须包含“b”字母才能正确执行。 Sorry for the long question if it is hard to follow I understand .如果很难理解,我很抱歉这个长问题

The letter b only works before quotes, [ " , ' , """ , '''' ].字母b仅在引号前有效,[ " , ' , """ , '''' ]。 And it is there to notate that this string is bytes .并且要注意这个字符串是bytes If you want to convert your string to bytes you can do that by b"string" or "string".encode() .如果要将字符串转换为字节,可以通过b"string""string".encode() However, in your case you can only use the encode() method of str since b only works for Literal Strings.但是,在您的情况下,您只能使用strencode()方法,因为b仅适用于文字字符串。 So in your case it will be blake2b(passwordEntry.encode())所以在你的情况下它将是blake2b(passwordEntry.encode())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM