hmac.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.computeHmac = void 0;
  4. /**
  5. * An **HMAC** enables verification that a given key was used
  6. * to authenticate a payload.
  7. *
  8. * See: [[link-wiki-hmac]]
  9. *
  10. * @_subsection: api/crypto:HMAC [about-hmac]
  11. */
  12. const crypto_js_1 = require("./crypto.js");
  13. const index_js_1 = require("../utils/index.js");
  14. let locked = false;
  15. const _computeHmac = function (algorithm, key, data) {
  16. return (0, crypto_js_1.createHmac)(algorithm, key).update(data).digest();
  17. };
  18. let __computeHmac = _computeHmac;
  19. /**
  20. * Return the HMAC for %%data%% using the %%key%% key with the underlying
  21. * %%algo%% used for compression.
  22. *
  23. * @example:
  24. * key = id("some-secret")
  25. *
  26. * // Compute the HMAC
  27. * computeHmac("sha256", key, "0x1337")
  28. * //_result:
  29. *
  30. * // To compute the HMAC of UTF-8 data, the data must be
  31. * // converted to UTF-8 bytes
  32. * computeHmac("sha256", key, toUtf8Bytes("Hello World"))
  33. * //_result:
  34. *
  35. */
  36. function computeHmac(algorithm, _key, _data) {
  37. const key = (0, index_js_1.getBytes)(_key, "key");
  38. const data = (0, index_js_1.getBytes)(_data, "data");
  39. return (0, index_js_1.hexlify)(__computeHmac(algorithm, key, data));
  40. }
  41. exports.computeHmac = computeHmac;
  42. computeHmac._ = _computeHmac;
  43. computeHmac.lock = function () { locked = true; };
  44. computeHmac.register = function (func) {
  45. if (locked) {
  46. throw new Error("computeHmac is locked");
  47. }
  48. __computeHmac = func;
  49. };
  50. Object.freeze(computeHmac);
  51. //# sourceMappingURL=hmac.js.map