<blockquote>
  <p>sha is a function where you give a message and it takes the ascii
  codes of the message and does the math operations on these codes.</p>
</blockquote>

<p>Wrong. sha256 is a hash function. This means it takes data of (fairly, there can be a granularity) arbitrary length and returns data of constant length. In the case of sha256, the granularity is 1 bit (though the granularity of most implementations of it is 1 Byte; Thank you, dave_thompson_085) and the data it returns is always 256 bit = 32 Byte. No ASCII encoding required.</p>

<p>You feed your hash function the wrong data. You feed it a string containing the ASCII-encoded hexadecimal representation of the data you're supposed to feed into the function.</p>

<p>If you have a hexadecimal representation encoded in ASCII, you first have to convert it to the data it represents and then feed that into the hash function.</p>

CC BY-SA 3.0
2017-01-15T19:54:24.550Z
Ok, I get it... partly. So my mistake was that I had a piece of data for example `0xab` and I was passing string `"ab"` to the function which is entirely wrong. But still, the sha function takes a string as an input so when I have a block header encoded as `ab`should I find a character that has code 1010 1011 and pass it to the function?... No, I think there must be another way and I apparently still get it wrong.
M-guy
CC BY-SA 3.0
2017-01-15T20:05:29.117Z
Remember to hash it twice (hash it, then copy the SHA2-256 result back to the input box and hash again)
lmnopcdefg
CC BY-SA 3.0
2017-01-15T20:18:40.867Z
@M-guy You can't use a sha256sum function which only accept a string as its input. Use an implementation of sha256 which accepts arbitrary data.
UTF-8
CC BY-SA 3.0
2017-01-15T20:48:38.667Z
Nitpick: SHA-256 itself (in fact all the SHA-2 family, and SHA-1, and the MD hashes before them, and SHA-3 also) is defined for bit granularity. However, practically all _software implementations_ limit to bytes, and all bitcoin hashing only needs bytes. @M-guy: all good C++ (or C) crypto libs I've seen support arbitrary bytes; if you identify which one you're using and how it may be that you're just using it wrong.
dave_thompson_085
CC BY-SA 3.0
2017-01-15T21:16:25.357Z
@dave_thompson_085 I didn't use a library, I was using this implementation: http://www.zedwood.com/article/cpp-sha256-function
M-guy
CC BY-SA 3.0
2017-01-16T01:34:28.783Z
That code is okay except the silly `std::string` wrapper is useless for bitcoin. Do something like `unsigned char hdr[80], hash[32]; SHA256 ctx = SHA256(); /* fill in hdr */ ctx.init(); ctx.update(hdr,80); ctx.final(hash); ctx.init(); ctx.update(hash,32); ctx.final(hash); /* (reverse and) use results in hash, possibly converted to hex or whatever you need */`
dave_thompson_085
CC BY-SA 3.0
2017-08-15T05:59:11.057Z
@dave_thompson_085 The `std::string` wrapper is fine. There's no problem with using a `std::string` to hold arbitrary binary data.
David Schwartz
CC BY-SA 3.0
2017-08-16T09:36:42.050Z
@DavidSchwartz: _that_ wrapper (in the cited sourcefile) converts to hex (presumably for display or output) which is badly wrong if used for both steps of bitcoin's double-hash. You _could_ do the inner hash 'C-style' and the outer hash with `string` but that's just ugly. For at least some other applications it's okay.
dave_thompson_085
CC BY-SA 3.0
2017-01-15T19:32:27.250Z
2017-01-16T01:29:27.277Z
2017-01-16T01:29:27.277Z
UTF-8
<p><a href="https://bitcoin.stackexchange.com/a/111768/127251">This thread should give you a direct working answer</a>.</p>
<p>Had a lot of help from <a href="https://bitcoin.stackexchange.com/users/48884/andrew-chow">Andrew Chow</a>..</p>
<p>Hope this helps you or anyone else.</p>
<p>Cheers!!!</p>

