import os import ecdsa import hashlib import base58 from Crypto.Hash import RIPEMD160 private_key = os.urandom(32) public_key = ecdsa.SigningKey.from_string(private_key, curve=ecdsa.SECP256k1).verifying_key.to_string() prefix_and_pubkey = b"\x04" + public_key intermediate = hashlib.sha256(prefix_and_pubkey).digest() ripemd160 = RIPEMD160.new() ripemd160.update(intermediate) hash160 = ripemd160.digest() prefix_and_hash160 = b"\x00" + hash160 double_hash = hashlib.sha256(hashlib.sha256(prefix_and_hash160).digest()).digest() checksum = double_hash[:4] pre_address = prefix_and_hash160 + checksum address = base58.b58encode(pre_address) print(address.decode())
hashlibで以下のように書くとエラーになる。
ripemd160 = hashlib.new(‘ripemd160’)
$ python3 address.py
Traceback (most recent call last):
File “/usr/lib/python3.10/hashlib.py”, line 160, in __hash_new
return _hashlib.new(name, data, **kwargs)
ValueError: [digital envelope routines] unsupported
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/home/vagrant/dev/work/address.py”, line 12, in
ripemd160 = hashlib.new(‘ripemd160’)
File “/usr/lib/python3.10/hashlib.py”, line 166, in __hash_new
return __get_builtin_constructor(name)(data)
File “/usr/lib/python3.10/hashlib.py”, line 123, in __get_builtin_constructor
raise ValueError(‘unsupported hash type ‘ + name)
ValueError: unsupported hash type ripemd160
従って、pycryptodomeをインストールして、ripemd160を使う
$ pip install pycryptodome