<p>The missing step would be converting the <code>ScriptData</code> into
CBOR prior to hashing.</p>
<p>In the case of <code>ScriptDataNumber</code> <code>42</code> the CBOR Hex is <code>&quot;182a&quot;</code> which is a bit simplified for Numbers vs more complex <code>ScriptData</code>. The CBOR <a href="https://datatracker.ietf.org/doc/html/rfc8949#appendix-B" rel="nofollow noreferrer">Jump Table</a> shows that <code>0x18</code> indicates <code>unsigned integer (one-byte uint8_t follows)</code> followed by <code>2a</code> which is <code>42</code> in hex.</p>
<p>Or using Haskell:</p>
<pre><code>let plutusData = PlutusCore.Data.I 42
print $ Base16.encode $ BSL.toStrict $ Codec.Serialise.serialise plutusData
-- &quot;182a&quot;
print $ Cardano.Api.hashScriptData $ Cardano.Api.Shelley.fromPlutusData plutusData
-- &quot;9e1199a988ba72ffd6e9c269cadb3b53b5f360ff99f112d9b2ee30c4d74ad88b&quot;
</code></pre>
<p>and using libsodiumjs as example to hash:</p>
<pre><code>var libsodiumWrappers = require(&quot;libsodium-wrappers&quot;);
var _sodium = libsodiumWrappers;

(async () =&gt; {
  await _sodium.ready;
  const sodium = _sodium;
  var h = sodium.crypto_generichash(32, sodium.from_hex('182a'));
  console.log(sodium.to_hex(h));
})();
</code></pre>
<p><a href="https://i.sstatic.net/F9xH3.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/F9xH3.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2021-11-02T22:12:53.937Z
Thanks, that's exactly what I was looking for! Regarding objects/json files, are we supposed to also convert to CBOR? For example a datum object like `{"constructor": 0,"fields": [{"int": 4}]}`, I transform into CBOR: `a24b636f6e7374727563746f7200466669656c647381a143696e7404` and when I hash it I get `5677bf1048dad6dce36483d2bec0fafc71f5dc9b01a2b1326c6443c2cd264b50`, but if I do the same with cardano-cli I get `8bb54ceaee2f57731094a2e96b8ad87bcc8988b1fa838e8c833eb3b72eae29a1`. Is there any additional step in this case?
glneto
CC BY-SA 4.0
2021-11-02T19:27:07.680Z
2021-11-02T19:27:07.680Z
Luigy
<p>I want to understand a bit better the hash-script-data functionality from cardano-cli. I checked the source code but had no luck in understanding how this hash is made.</p>
<p>From the examples, if we run:</p>
<pre><code>cardano-cli transaction hash-script-data --script-data-value 42
</code></pre>
<p>We get the following hash:</p>
<pre><code>9e1199a988ba72ffd6e9c269cadb3b53b5f360ff99f112d9b2ee30c4d74ad88b
</code></pre>
<p>However if we try to do a simple SHA256 hash of a 42 value we get:</p>
<pre><code>73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049
</code></pre>
<p>Also tried in the Plutus object format:</p>
<pre><code>{
  &quot;constructor&quot;: 0,
  &quot;fields&quot;: [{
    &quot;int&quot;: 42
  }]
}
</code></pre>
<p>but none of those match the hash generated by cardano-cli.</p>
<p>Also tried innumerous other alternatives but obviously I'm missing something, anyone knows what's the process for generating this hash?</p>
<p>Thank you!</p>

<plutus><cardano-cli><transaction><script><hash>
Hash script data
# Hash script data

(Asked by: username_0 on 2021-11-01)

I want to understand a bit better the hash-script-data functionality from cardano-cli. I checked the source code but had no luck in understanding how this hash is made.
From the examples, if we run:
cardano-cli transaction hash-script-data --script-data-value 42

We get the following hash:
9e1199a988ba72ffd6e9c269cadb3b53b5f360ff99f112d9b2ee30c4d74ad88b

However if we try to do a simple SHA256 hash of a 42 value we get:
73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049

Also tried in the Plutus object format:
{
  "constructor": 0,
  "fields": [{
    "int": 42
  }]
}

but none of those match the hash generated by cardano-cli.
Also tried innumerous other alternatives but obviously I'm missing something, anyone knows what's the process for generating this hash?
Thank you!

**Tags:** plutus, cardano-cli, transaction, script, hash

---

## 1 answers

### Answer by username_1 on 2021-11-02. Score: 3 | Accepted answer

The missing step would be converting the ScriptData into
CBOR prior to hashing.
In the case of ScriptDataNumber 42 the CBOR Hex is "182a" which is a bit simplified for Numbers vs more complex ScriptData. The CBOR Jump Table shows that 0x18 indicates unsigned integer (one-byte uint8_t follows) followed by 2a which is 42 in hex.
Or using Haskell:
let plutusData = PlutusCore.Data.I 42
print $ Base16.encode $ BSL.toStrict $ Codec.Serialise.serialise plutusData
-- "182a"
print $ Cardano.Api.hashScriptData $ Cardano.Api.Shelley.fromPlutusData plutusData
-- "9e1199a988ba72ffd6e9c269cadb3b53b5f360ff99f112d9b2ee30c4d74ad88b"

and using libsodiumjs as example to hash:
var libsodiumWrappers = require("libsodium-wrappers");
var _sodium = libsodiumWrappers;

(async () => {
  await _sodium.ready;
  const sodium = _sodium;
  var h = sodium.crypto_generichash(32, sodium.from_hex('182a'));
  console.log(sodium.to_hex(h));
})();

> **Comments:**
> **username_0**: Thanks, that's exactly what I was looking for! Regarding objects/json files, are we supposed to also convert to CBOR? For example a datum object like `{"constructor": 0,"fields": [{"int": 4}]}`, I transform into CBOR: `a24b636f6e7374727563746f7200466669656c647381a143696e7404` and when I hash it I get `5677bf1048dad6dce36483d2bec0fafc71f5dc9b01a2b1326c6443c2cd264b50`, but if I do the same with cardano-cli I get `8bb54ceaee2f57731094a2e96b8ad87bcc8988b1fa838e8c833eb3b72eae29a1`. Is there any additional step in this case?  

---

