ethers.js/lib.commonjs/crypto/hmac.js

51 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2022-09-05 16:57:11 -04:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.computeHmac = void 0;
2022-12-02 21:27:06 -05:00
/**
* An **HMAC** enables verification that a given key was used
* to authenticate a payload.
*
* See: [[link-wiki-hmac]]
*
* @_subsection: api/crypto:HMAC [about-hmac]
*/
2022-11-09 02:57:02 -05:00
const crypto_js_1 = require("./crypto.js");
2022-09-05 16:57:11 -04:00
const index_js_1 = require("../utils/index.js");
let locked = false;
const _computeHmac = function (algorithm, key, data) {
2022-11-09 02:57:02 -05:00
return (0, crypto_js_1.createHmac)(algorithm, key).update(data).digest();
2022-09-05 16:57:11 -04:00
};
let __computeHmac = _computeHmac;
2022-12-02 21:27:06 -05:00
/**
* Return the HMAC for %%data%% using the %%key%% key with the underlying
* %%algo%% used for compression.
2022-12-09 18:24:58 -05:00
*
* @example:
* key = id("some-secret")
*
* // Compute the HMAC
* computeHmac("sha256", key, "0x1337")
* //_result:
*
* // To compute the HMAC of UTF-8 data, the data must be
* // converted to UTF-8 bytes
* computeHmac("sha256", key, toUtf8Bytes("Hello World"))
* //_result:
*
2022-12-02 21:27:06 -05:00
*/
2022-09-05 16:57:11 -04:00
function computeHmac(algorithm, _key, _data) {
2022-09-15 22:58:45 -04:00
const key = (0, index_js_1.getBytes)(_key, "key");
const data = (0, index_js_1.getBytes)(_data, "data");
2022-09-05 16:57:11 -04:00
return (0, index_js_1.hexlify)(__computeHmac(algorithm, key, data));
}
exports.computeHmac = computeHmac;
computeHmac._ = _computeHmac;
computeHmac.lock = function () { locked = true; };
computeHmac.register = function (func) {
if (locked) {
throw new Error("computeHmac is locked");
}
__computeHmac = func;
};
Object.freeze(computeHmac);
//# sourceMappingURL=hmac.js.map