3

I'm currently studying Bitcoin and I followed Bitcoin wiki's instructions to make an address.

But my program never responds as the site above does.

Supposedly, the ECDSA key 0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6's SHA-256 should be 600FFE422B4E00731A59557A5CCA46CC183944191006324A447BDB2D98D4B408, but my code returns 32511e82d56dcea68eb774094e25bab0f8bdd9bc1eca1ceeda38c7a43aceddce and I don't think it's wrong because alot of online SHA-256 service returns the same.

Need I convert the hex into something else like binary to generate the hash?

2

The hashes are computed on the raw byte value of the key, not the hex-encoded string:

from hashlib import sha256
key = '0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6'
sha256(key).hexdigest()
# Output: '32511e82d56dcea68eb774094e25bab0f8bdd9bc1eca1ceeda38c7a43aceddce'
sha256(key.decode('hex')).hexdigest()
# Output: '600ffe422b4e00731a59557a5cca46cc183944191006324a447bdb2d98d4b408'

So the trick is to hex-decode the string into its original form before hashing it.

0

I would agree with the others above.

Put very simply, you are very likely feeding in a text string, with its own binary representation of the characters; this has no useful relation to the actual binary bit patterns represented by the text encoding of the characters. So, as stated above, take your text of the hex symbols and convert it to a binary representation of what was originally intended.

Note that this is not unique to SHA-256. This can be a problem for any program that expects binary input.

A similar problem arises when decoding the text representation of dates in a Coinbase download. It has a character value of the datime, but all I need is the date itself in date format. Not difficult but it makes one pause to really think about what you need.

Hope this helps.

Cheers

0

Assuming you already have a public key, here's how you can generate an address using Python:

import binascii, hashlib, base58

# alias method
decode_hex = binascii.unhexlify

def gen_address(public_key):
    # perform SHA-256 hashing on the public key
    sha256 = hashlib.sha256()
    sha256.update( decode_hex(public_key) )
    hash = sha256.hexdigest()

    # public key hash (for p2pkh) - perform RIPEMD-160 hashing on the result of SHA-256
    # prepend mainnet version byte
    ripemd160 = hashlib.new('ripemd160')
    ripemd160.update( decode_hex(hash) )
    public_key_hash = ripemd160.hexdigest()
    mainnet_public_key_hash = '00' + public_key_hash

    # perform SHA-256 hash on the extended RIPEMD-160 result
    sha256 = hashlib.sha256()
    sha256.update( decode_hex(mainnet_public_key_hash) )
    hash = sha256.hexdigest()

    # perform SHA-256 on the previous SHA-256 hash
    sha256 = hashlib.sha256()
    sha256.update( decode_hex(hash) )
    hash = sha256.hexdigest()

    # create a checksum using the first 4 bytes of the previous SHA-256 hash
    # appedend the 4 checksum bytes to the extended RIPEMD-160 hash
    checksum = hash[:8]
    hash = mainnet_public_key_hash + checksum

    # convert RIPEMD-160 + checksum into base58 encoded string
    return base58.b58encode( decode_hex(hash) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.