user4405: 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!

user4230: 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));
})();



4405: <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>
 4230: <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.stack.imgur.com/F9xH3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/F9xH3.png" alt="enter image description here" /></a></p>
