ethers.js/lib.commonjs/crypto/keccak.js

52 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2022-09-05 16:57:11 -04:00
"use strict";
2022-12-02 21:27:06 -05:00
/**
* Cryptographic hashing functions
*
* @_subsection: api/crypto:Hash Functions [about-crypto-hashing]
*/
2022-09-05 16:57:11 -04:00
Object.defineProperty(exports, "__esModule", { value: true });
exports.keccak256 = void 0;
const sha3_1 = require("@noble/hashes/sha3");
const index_js_1 = require("../utils/index.js");
let locked = false;
const _keccak256 = function (data) {
return (0, sha3_1.keccak_256)(data);
};
let __keccak256 = _keccak256;
2022-11-30 15:44:23 -05:00
/**
* Compute the cryptographic KECCAK256 hash of %%data%%.
*
2022-12-09 18:24:58 -05:00
* The %%data%% **must** be a data representation, to compute the
* hash of UTF-8 data use the [[id]] function.
*
2022-11-30 15:44:23 -05:00
* @returns DataHexstring
2022-12-09 18:24:58 -05:00
* @example:
* keccak256("0x")
* //_result:
*
* keccak256("0x1337")
* //_result:
*
* keccak256(new Uint8Array([ 0x13, 0x37 ]))
* //_result:
*
2022-12-30 11:30:03 -05:00
* // Strings are assumed to be DataHexString, otherwise it will
* // throw. To hash UTF-8 data, see the note above.
* keccak256("Hello World")
* //_error:
2022-11-30 15:44:23 -05:00
*/
2022-09-05 16:57:11 -04:00
function keccak256(_data) {
2022-09-15 22:58:45 -04:00
const data = (0, index_js_1.getBytes)(_data, "data");
2022-09-05 16:57:11 -04:00
return (0, index_js_1.hexlify)(__keccak256(data));
}
exports.keccak256 = keccak256;
keccak256._ = _keccak256;
keccak256.lock = function () { locked = true; };
keccak256.register = function (func) {
if (locked) {
throw new TypeError("keccak256 is locked");
}
__keccak256 = func;
};
Object.freeze(keccak256);
//# sourceMappingURL=keccak.js.map