ethers.js/docs/v5/api/utils/hashing/index.html
2020-06-09 23:56:58 -04:00

155 lines
22 KiB
HTML

<!DOCTYPE html>
<html class="paged">
<head>
<title>Hashing Algorithms</title>
<link rel="stylesheet" type="text/css" href="/v5/static/style.css">
</head>
<body>
<div class="sidebar">
<div class="header">
<div class="logo"><a href="/v5/"><div class="image"></div><div class="name">ethers</div><div class="version">v5.0-beta</div></a></div>
</div>
<div class="toc"><div>
<div class="link title"><a href="/">Documentation</a></div><div class="base show link depth-1"><a href="/v5/getting-started/">Getting Started</a></div><div class="base show link depth-1"><a href="/v5/concepts/">Ethereum Basics</a></div><div class="hide link depth-2"><a href="/v5/concepts/events/">Events</a></div><div class="hide link depth-2"><a href="/v5/concepts/gas/">Gas</a></div><div class="hide link depth-2"><a href="/v5/concepts/security/">Security</a></div><div class="base ancestor show link depth-1"><a href="/v5/api/">Application Programming Interface</a></div><div class="show link depth-2"><a href="/v5/api/contract/">Contract Interaction</a></div><div class="hide link depth-3"><a href="/v5/api/contract/contract/">Contract</a></div><div class="hide link depth-3"><a href="/v5/api/contract/contract-factory/">ContractFactory</a></div><div class="hide link depth-3"><a href="/v5/api/contract/example/">Example: ERC-20 Contract</a></div><div class="show link depth-2"><a href="/v5/api/signer/">Signers</a></div><div class="show link depth-2"><a href="/v5/api/providers/">Providers</a></div><div class="hide link depth-3"><a href="/v5/api/providers/provider/">Provider</a></div><div class="hide link depth-3"><a href="/v5/api/providers/jsonrpc-provider/">JsonRpcProvider</a></div><div class="hide link depth-3"><a href="/v5/api/providers/api-providers/">API Providers</a></div><div class="hide link depth-3"><a href="/v5/api/providers/other/">Other Providers</a></div><div class="hide link depth-3"><a href="/v5/api/providers/types/">Types</a></div><div class="ancestor show link depth-2"><a href="/v5/api/utils/">Utilities</a></div><div class="show link depth-3"><a href="/v5/api/utils/abi/">Application Binary Interface</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/interface/">Interface</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/fragments/">Fragments</a></div><div class="show link depth-3"><a href="/v5/api/utils/address/">Addresses</a></div><div class="show link depth-3"><a href="/v5/api/utils/bignumber/">BigNumber</a></div><div class="show link depth-3"><a href="/v5/api/utils/bytes/">Byte Manipulation</a></div><div class="show link depth-3"><a href="/v5/api/utils/constants/">Constants</a></div><div class="show link depth-3"><a href="/v5/api/utils/display-logic/">Display Logic and Input</a></div><div class="show link depth-3"><a href="/v5/api/utils/encoding/">Encoding Utilities</a></div><div class="show link depth-3"><a href="/v5/api/utils/fixednumber/">FixedNumber</a></div><div class="myself ancestor ancestor show link depth-3"><a href="/v5/api/utils/hashing/">Hashing Algorithms</a></div><div class="show link depth-3"><a href="/v5/api/utils/hdnode/">HD Wallet</a></div><div class="show link depth-3"><a href="/v5/api/utils/logger/">Logging</a></div><div class="show link depth-3"><a href="/v5/api/utils/properties/">Property Utilities</a></div><div class="show link depth-3"><a href="/v5/api/utils/signing-key/">Signing Key</a></div><div class="show link depth-3"><a href="/v5/api/utils/strings/">Strings</a></div><div class="show link depth-3"><a href="/v5/api/utils/transactions/">Transactions</a></div><div class="show link depth-3"><a href="/v5/api/utils/web/">Web Utilities</a></div><div class="show link depth-3"><a href="/v5/api/utils/wordlists/">Wordlists</a></div><div class="show link depth-2"><a href="/v5/api/other/">Other Libraries</a></div><div class="hide link depth-3"><a href="/v5/api/other/assembly/">Assembly</a></div><div class="hide link depth-4"><a href="/v5/api/other/assembly/dialect/">Ethers ASM Dialect</a></div><div class="hide link depth-4"><a href="/v5/api/other/assembly/api/">Utilities</a></div><div class="hide link depth-4"><a href="/v5/api/other/assembly/ast/">Abstract Syntax Tree</a></div><div class="hide link depth-3"><a href="/v5/api/other/hardware/">Hardware Wallets</a></div><div class="show link depth-2"><a href="/v5/api/experimental/">Experimental</a></div><div class="base show link depth-1"><a href="/v5/cli/">Command Line Interfaces</a></div><div class="hide link depth-2"><a href="/v5/cli/ethers/">Sandbox Utility</a></div><div class="hide link depth-2"><a href="/v5/cli/asm/">Assembler</a></div><div class="hide link depth-2"><a href="/v5/cli/ens/">Ethereum Naming Service</a></div><div class="hide link depth-2"><a href="/v5/cli/typescript/">TypeScript</a></div><div class="hide link depth-2"><a href="/v5/cli/plugin/">Making Your Own</a></div><div class="base show link depth-1"><a href="/v5/cookbook/">Cookbook</a></div><div class="base show link depth-1"><a href="/v5/migration/">Migration Guide</a></div><div class="hide link depth-2"><a href="/v5/migration/web3/">Migration: From Web3.js</a></div><div class="hide link depth-2"><a href="/v5/migration/ethers-v4/">Migration: From Ethers v4</a></div><div class="base show link depth-1"><a href="/v5/testing/">Testing</a></div><div class="base show link depth-1"><a href="/v5/contributing/">Contributing and Hacking</a></div><div class="base show link depth-1"><a href="/v5/documentation/">Flatworm Docs</a></div><div class="base show link depth-1"><a href="/v5/license/">License and Copyright</a></div>
</div></div>
</div>
<div class="content">
<div class="breadcrumbs"><a href="/v5/">Documentation</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<a href="/v5/api/">API</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<a href="/v5/api/utils/">Utilities</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<span class="current">Hashing Algorithms</span></div>
<a name="hashing-algorithms"></a><a name="hashing-algorithms"></a><h1 class="show-anchors"><div>Hashing Algorithms<div class="anchors"><a class="self" href="/v5/api/utils/hashing/#hashing-algorithms"></a></div></div></h1><p>Explain what hash functions are?</p>
<a name="cryptographic-hash-functions"></a><a name="hashing-algorithms--cryptographic-hash-functions"></a><h2 class="show-anchors"><div>Cryptographic Hash Functions<div class="anchors"><a class="self" href="/v5/api/utils/hashing/#cryptographic-hash-functions"></a></div></div></h2><p>The <a href="https://en.wikipedia.org/wiki/Cryptographic_hash_function">Cryptographic Hash Functions</a> are a specific family of hash functions.</p>
<a name="utils-id"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">id</span><span class="symbol">(</span> <span class="param">text</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 32 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-id"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/hash/src.ts/index.ts#L47">source</a></div></div><div class="body"><p>The Ethereum Identity function computs the <a href="https://en.wikipedia.org/wiki/SHA-3">KECCAK256</a> hash of the <i>text</i> bytes.</p>
</div></div><a name="utils-keccak256"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">keccak256</span><span class="symbol">(</span> <span class="param">aBytesLike</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 32 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-keccak256"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/keccak256/src.ts/index.ts#L7">source</a></div></div><div class="body"><p>Returns the <a href="https://en.wikipedia.org/wiki/SHA-3">KECCAK256</a> digest <i>aBytesLike</i>.</p>
</div></div><a name="utils-ripemd160"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">ripemd160</span><span class="symbol">(</span> <span class="param">aBytesLike</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 20 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-ripemd160"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/sha2/src.ts/index.ts#L14">source</a></div></div><div class="body"><p>Returns the <a href="https://en.m.wikipedia.org/wiki/RIPEMD">RIPEMD-160</a> digest of <i>aBytesLike</i>.</p>
</div></div><a name="utils-sha256"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">sha256</span><span class="symbol">(</span> <span class="param">aBytesLike</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 32 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-sha256"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/sha2/src.ts/index.ts#L18">source</a></div></div><div class="body"><p>Returns the <a href="https://en.wikipedia.org/wiki/SHA-2">SHA2-256</a> digest of <i>aBytesLike</i>.</p>
</div></div><a name="utils-sha512"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">sha512</span><span class="symbol">(</span> <span class="param">aBytesLike</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 64 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-sha512"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/sha2/src.ts/index.ts#L22">source</a></div></div><div class="body"><p>Returns the <a href="https://en.wikipedia.org/wiki/SHA-2">SHA2-512</a> digest of <i>aBytesLike</i>.</p>
</div></div><div class="code-title"><div>KECCAK256</div></div><div class="code">utils.keccak256([ 0x12, 0x34 ])
//!
utils.keccak256("0x")
//!
utils.keccak256("0x1234")
//!
// The value MUST be data, such as:
// - an Array of numbers
// - a data hex string (e.g. "0x1234")
// - a Uint8Array
// Do NOT use UTF-8 strings that are not a DataHexstring
utils.keccak256("hello world")
//! error
// If needed, convert strings to bytes first:
utils.keccak256(utils.toUtf8Bytes("hello world"))
//!
// Or equivalently use the identity function:
utils.id("hello world")
//!
// Keep in mind that the string "0x1234" represents TWO
// bytes (i.e. [ 0x12, 0x34 ]. If you wish to compute the
// hash of the 6 characters "0x1234", convert it to UTF-8
// bytes first using utils.toUtf8Bytes.
// Consider the following examples:
// Hash of TWO (2) bytes:
utils.keccak256("0x1234")
//!
// Hash of TWO (2) bytes: (same result)
utils.keccak256([ 0x12, 0x34 ])
//!
const bytes = utils.toUtf8Bytes("0x1234");
// &lt;hide&gt;
bytes
// &lt;/hide&gt;
//!
// Hash of SIX (6) characters (different than above)
utils.keccak256(bytes)
//!
// Hash of SIX (6) characters (same result)
utils.id("0x1234")
//!</div><div class="code-title"><div>RIPEMD160</div></div><div class="code">utils.ripemd160("0x")
//!
utils.ripemd160("0x1234")
//!</div><div class="code-title"><div>SHA-2</div></div><div class="code">utils.sha256("0x")
//!
utils.sha256("0x1234")
//!
utils.sha512("0x")
//!
utils.sha512("0x1234")
//!</div><a name="utils--hmac"></a><a name="hashing-algorithms--utils--hmac"></a><h2 class="show-anchors"><div>HMAC<div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils--hmac"></a></div></div></h2>
<a name="utils-computeHmac"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">computeHmac</span><span class="symbol">(</span> <span class="param">algorithm</span> <span class="symbol">,</span> <span class="param">key</span> <span class="symbol">,</span> <span class="param">data</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a> &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-computeHmac"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/sha2/src.ts/index.ts#L27">source</a></div></div><div class="body"><p>Returns the <a href="https://en.wikipedia.org/wiki/HMAC">HMAC</a> of <i>data</i> with <i>key</i> using the <a href="/v5/api/utils/hashing/#utils--hmac-supported-algorithm">Algorithm</a> <i>algorithm</i>.</p>
</div></div><a name="utils--hmac-supported-algorithm"></a><a name="hashing-algorithms--utils--hmac--utils--hmac-supported-algorithm"></a><h3 class="show-anchors"><div><b>HMAC Supported Algorithms</b><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils--hmac-supported-algorithm"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/sha2/src.ts/index.ts#L11">source</a></div></div></h3>
<div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="path">SupportedAlgorithm</span><span class="symbol">.</span><span class="method">sha256</span> <span class="arrow">&rArr;</span> <span class="returns">string</span><div class="anchors"></div></div><div class="body"><p>Use the <a href="https://en.wikipedia.org/wiki/SHA-2">SHA2-256</a> hash algorithm.</p>
</div></div><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="path">SupportedAlgorithm</span><span class="symbol">.</span><span class="method">sha512</span> <span class="arrow">&rArr;</span> <span class="returns">string</span><div class="anchors"></div></div><div class="body"><p>Use the <a href="https://en.wikipedia.org/wiki/SHA-2">SHA2-512</a> hash algorithm.</p>
</div></div><div class="code-title"><div>HMAC</div></div><div class="code">const key = "0x0102";
const data = "0x1234";
utils.computeHmac("sha256", key, data)
//!</div><a name="hashing-algorithms--hashing-helpers"></a><h2 class="show-anchors"><div>Hashing Helpers<div class="anchors"><a class="self" href="/v5/api/utils/hashing/#hashing-algorithms--hashing-helpers"></a></div></div></h2>
<a name="utils-hashMessage"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">hashMessage</span><span class="symbol">(</span> <span class="param">message</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 32 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-hashMessage"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/hash/src.ts/index.ts#L53">source</a></div></div><div class="body"><p>Computes the <a href="https://eips.ethereum.org/EIPS/eip-191">EIP-191</a> personal message digest of <i>message</i>. Personal messages are converted to UTF-8 bytes and prefixed with <code class="inline">\x19Ethereum Signed Message:</code> and the length of <i>message</i>.</p>
</div></div><a name="utils-namehash"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">namehash</span><span class="symbol">(</span> <span class="param">name</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 32 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-namehash"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/hash/src.ts/index.ts#L29">source</a></div></div><div class="body"><p>Returns the <a href="https://docs.ens.domains/contract-api-reference/name-processing#hashing-names">ENS Namehash</a> of <i>name</i>.</p>
</div></div><div class="code-title"><div>Hashing Messages</div></div><div class="code">// @TODO: include exampels of hashMessage; it can be complex. :)</div><div class="code-title"><div>Namehash</div></div><div class="code">utils.namehash("")
//!
utils.namehash("eth")
//!
utils.namehash("ricmoo.firefly.eth")
//!
utils.namehash("ricmoo.xyz")
//!</div><a name="hashing-algorithms--solidity-hashing-algorithms"></a><h2 class="show-anchors"><div>Solidity Hashing Algorithms<div class="anchors"><a class="self" href="/v5/api/utils/hashing/#hashing-algorithms--solidity-hashing-algorithms"></a></div></div></h2><p>When using the Solidity <code class="inline">abi.packEncoded(...)</code> function, a non-standard <i>tightly packed</i> version of encoding is used. These functions implement the tightly packing algorithm.</p>
<a name="utils-solidityPack"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">solidityPack</span><span class="symbol">(</span> <span class="param">types</span> <span class="symbol">,</span> <span class="param">values</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a> &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-solidityPack"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/solidity/src.ts/index.ts#L73">source</a></div></div><div class="body"><p>Returns the non-standard encoded <i>values</i> packed according to their respecive type in <i>types</i>.</p>
</div></div><a name="utils-solidityKeccak256"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">solidityKeccak256</span><span class="symbol">(</span> <span class="param">types</span> <span class="symbol">,</span> <span class="param">values</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 32 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-solidityKeccak256"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/solidity/src.ts/index.ts#L82">source</a></div></div><div class="body"><p>Returns the <a href="https://en.wikipedia.org/wiki/SHA-3">KECCAK256</a> of the non-standard encoded <i>values</i> packed according to their respective type in <i>types</i>.</p>
</div></div><a name="utils-soliditySha256"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">soliditySha256</span><span class="symbol">(</span> <span class="param">types</span> <span class="symbol">,</span> <span class="param">values</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 32 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-soliditySha256"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/solidity/src.ts/index.ts#L86">source</a></div></div><div class="body"><p>Returns the <a href="https://en.wikipedia.org/wiki/SHA-2">SHA2-256</a> of the non-standard encoded <i>values</i> packed according to their respective type in <i>types</i>.</p>
</div></div><div class="code-title"><div>Solidity Hashing</div></div><div class="code">utils.solidityPack([ "int16", "uint48" ], [ -1, 12 ])
//!
utils.solidityPack([ "string", "uint8" ], [ "Hello", 3 ])
//!
utils.solidityKeccak256([ "int16", "uint48" ], [ -1, 12 ])
//!
utils.soliditySha256([ "int16", "uint48" ], [ -1, 12 ])
//!</div>
<div class="footer">
<div class="nav previous"><a href="/v5/api/utils/fixednumber/"><span class="arrow">&larr;</span>FixedNumber</a></div>
<div class="nav next"><a href="/v5/api/utils/hdnode/">HD Wallet<span class="arrow">&rarr;</span></a></div>
</div>
<div class="copyright">The content of this site is licensed under the <a href="https://choosealicense.com/licenses/cc-by-4.0/">Creative Commons License</a>. Generated on June 8, 2020, 10:6pm.</div>
</div>
<script src="/v5/static/script.js" type="text/javascript"></script>
</body>
</html>