Hashing a Bitcoin Key

I had the chance to generate the address for a bitcoin key in a code. There are steps to this. First, one must programmatically generate a public key using RSA. In RSA, the key must be a multiple of 1024, so my key was 2048 characters. Then, it was turned into a public key, and from there, hashed using SHA256 and RIPEMD160 in that order. After that, it was transformed into Base 58 using Base58Check.

The key could have been hashed without using RIPEMD160, however, that particular hash is meant to protect against Hash Length Extension Attacks, which can be protected against using a software known as HMAC.  Hash Length Extension Attacks can happen when someone takes a message that has been sent, append their own message to the end of that message, and sent it off looking as though it came from the original sender. There are more steps to it, but that is the basic idea.The challenge was fun, and I learned a lot. As far as coding go, this edges into advanced because it uses python protocols that aren’t very well known. The link to the code can be found here.

Sources

https://docs.python.org/2/library/hashlib.html

https://stackoverflow.com/questions/26539366/how-to-use-sha256-hash-in-python

def generate_RSA(bits=2048):
'''
Generate an RSA keypair with an exponent of 65537 in PEM format
param: bits The key length in bits
Return private key and public key
'''
from Crypto.PublicKey import RSA
new_key = RSA.generate(bits, e=65537)
public_key = new_key.publickey().exportKey("PEM")
private_key = new_key.exportKey("PEM")
return private_key, public_key

https://stackoverflow.com/questions/2466401/how-to-generate-ssh-key-pairs-with-python

https://kite.com/python/docs/Crypto.PublicKey.RSA.RSAImplementation.importKey

https://bitcoin.stackexchange.com/questions/37040/ripemd160sha256publickey-where-am-i-going-wrong

https://pypi.org/project/base58check/

https://stackoverflow.com/questions/19511440/add-b-prefix-to-python-variable

https://www.whitehatsec.com/blog/hash-length-extension-attacks/

https://blog.skullsecurity.org/2012/everything-you-need-to-know-about-hash-length-extension-attacks

https://crypto.stackexchange.com/questions/3978/understanding-the-length-extension-attack

Comments

Leave a comment