0

I'm trying to follow the steps here https://en.bitcoin.it/wiki/Bech32 to generate a valid bech32 address. I am getting stuck on the first step:

  1. Having a compressed public key (0x02 or 0x03 followed by 32 byte X coordinate): 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
  2. Perform SHA-256 hashing on the public key: 0f715baf5d4c2ed329785cef29e562f73488c8a2bb9dbc5700b361d54b9b0554

Here is one of the things I tried:

>>> import hashlib
>>> m = hashlib.sha256()
>>> m.update('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')
>>> m.hexdigest()
'd13c888cfd35d6ab67dc3f67edcc401833e6ae4eec20b254b1981b187946ed91'

Note:

  1. I'm limited to python 2.7.18
  2. I'm making these addresses for testing purposes, they are not needed for real use but should be valid
  3. If you know how to do steps 3+, please share them as well :)
  4. There is a putative reference implementation of this here: https://github.com/fiatjaf/bech32/blob/master/bech32/__init__.py, but I can't make heads or tails of it... it seems to be completely different from the process described.
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
tjb
  • 11,480
  • 9
  • 70
  • 91
  • What's exactly the problem? RIPEMD-160 is supported by the hashlib bundled with Python 2.7.17+: `hashlib.new('ripemd160', data).hexdigest()` Are You seeking for more detailed instruction/explanation what's happening on the mentioend wiki? – Kamiccolo Dec 22 '21 at 13:27
  • The problem is I'm not getting the values I should be getting according to the wiki. >>> hashlib.new('ripemd160', b'0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798').hexdigest() ====> '0b33a7d1c4ee5cdd857744daa64b5a2ef0285149' but the wiki states that the result should be '751e76e8199196d454941c45d1b3a323f1433bd6' .... I would like to go through the example generating the values exactly so I know I'm doing every step correctly – tjb Dec 22 '21 at 13:34
  • could You please update Your question accordingly? What exactly are You doing, what do You expect, and what exactly are You getting. External wiki does make the question pretty useless for the future. – Kamiccolo Dec 22 '21 at 13:40
  • But for now it looks like You're hashing a string not a binary stream, which representation in hex You have there. – Kamiccolo Dec 22 '21 at 13:41
  • Something between of lines of this: `hashlib.new('sha256', '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'.decode('hex')).hexdigest()` - (decoding the string with hex representation of the binary to actual binary) – Kamiccolo Dec 22 '21 at 13:48

1 Answers1

2

It seems that you're hashing the string representation of the binary, instead of binary stream itself. Not sure what's the most pythonic way to do that in Python 2.7, but you'd get what you need with something like the following:

import hashlib

str_of_key = '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'
hashlib.new('sha256', str_of_key.decode('hex')).hexdigest()

For the reference, related question about .decode('hex'): https://stackoverflow.com/a/5682984/1150918

chepner
  • 497,756
  • 71
  • 530
  • 681
Kamiccolo
  • 7,758
  • 3
  • 34
  • 47