There are many hashing algorithms used throughout the hashgraph and blockchain space as well as some more complex usages which require utilities to facilitate these common operations.
The Hashing Utilities are directly imported from The Ethers Project. The complete documentation can be found in the official ethers docs.
hethers.utils.keccak256([ 0x12,0x34 ])// '0x56570de287d73cd1cb6092bb8fdee6173974955fdef345ae579ee9f475ea7432'hethers.utils.keccak256("0x")// '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'hethers.utils.keccak256("0x1234")// '0x56570de287d73cd1cb6092bb8fdee6173974955fdef345ae579ee9f475ea7432'// 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 DataHexstringhethers.utils.keccak256("hello world")// [Error: invalid arrayify value] {// argument: 'value',// code: 'INVALID_ARGUMENT',// reason: 'invalid arrayify value',// value: 'hello world'// }// If needed, convert strings to bytes first:hethers.utils.keccak256(utils.toUtf8Bytes("hello world"))// '0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad'// Or equivalently use the identity function:hethers.utils.id("hello world")// '0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad'// 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:hethers.utils.keccak256("0x1234")// '0x56570de287d73cd1cb6092bb8fdee6173974955fdef345ae579ee9f475ea7432'// Hash of TWO (2) bytes: (same result)hethers.utils.keccak256([ 0x12,0x34 ])// '0x56570de287d73cd1cb6092bb8fdee6173974955fdef345ae579ee9f475ea7432'bytes =hethers.utils.toUtf8Bytes("0x1234")// Uint8Array [ 48, 120, 49, 50, 51, 52 ]// Hash of SIX (6) characters (different than above)hethers.utils.keccak256(bytes)// '0x1ac7d1b81b7ba1025b36ccb86723da6ee5a87259f1c2fd5abe69d3200b512ec8'// Hash of SIX (6) characters (same result)hethers.utils.id("0x1234")// '0x1ac7d1b81b7ba1025b36ccb86723da6ee5a87259f1c2fd5abe69d3200b512ec8'
Computes the EIP-191 personal message digest of message. Personal messages are converted to UTF-8 bytes and prefixed with \x19Ethereum Signed Message: and the length of message.
// Hashing a string messagehethers.utils.hashMessage("Hello World")// '0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2'// Hashing binary data (also "Hello World", but as bytes)hethers.utils.hashMessage( [ 72,101,108,108,111,32,87,111,114,108,100 ])// '0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2'// NOTE: It is important to understand how strings and binary// data is handled differently. A string is ALWAYS processed// as the bytes of the string, so a hexstring MUST be// converted to an ArrayLike object first.// Hashing a hex string is the same as hashing a STRING// Note: this is the hash of the 4 characters [ '0', 'x', '4', '2' ]hethers.utils.hashMessage("0x42")// '0xf0d544d6e4a96e1c08adc3efabe2fcb9ec5e28db1ad6c33ace880ba354ab0fce'// Hashing the binary data// Note: this is the hash of the 1 byte [ 0x42 ]hethers.utils.hashMessage([ 0x42 ])// '0xd18c12b87124f9ceb7e1d3a5d06a5ac92ecab15931417e8d1558d9a263f99d63'// Hashing the binary data// Note: similarly, this is the hash of the 1 byte [ 0x42 ]hethers.utils.hashMessage(utils.arrayify("0x42"))// '0xd18c12b87124f9ceb7e1d3a5d06a5ac92ecab15931417e8d1558d9a263f99d63'
namehash and isValidNameare removed from hethers.utils
Typed Data Encoder
The TypedDataEncoder is used to compute the various encoded data required for EIP-712 signed data.
Signed data requires a domain, list of structures and their members and the data itself.
The domain is an object with values for any of the standard domain properties.
The types is an object with each property being the name of a structure, mapping to an array of field descriptions. It should not include the EIP712Domain property unless it is required as a child structure of another
Returns a copy of value, where any leaf value with a type of address will have been recursively replaced with the value of calling resolveName with that value.
When using the Solidity abi.packEncoded(...) function, a non-standard tightly packed version of encoding is used. These functions implement the tightly packing algorithm.
Returns the SHA2-256 of the non-standard encoded values packed according to their respective type in types.
hethers.utils.solidityPack([ "int16","uint48" ], [ -1,12 ])// '0xffff00000000000c'hethers.utils.solidityPack([ "string","uint8" ], [ "Hello",3 ])// '0x48656c6c6f03'hethers.utils.solidityKeccak256([ "int16","uint48" ], [ -1,12 ])// '0x81da7abb5c9c7515f57dab2fc946f01217ab52f3bd8958bc36bd55894451a93c'hethers.utils.soliditySha256([ "int16","uint48" ], [ -1,12 ])// '0xa5580fb602f6e2ba9c588011dc4e6c2335e0f5d970dc45869db8f217efc6911a'// As a short example of the non-distinguished nature of// Solidity tight-packing (which is why it is inappropriate// for many things from a security point of view), consider// the following examples are all equal, despite representing// very different values and layouts.hethers.utils.solidityPack([ "string","string" ], [ "hello","world01" ])// '0x68656c6c6f776f726c643031'hethers.utils.solidityPack([ "string","string" ], [ "helloworld","01" ])// '0x68656c6c6f776f726c643031'hethers.utils.solidityPack([ "string","string","uint16" ], [ "hell","oworld",0x3031 ])// '0x68656c6c6f776f726c643031'hethers.utils.solidityPack([ "uint96" ], [ "32309054545061485574011236401" ])// '0x68656c6c6f776f726c643031'