<p>The result you are getting (<code>3251...</code>)
is the result of sha256 on the <em>string</em> <code>0450...</code>. </p>

<p>The expected output on bitcoin.it is the result of sha256 on the <em>bytes</em> that are currently hex-encoded. <strong>To resolve this, hex-decode before hashing your input.</strong> <a href="http://ideone.com/uFKgAI" rel="nofollow">In Python 2:</a></p>

<blockquote>
<pre><code>import hashlib
in_data = "0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6".decode('hex')
my_sha = hashlib.sha256()
my_sha.update(in_data)
output = my_sha.digest().encode('hex')
</code></pre>
</blockquote>

<p>That should retain <code>output</code> with the expected value. </p>

CC BY-SA 3.0
2015-04-08T15:43:47.053Z
Yep, that's tripped me up too. You'll need to use bytes objects - `b"hexData"` - for Python 3.x
Wizard Of Ozzie
CC BY-SA 3.0
2015-04-08T15:49:32.760Z
Good point. I'm not sure how to write the code so it's compatible with Python 3 ("string".encode('hex') is replaced), so unless you do I'll just specify Python 2 here.
JohnDvorak
CC BY-SA 3.0
2015-04-08T15:58:43.493Z
`import codecs`, then `codecs.encode(codecs.decode("abcdef".encode("utf-8"), "hex")[::-1], "hex").decode()`... It's quite counterintuitive IMO, as I'm so used to Python 2.7
Wizard Of Ozzie
CC BY-SA 3.0
2015-04-06T20:42:46.917Z
2015-04-08T15:50:18.870Z
2015-04-08T15:50:18.870Z
JohnDvorak
<p>I am following <a href="https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses" rel="nofollow">this link</a>, trying to perform SHA-256 on a given public key. <br>
However, I cannot get the expected result. <br> 
I am using the sha256 library in python <b>hashlib</b></p>

<p>The input is </p>

<pre><code>0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6
</code></pre>

<p>Mine output is </p>

<pre><code>32511e82d56dcea68eb774094e25bab0f8bdd9bc1eca1ceeda38c7a43aceddce
</code></pre>

<p>The expected output on bitcoin.it is</p>

<pre><code>600FFE422B4E00731A59557A5CCA46CC183944191006324A447BDB2D98D4B408
</code></pre>

<p>Do I miss anything? </p>

<hash><public-key><encoding>
About performing SHA-256 hashing on the public key
moshaholo
# About performing SHA-256 hashing on the public key

(Asked by: username_0 on 2015-04-06)

I am following [this link](https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses), trying to perform SHA-256 on a given public key.   
However, I cannot get the expected result.   
I am using the sha256 library in python **hashlib**

The input is

```
0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6

```

Mine output is

```
32511e82d56dcea68eb774094e25bab0f8bdd9bc1eca1ceeda38c7a43aceddce

```

The expected output on bitcoin.it is

```
600FFE422B4E00731A59557A5CCA46CC183944191006324A447BDB2D98D4B408

```

Do I miss anything?

**Tags:** hash, public-key, encoding


---

## 1 answers

### Answer by username_1 on 2015-04-06. Score: 7 | Accepted answer

The result you are getting (`3251...`)
is the result of sha256 on the *string* `0450...`.

The expected output on bitcoin.it is the result of sha256 on the *bytes* that are currently hex-encoded. **To resolve this, hex-decode before hashing your input.** [In Python 2:](http://ideone.com/uFKgAI)

> ```
> import hashlib
> in_data = "0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6".decode('hex')
> my_sha = hashlib.sha256()
> my_sha.update(in_data)
> output = my_sha.digest().encode('hex')
>
> ```

That should retain `output` with the expected value.

> **Comments:**
> **username_2**: Yep, that's tripped me up too. You'll need to use bytes objects - `b"hexData"` - for Python 3.x  
> **username_1**: Good point. I'm not sure how to write the code so it's compatible with Python 3 ("string".encode('hex') is replaced), so unless you do I'll just specify Python 2 here.  
> **username_2**: `import codecs`, then `codecs.encode(codecs.decode("abcdef".encode("utf-8"), "hex")[::-1], "hex").decode()`... It's quite counterintuitive IMO, as I'm so used to Python 2.7  

---

