Skip to content

Instantly share code, notes, and snippets.

@vadim-su
Created March 6, 2018 18:23
Show Gist options
  • Select an option

  • Save vadim-su/fe58fc18467262642ef0394e53e098e3 to your computer and use it in GitHub Desktop.

Select an option

Save vadim-su/fe58fc18467262642ef0394e53e098e3 to your computer and use it in GitHub Desktop.
Block hashing algorithm for python3 (https://en.bitcoin.it/wiki/Block_hashing_algorithm)
import hashlib
from hashlib import sha256
import codecs
def dsha256(data):
return sha256(sha256(data).digest()).digest()
def ripemd(data):
return hashlib.new('ripemd160', data).digest()
def main():
header_hex = (
"01000000" +
"81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000" +
"e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b" +
"c7f5d74d" + "f2b9441a" + "42a14695"
)
header_bin = codecs.decode(header_hex, 'hex')
hash = dsha256(header_bin)
print(hash.hex())
print(hash[::-1].hex())
if __name__ == '__main__':
main()
@vadim-su

vadim-su commented Mar 6, 2018

Copy link
Copy Markdown
Author
# python3 block_hash.py
1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e0000000000000000
00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment