Hash.mjs 611 B

12345678910111213141516171819202122232425262728
  1. /**
  2. * Hashing resources.
  3. *
  4. * @author n1474335 [n1474335@gmail.com]
  5. *
  6. * @copyright Crown Copyright 2016
  7. * @license Apache-2.0
  8. */
  9. import Utils from "../Utils.mjs";
  10. import CryptoApi from "crypto-api/src/crypto-api.mjs";
  11. /**
  12. * Generic hash function.
  13. *
  14. * @param {string} name
  15. * @param {ArrayBuffer} input
  16. * @param {Object} [options={}]
  17. * @returns {string}
  18. */
  19. export function runHash(name, input, options={}) {
  20. const msg = Utils.arrayBufferToStr(input, false),
  21. hasher = CryptoApi.getHasher(name, options);
  22. hasher.update(msg);
  23. return CryptoApi.encoder.toHex(hasher.finalize());
  24. }