Search code examples
pythonhashhashliblogin-system

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. I can't seem to figure out how to hash a variable such as passwordEntry. All the hashlib examples are just of blake2b hashing characters. For example: blake2b(b'IWantToHashThis') I am quite confused on why the "b" letter has to be included in the hash. If I try to hash a variable the "b" letter can't be concluded with the variable I want to hash. 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." Like I said before the "b" letter has to be included in the hashing algorithm for it to preform correctly. Sorry for the long question if it is hard to follow I understand.


Solution

  • The letter b only works before quotes, [", ', """, '''']. And it is there to notate that this string is bytes. If you want to convert your string to bytes you can do that by b"string" or "string".encode(). However, in your case you can only use the encode() method of str since b only works for Literal Strings. So in your case it will be blake2b(passwordEntry.encode())