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

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

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

CC BY-SA 3.0
2016-03-15T11:44:07.027Z
2016-03-15T11:44:07.027Z
cdecker
<p>Assuming you already have a public key, here's how you can generate an address using Python:</p>

<pre><code>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) )
</code></pre>

CC BY-SA 3.0
2017-11-24T04:29:03.320Z
2017-11-24T04:29:03.320Z
shmuli
<p>I would agree with the others above.   </p>

<p>Put very simply, <strong>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.</strong></p>

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

<p>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.</p>

<p>Hope this helps.</p>

<p>Cheers</p>

CC BY-SA 3.0
2016-03-22T04:50:18.237Z
2016-03-22T04:50:18.237Z
SDsolar
<p>I'm currently studying Bitcoin and I followed <a href="https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses#How_to_create_Bitcoin_Address" rel="nofollow">Bitcoin wiki's instructions</a> to make an address.</p>

<p>But my program never responds as the site above does.</p>

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

<p>Need I convert the hex into something else like binary to generate the hash?</p>

<address><sha256>
How to generate bitcoin address?
suish
# How to generate bitcoin address?

(Asked by: username_0 on 2016-03-15)

I'm currently studying Bitcoin and I followed [Bitcoin wiki's instructions](https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses#How_to_create_Bitcoin_Address) 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?

**Tags:** address, sha256


---

## 3 answers

### Answer by username_1 on 2016-03-15. Score: 2 | Accepted answer

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.


---

### Answer by username_2 on 2017-11-24. Score: 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) )

```


---

### Answer by username_3 on 2016-03-22. Score: 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


---

