Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Confusion about Hashlib
#1
I have never used haslib before. If I understand it is a way to encode an object for compression? I have the following line giving an error:

multi_inc_prot = 'MULTI_INC_PROT_%s' % hashlib.md5(sys.argv[0]).hexdigest()
The error is:

Error:
Traceback (most recent call last): File "/home/pace.com/rob/sandbox/branches/first-branch/CodeBase/BMFamily/src1/unittests/make_path0014.py", line 51, in <module> MakeSimulatedPositionsSourceCode(MakeSim, 'path0014', 'Path0014') File "../../../py0/Tools/helpers_make_path.py", line 181, in MakeSimulatedPositionsSourceCode multi_inc_prot = string % hashlib.md5(sys.argv[0]).hexdigest() TypeError: Unicode-objects must be encoded before hashing
I tried:

string = 'MULTI_INC_PROT_%s'.encode('utf-8')
  
  multi_inc_prot = string % hashlib.md5(sys.argv[0]).hexdigest()
which did not work. Apparently, I am misunderstanding the error. Could someone please explain?

Never mind I figured it out. It was the argument that needed encoding:

multi_inc_prot = 'MULTI_INC_PROT_%s' % hashlib.md5(sys.argv[0].encode('utf-8')).hexdigest()
Reply
#2
usage instructions here: http://code.krypto.org/python/hashl
also, please When showing code, provide enough to run a stand alone snippet (keep it as short as possible, while still creating the error)
Reply
#3
Error:
TypeError: Unicode-objects must be encoded before hashing
You are trying hash a str, hashlib.HASHFUNC expects bytes.
Just use sys.argv[0].encode().
This encodes the unicode str to bytes.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] How to crack hash with hashlib Milan 0 1,403 Mar-09-2023, 08:25 PM
Last Post: Milan
  Python3 hashlib ogautier 1 1,531 Mar-28-2022, 04:42 AM
Last Post: snippsat
  how can I generate a password in hashlib go127a 20 8,591 May-19-2019, 09:26 AM
Last Post: buran
  Using SHA3 (keccak) from Hashlib CryptoFlo 0 7,682 Mar-14-2018, 10:56 AM
Last Post: CryptoFlo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020