0

I want encode block_string variable to keccak.

This is my current function. In this code I use sha256 (hashlib library) for encode block_string value but I want to use keccak instead of sha256:

def hash(block):
        block_string = json.dumps(block, sort_keys=True).encode()
        return hashlib.sha256(block_string).hexdigest()
xdevman
  • 11
  • 3
  • Welcome to the site. "didn't work" is a very vague description of a problem. Perhaps you would like to [edit] your question and explain better. – khelwood Mar 13 '22 at 09:05
  • 1
    Also `print keccak_hash.hexdigest()` would give a syntax error in Python 3. Unless that is the problem you are asking about, this does not seem to be a good code example to demonstrate your problem. – khelwood Mar 13 '22 at 09:08
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 13 '22 at 09:56

1 Answers1

0

It needs to be a byte literal. Try: a = b'some date'

Alternatively you could do a=a.encode('UTF-8') to achieve the same purpose.

Full corrected code:

from Crypto.Hash import keccak
a = b'some date'
# Alternatively
# a = 'some date'
# a = a.encode('UTF-8')
keccak_hash = keccak.new(digest_bits=512)
keccak_hash.update(a)
print (keccak_hash.hexdigest())
Samson
  • 1,336
  • 2
  • 13
  • 28