<p><a href="https://bitcoin.stackexchange.com/a/111768/127251">My answer from this thread should help</a>.</p>
<p>Had a lot of help from <a href="https://bitcoin.stackexchange.com/users/48884/andrew-chow">Andrew Chow</a>..</p>
<p>I assume you already know the whole endian swapping mess in bitcoin...</p>
<p>Hope this helps you or anyone else.</p>
<p>Cheers!!!</p>

CC BY-SA 4.0
2022-01-13T07:19:56.777Z
2022-01-13T07:19:56.777Z
AncientTides
<p>Yes you should convert your first output before feeding it back in: <strong>A hash function is typically a function which takes in an array of bytes (of arbitrary size) and spits out an array of bytes (of fixed size)</strong>. When making the first call <code>first = sha256('myfirstSHA')</code>, it is likely that the string argument is implicitly converted to an array of bytes, whereby each character is substituted by a single byte encoding. However, the string output you are getting (which is a string, not an array of bytes), clearly represents a hex encoding of a 32 bytes array. <strong>You need to convert this hex encoding into a 32 bytes array</strong>. If you have a Linux terminal, as indicated by <a href="https://bitcoin.stackexchange.com/questions/50590/how-to-compute-double-sha256-shown-in-examples-of-bip-143/50591#50591">this post</a>, a very useful command is <code>xxd -r -p</code> which converts your hex encoding into actual bytes:</p>

<pre><code>$ echo -n myfirstSHA | sha256sum
9b2b95b24dd9149480ebda21aafe3f1a3c0370798ceec3c4d09c6a16adfe01c8 - 
$ echo -n myfirstSHA | sha256sum | xxd -r -p | sha256sum
96082208e341446bb8ba032486d142cbe73f1a66276b96c18ff815f31293fe0d -
</code></pre>

<p>If you are worried about the trailing '-' which appears in the output of <code>sha256sum</code>, you can cut it out:</p>

<pre><code>$ echo -n myfirstSHA | sha256sum | cut -d' ' -f1
9b2b95b24dd9149480ebda21aafe3f1a3c0370798ceec3c4d09c6a16adfe01c8
</code></pre>

<p>and then proceed as before:</p>

<pre><code>$ echo -n myfirstSHA | sha256sum | cut -d' ' -f1 | xxd -r -p | sha256sum
96082208e341446bb8ba032486d142cbe73f1a66276b96c18ff815f31293fe0d  -
</code></pre>

<p>In case you do not trust that <code>xxd -r -p</code> does the right thing, you can store the output in a file:</p>

<pre><code>$ echo -n myfirstSHA | sha256sum | xxd -r -p &gt; temp
</code></pre>

<p>then use <code>hexdump -C temp</code> or indeed <code>xxd temp</code> to check the content of the binary file:</p>

<pre><code>00000000  9b 2b 95 b2 4d d9 14 94  80 eb da 21 aa fe 3f 1a  |.+..M......!..?.|
00000010  3c 03 70 79 8c ee c3 c4  d0 9c 6a 16 ad fe 01 c8  |&lt;.py......j.....|
00000020
</code></pre>

CC BY-SA 3.0
2017-03-08T15:18:02.347Z
2017-03-15T11:30:15.290Z
2017-03-15T11:30:15.290Z
Sven Williamson
<p>So I have a function called sha256 which will take a string and return the SHA-256 hash.</p>

<p><code>var First = sha256('myfirstSHA');</code></p>

<p>And the output is the hex:</p>

<p><code>9b2b95b24dd9149480ebda21aafe3f1a3c0370798ceec3c4d09c6a16adfe01c8</code></p>

<p>All feels good here, but if I want a double SHA-256 how should I go abut calculating it? Can I just use the same function as above twice? like this...</p>

<p><code>var Second = sha256(sha256('myfirstSHA'));</code></p>

<p>What should the value of Second be? </p>

<p>I get the feeling I may need to get the value of the first hash and convert it to something before putting it back through the sha-256 function a second time??</p>

<p>So my question is, what should I convert it to, before feeding it back in again?</p>

<p>Any advice is most welcome!</p>

<encryption><sha256>
Is this how I should calculate a Double SHA256
Will-In-China
# Is this how I should calculate a Double SHA256

(Asked by: username_0 on 2017-03-08)

So I have a function called sha256 which will take a string and return the SHA-256 hash.

`var First = sha256('myfirstSHA');`

And the output is the hex:

`9b2b95b24dd9149480ebda21aafe3f1a3c0370798ceec3c4d09c6a16adfe01c8`

All feels good here, but if I want a double SHA-256 how should I go abut calculating it? Can I just use the same function as above twice? like this...

`var Second = sha256(sha256('myfirstSHA'));`

What should the value of Second be?

I get the feeling I may need to get the value of the first hash and convert it to something before putting it back through the sha-256 function a second time??

So my question is, what should I convert it to, before feeding it back in again?

Any advice is most welcome!

**Tags:** encryption, sha256


---

## 2 answers

### Answer by username_2 on 2017-03-08. Score: 5 | Accepted answer

Yes you should convert your first output before feeding it back in: **A hash function is typically a function which takes in an array of bytes (of arbitrary size) and spits out an array of bytes (of fixed size)**. When making the first call `first = sha256('myfirstSHA')`, it is likely that the string argument is implicitly converted to an array of bytes, whereby each character is substituted by a single byte encoding. However, the string output you are getting (which is a string, not an array of bytes), clearly represents a hex encoding of a 32 bytes array. **You need to convert this hex encoding into a 32 bytes array**. If you have a Linux terminal, as indicated by [this post](https://bitcoin.stackexchange.com/questions/50590/how-to-compute-double-sha256-shown-in-examples-of-bip-143/50591#50591), a very useful command is `xxd -r -p` which converts your hex encoding into actual bytes:

```
$ echo -n myfirstSHA | sha256sum
9b2b95b24dd9149480ebda21aafe3f1a3c0370798ceec3c4d09c6a16adfe01c8 - 
$ echo -n myfirstSHA | sha256sum | xxd -r -p | sha256sum
96082208e341446bb8ba032486d142cbe73f1a66276b96c18ff815f31293fe0d -

```

If you are worried about the trailing '-' which appears in the output of `sha256sum`, you can cut it out:

```
$ echo -n myfirstSHA | sha256sum | cut -d' ' -f1
9b2b95b24dd9149480ebda21aafe3f1a3c0370798ceec3c4d09c6a16adfe01c8

```

and then proceed as before:

```
$ echo -n myfirstSHA | sha256sum | cut -d' ' -f1 | xxd -r -p | sha256sum
96082208e341446bb8ba032486d142cbe73f1a66276b96c18ff815f31293fe0d  -

```

In case you do not trust that `xxd -r -p` does the right thing, you can store the output in a file:

```
$ echo -n myfirstSHA | sha256sum | xxd -r -p > temp

```

then use `hexdump -C temp` or indeed `xxd temp` to check the content of the binary file:

```
00000000  9b 2b 95 b2 4d d9 14 94  80 eb da 21 aa fe 3f 1a  |.+..M......!..?.|
00000010  3c 03 70 79 8c ee c3 c4  d0 9c 6a 16 ad fe 01 c8  |<.py......j.....|
00000020

```


---

### Answer by username_1 on 2022-01-13. Score: 1

[My answer from this thread should help](https://bitcoin.stackexchange.com/a/111768/127251).

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

I assume you already know the whole endian swapping mess in bitcoin...

Hope this helps you or anyone else.

Cheers!!!


---

