ethers.js/docs.wrm/api/utils/hashing.wrm

190 lines
5.2 KiB
Plaintext
Raw Normal View History

_section: Hashing Algorithms
Explain what hash functions are?
2020-05-08 10:24:40 +03:00
_subsection: Cryptographic Hash Functions
2020-02-02 15:58:29 +03:00
The [Cryptographic Hash Functions](link-wiki-cryptographichash)
are a specific family of hash functions.
2020-05-08 10:24:40 +03:00
_property: ethers.utils.id(text) => string<[[DataHexString]]<32>> @<utils-id> @SRC<hash>
The Ethereum Identity function computs the [KECCAK256](link-wiki-sha3) hash of the //text// bytes.
_property: ethers.utils.keccak256(aBytesLike) => string<[[DataHexString]]<32>> @<utils-keccak256> @SRC<keccak256>
2020-02-02 15:58:29 +03:00
Returns the [KECCAK256](link-wiki-sha3) digest //aBytesLike//.
2020-05-08 10:24:40 +03:00
_property: ethers.utils.ripemd160(aBytesLike) => string<[[DataHexString]]<20>> @<utils-ripemd160> @SRC<sha2>
2020-02-02 15:58:29 +03:00
Returns the [RIPEMD-160](link-wiki-ripemd) digest of //aBytesLike//.
2020-05-08 10:24:40 +03:00
_property: ethers.utils.sha256(aBytesLike) => string<[[DataHexString]]<32>> @<utils-sha256> @SRC<sha2:function.sha256>
2020-02-02 15:58:29 +03:00
Returns the [SHA2-256](link-wiki-sha2) digest of //aBytesLike//.
2020-05-08 10:24:40 +03:00
_property: ethers.utils.sha512(aBytesLike) => string<[[DataHexString]]<64>> @<utils-sha512> @SRC<sha2:function.sha512>
2020-02-02 15:58:29 +03:00
Returns the [SHA2-512](link-wiki-sha2) digest of //aBytesLike//.
2020-05-08 10:24:40 +03:00
_code: KECCAK256 @lang<javascript>
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");
// <hide>
bytes
// </hide>
//!
// Hash of SIX (6) characters (different than above)
utils.keccak256(bytes)
//!
// Hash of SIX (6) characters (same result)
utils.id("0x1234")
//!
_code: RIPEMD160 @lang<javascript>
utils.ripemd160("0x")
//!
utils.ripemd160("0x1234")
//!
_code: SHA-2 @lang<javascript>
utils.sha256("0x")
//!
2020-05-08 10:24:40 +03:00
utils.sha256("0x1234")
//!
2020-05-08 10:24:40 +03:00
utils.sha512("0x")
//!
utils.sha512("0x1234")
//!
_subsection: HMAC @<utils--hmac>
_property: ethers.utils.computeHmac(algorithm, key, data) => string<[[DataHexString]]> @<utils-computeHmac> @SRC<sha2>
Returns the [HMAC](link-wiki-hmac) of //data// with //key//
using the [Algorithm](utils--hmac-supported-algorithm) //algorithm//.
_heading: **HMAC Supported Algorithms** @<utils--hmac-supported-algorithm> @SRC<sha2:enum.SupportedAlgorithm>
2020-02-02 08:52:20 +03:00
_property: ethers.utils.SupportedAlgorithm.sha256 => string
2020-02-02 15:58:29 +03:00
Use the [SHA2-256](link-wiki-sha2) hash algorithm.
2020-02-02 08:52:20 +03:00
_property: ethers.utils.SupportedAlgorithm.sha512 => string
2020-02-02 15:58:29 +03:00
Use the [SHA2-512](link-wiki-sha2) hash algorithm.
2020-05-08 10:24:40 +03:00
_code: HMAC @lang<javascript>
2020-05-08 10:24:40 +03:00
const key = "0x0102";
const data = "0x1234";
utils.computeHmac("sha256", key, data)
//!
2020-05-08 10:24:40 +03:00
_subsection: Hashing Helpers
_property: ethers.utils.hashMessage(message) => string<[[DataHexString]]<32>> @<utils-hashMessage> @SRC<hash>
2020-02-25 22:57:11 +03:00
Computes the [[link-eip-191]] personal message digest of //message//. Personal messages are
2020-02-02 08:52:20 +03:00
converted to UTF-8 bytes and prefixed with ``\\x19Ethereum Signed Message:``
and the length of //message//.
2020-05-08 10:24:40 +03:00
_property: ethers.utils.namehash(name) => string<[[DataHexString]]<32>> @<utils-namehash> @SRC<hash>
2020-02-02 15:58:29 +03:00
Returns the [ENS Namehash](link-namehash) of //name//.
2020-05-08 10:24:40 +03:00
_code: Hashing Messages @lang<javascript>
// @TODO: include exampels of hashMessage; it can be complex. :)
_code: Namehash @lang<javascript>
utils.namehash("")
//!
utils.namehash("eth")
//!
utils.namehash("ricmoo.firefly.eth")
//!
utils.namehash("ricmoo.xyz")
//!
_subsection: Solidity Hashing Algorithms
When using the Solidity ``abi.packEncoded(...)`` function, a non-standard
//tightly packed// version of encoding is used. These functions implement
the tightly packing algorithm.
2020-05-08 10:24:40 +03:00
_property: ethers.utils.solidityPack(types, values) => string<[[DataHexString]]> @<utils-solidityPack> @SRC<solidity:pack>
Returns the non-standard encoded //values// packed according to
their respecive type in //types//.
_property: ethers.utils.solidityKeccak256(types, values) => string<[[DataHexString]]<32>> @<utils-solidityKeccak256> @SRC<solidity:keccak256>
Returns the [KECCAK256](link-wiki-sha3) of the non-standard encoded //values// packed
according to their respective type in //types//.
_property: ethers.utils.soliditySha256(types, values) => string<[[DataHexString]]<32>> @<utils-soliditySha256> @SRC<solidity:sha256>
Returns the [SHA2-256](link-wiki-sha2) of the non-standard encoded //values// packed
according to their respective type in //types//.
_code: Solidity Hashing @lang<javascript>
utils.solidityPack([ "int16", "uint48" ], [ -1, 12 ])
//!
2020-05-08 10:24:40 +03:00
utils.solidityPack([ "string", "uint8" ], [ "Hello", 3 ])
//!
2020-05-08 10:24:40 +03:00
utils.solidityKeccak256([ "int16", "uint48" ], [ -1, 12 ])
//!
2020-05-08 10:24:40 +03:00
utils.soliditySha256([ "int16", "uint48" ], [ -1, 12 ])
//!