1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- /**
- * An **HMAC** enables verification that a given key was used
- * to authenticate a payload.
- *
- * See: [[link-wiki-hmac]]
- *
- * @_subsection: api/crypto:HMAC [about-hmac]
- */
- import { createHmac } from "./crypto.js";
- import { getBytes, hexlify } from "../utils/index.js";
- let locked = false;
- const _computeHmac = function (algorithm, key, data) {
- return createHmac(algorithm, key).update(data).digest();
- };
- let __computeHmac = _computeHmac;
- /**
- * Return the HMAC for %%data%% using the %%key%% key with the underlying
- * %%algo%% used for compression.
- *
- * @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:
- *
- */
- export function computeHmac(algorithm, _key, _data) {
- const key = getBytes(_key, "key");
- const data = getBytes(_data, "data");
- return hexlify(__computeHmac(algorithm, key, data));
- }
- 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
|