CC BY-SA 4.0
2022-01-13T07:17:56.633Z
2022-01-13T07:17:56.633Z
AncientTides
<p>I was examining the block hashing algorithm and I tried to hash some blocks with my c++ sha256 to check results. </p>

<p>The hashes were wrong all the time so I thought I couldn't build the block header properly but then I took the data from wiki <a href="https://en.bitcoin.it/wiki/Block_hashing_algorithm" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Block_hashing_algorithm</a> and concatenated the values from the example and the result is still wrong (I tried it also in online sha-256 calculator so the function is ok). </p>

<p>Of course I must be missing something like coding but if I understand it right, sha is a function where you give a message and it takes the ascii codes of the message and does the math operations on these codes. But how can I pass a block header as a string? Or do I have to find an implementation of sha that interpretes the input differently? </p>

<p>I'm sorry, I know it's a noob question but this doesn't seem so obvious to me.</p>

<p>Edit: </p>

<p>The code is quite complex, but what I have at this moment is basically a made up block header that I packed in a string and for the example from the wiki page I mentioned above the string looks like</p>

<p><code>0100000081cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000e320b6c2
fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122bc7f5d74df2b9441a42a14695</code></p>

<p>I also have a sha function that takes a string as an input so the hash should be</p>

<p><code>1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e0000000000000000</code></p>

<p>but it's </p>

<p><code>cf5397ae292d5d37a0df81aa7f89f9fc7e1c43329e441ce2294f0a72e542c7d9</code></p>

<p>I also checked it on <a href="http://www.xorbin.com/tools/sha256-hash-calculator" rel="nofollow noreferrer">http://www.xorbin.com/tools/sha256-hash-calculator</a> so I think the code is ok but I must just be missing something here.</p>

CC BY-SA 3.0
2017-01-15T18:43:42.113Z
Maybe it will help if you post your code and sample input.
Nate Eldredge
CC BY-SA 3.0
2017-01-15T19:21:36.790Z
I tried a couple of wrong ways of doing it, and none of them match your output. Could you post the code?
Nick ODell
CC BY-SA 3.0
2017-01-15T19:34:45.273Z
Do you want me to paste the whole sha implementation?? Check the link with the hash calculator and it shows the same output (it's the double sha, I've been comparing it with the real block hash, which I pasted above)
M-guy
CC BY-SA 3.0
2017-01-15T20:52:46.620Z
Also your title (but not body) mentions mining. I hope you realize that anything programmable in C++ cannot mine bitcoin competively since about 5 years ago.
dave_thompson_085
CC BY-SA 3.0
2017-01-15T21:14:12.110Z
@dave_thompson_085 yup I do.
M-guy
<development><sha256>
How do you hash data for mining?
M-guy
# How do you hash data for mining?

(Asked by: username_0 on 2017-01-15)

I was examining the block hashing algorithm and I tried to hash some blocks with my c++ sha256 to check results.

The hashes were wrong all the time so I thought I couldn't build the block header properly but then I took the data from wiki <https://en.bitcoin.it/wiki/Block_hashing_algorithm> and concatenated the values from the example and the result is still wrong (I tried it also in online sha-256 calculator so the function is ok).

Of course I must be missing something like coding but if I understand it right, sha is a function where you give a message and it takes the ascii codes of the message and does the math operations on these codes. But how can I pass a block header as a string? Or do I have to find an implementation of sha that interpretes the input differently?

I'm sorry, I know it's a noob question but this doesn't seem so obvious to me.

Edit:

The code is quite complex, but what I have at this moment is basically a made up block header that I packed in a string and for the example from the wiki page I mentioned above the string looks like

`0100000081cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000e320b6c2
fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122bc7f5d74df2b9441a42a14695`

I also have a sha function that takes a string as an input so the hash should be

`1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e0000000000000000`

but it's

`cf5397ae292d5d37a0df81aa7f89f9fc7e1c43329e441ce2294f0a72e542c7d9`

