All files / ethers.js/src.ts/crypto keccak.ts

100% Statements 54/54
100% Branches 6/6
100% Functions 4/4
100% Lines 54/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 551x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 637077x 637077x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 637078x 637078x 637078x 1x 1x 1x 3x 2x 2x 1x  
/**
 *  Cryptographic hashing functions
 *
 *  @_subsection: api/crypto:Hash Functions [about-crypto-hashing]
 */
 
import { keccak_256 } from "@noble/hashes/sha3";
 
import { getBytes, hexlify } from "../utils/index.js";
 
import type { BytesLike } from "../utils/index.js";
 
 
let locked = false;
 
const _keccak256 = function(data: Uint8Array): Uint8Array {
    return keccak_256(data);
}
 
let __keccak256: (data: Uint8Array) => BytesLike = _keccak256;
 
/**
 *  Compute the cryptographic KECCAK256 hash of %%data%%.
 *
 *  The %%data%% **must** be a data representation, to compute the
 *  hash of UTF-8 data use the [[id]] function.
 *
 *  @returns DataHexstring
 *  @example:
 *    keccak256("0x")
 *    //_result:
 *
 *    keccak256("0x1337")
 *    //_result:
 *
 *    keccak256(new Uint8Array([ 0x13, 0x37 ]))
 *    //_result:
 *
 *    // Strings are assumed to be DataHexString, otherwise it will
 *    // throw. To hash UTF-8 data, see the note above.
 *    keccak256("Hello World")
 *    //_error:
 */
export function keccak256(_data: BytesLike): string {
    const data = getBytes(_data, "data");
    return hexlify(__keccak256(data));
}
keccak256._ = _keccak256;
keccak256.lock = function(): void { locked = true; }
keccak256.register = function(func: (data: Uint8Array) => BytesLike) {
    if (locked) { throw new TypeError("keccak256 is locked"); }
    __keccak256 = func;
}
Object.freeze(keccak256);