hash-fn.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const hash_fn_1 = require("../base/hash-fn");
  4. const hash_1 = require("./hash");
  5. const wasm_1 = require("./wasm");
  6. const textEncoder = new TextEncoder();
  7. /**
  8. * @hidden
  9. */
  10. exports.normalizeInput = (input) => hash_fn_1.inputToArray(typeof input === 'string' ? textEncoder.encode(input) : input);
  11. /**
  12. * Returns a blake3 hash of the input.
  13. */
  14. function hash(input, { length = hash_fn_1.defaultHashLength } = {}) {
  15. const result = new hash_1.Hash(length);
  16. wasm_1.getWasm().hash(exports.normalizeInput(input), result);
  17. return result;
  18. }
  19. exports.hash = hash;
  20. /**
  21. * Given cryptographic key material and a context string, services a subkey of
  22. * any length. See {@link https://docs.rs/blake3/0.1.3/blake3/fn.derive_key.html}
  23. * for more information.
  24. */
  25. function deriveKey(context, material, { length = hash_fn_1.defaultHashLength } = {}) {
  26. const derive = wasm_1.getWasm().create_derive(context);
  27. derive.update(exports.normalizeInput(material));
  28. const result = new hash_1.Hash(length);
  29. derive.digest(result);
  30. return result;
  31. }
  32. exports.deriveKey = deriveKey;
  33. /**
  34. * The keyed hash function. See {@link https://docs.rs/blake3/0.1.3/blake3/fn.keyed_hash.html}.
  35. */
  36. function keyedHash(key, input, { length = hash_fn_1.defaultHashLength } = {}) {
  37. if (key.length !== 32) {
  38. throw new Error(`key provided to keyedHash must be 32 bytes, got ${key.length}`);
  39. }
  40. const derive = wasm_1.getWasm().create_keyed(key);
  41. derive.update(exports.normalizeInput(input));
  42. const result = new hash_1.Hash(length);
  43. derive.digest(result);
  44. return result;
  45. }
  46. exports.keyedHash = keyedHash;
  47. //# sourceMappingURL=hash-fn.js.map