I also checked it on <http://www.xorbin.com/tools/sha256-hash-calculator> so I think the code is ok but I must just be missing something here.

**Tags:** development, sha256

> **Comments:**
> **username_1**: Maybe it will help if you post your code and sample input.  
> **username_2**: I tried a couple of wrong ways of doing it, and none of them match your output. Could you post the code?  
> **username_0**: Do you want me to paste the whole sha implementation?? Check the link with the hash calculator and it shows the same output (it's the double sha, I've been comparing it with the real block hash, which I pasted above)  
> **username_3**: Also your title (but not body) mentions mining. I hope you realize that anything programmable in C++ cannot mine bitcoin competively since about 5 years ago.  
> **username_0**: @username_3 yup I do.  

---

## 2 answers

### Answer by username_4 on 2017-01-15. Score: 1

> sha is a function where you give a message and it takes the ascii
> codes of the message and does the math operations on these codes.

Wrong. sha256 is a hash function. This means it takes data of (fairly, there can be a granularity) arbitrary length and returns data of constant length. In the case of sha256, the granularity is 1 bit (though the granularity of most implementations of it is 1 Byte; Thank you, dave\_thompson\_085) and the data it returns is always 256 bit = 32 Byte. No ASCII encoding required.

You feed your hash function the wrong data. You feed it a string containing the ASCII-encoded hexadecimal representation of the data you're supposed to feed into the function.

If you have a hexadecimal representation encoded in ASCII, you first have to convert it to the data it represents and then feed that into the hash function.

> **Comments:**
> **username_0**: Ok, I get it... partly. So my mistake was that I had a piece of data for example `0xab` and I was passing string `"ab"` to the function which is entirely wrong. But still, the sha function takes a string as an input so when I have a block header encoded as `ab`should I find a character that has code 1010 1011 and pass it to the function?... No, I think there must be another way and I apparently still get it wrong.  
> **username_5**: Remember to hash it twice (hash it, then copy the SHA2-256 result back to the input box and hash again)  
> **username_4**: @username_0 You can't use a sha256sum function which only accept a string as its input. Use an implementation of sha256 which accepts arbitrary data.  
> **username_3**: Nitpick: SHA-256 itself (in fact all the SHA-2 family, and SHA-1, and the MD hashes before them, and SHA-3 also) is defined for bit granularity. However, practically all _software implementations_ limit to bytes, and all bitcoin hashing only needs bytes. @username_0: all good C++ (or C) crypto libs I've seen support arbitrary bytes; if you identify which one you're using and how it may be that you're just using it wrong.  
> **username_0**: @username_3 I didn't use a library, I was using this implementation: http://www.zedwood.com/article/cpp-sha256-function  
> **username_3**: That code is okay except the silly `std::string` wrapper is useless for bitcoin. Do something like `unsigned char hdr[80], hash[32]; SHA256 ctx = SHA256(); /* fill in hdr */ ctx.init(); ctx.update(hdr,80); ctx.final(hash); ctx.init(); ctx.update(hash,32); ctx.final(hash); /* (reverse and) use results in hash, possibly converted to hex or whatever you need */`  
> **username_6**: @username_3 The `std::string` wrapper is fine. There's no problem with using a `std::string` to hold arbitrary binary data.  
> **username_3**: @username_6: _that_ wrapper (in the cited sourcefile) converts to hex (presumably for display or output) which is badly wrong if used for both steps of bitcoin's double-hash. You _could_ do the inner hash 'C-style' and the outer hash with `string` but that's just ugly. For at least some other applications it's okay.  

---

### Answer by username_7 on 2022-01-13. Score: 0

[This thread should give you a direct working answer](https://bitcoin.stackexchange.com/a/111768/127251).

Had a lot of help from [Andrew Chow](https://bitcoin.stackexchange.com/users/48884/andrew-chow)..

Hope this helps you or anyone else.

Cheers!!!


---

