hash-fn.js 1.7 KB

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