hash-fn.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const native_1 = __importDefault(require("./native"));
  7. const hash_fn_1 = require("../base/hash-fn");
  8. /**
  9. * @hidden
  10. */
  11. exports.normalizeInput = (input, encoding) => {
  12. if (input instanceof Buffer) {
  13. return input;
  14. }
  15. if (typeof input === 'string') {
  16. return Buffer.from(input, encoding);
  17. }
  18. return Buffer.from(input);
  19. };
  20. /**
  21. * Returns a blake3 hash of the input, returning the binary hash data.
  22. */
  23. function hash(input, { length = hash_fn_1.defaultHashLength } = {}) {
  24. return native_1.default.hash(exports.normalizeInput(input), length);
  25. }
  26. exports.hash = hash;
  27. /**
  28. * Given cryptographic key material and a context string, services a subkey of
  29. * any length. See {@link https://docs.rs/blake3/0.1.3/blake3/fn.derive_key.html}
  30. * for more information.
  31. */
  32. function deriveKey(context, material, { length = hash_fn_1.defaultHashLength } = {}) {
  33. const hasher = new native_1.default.Hasher(undefined, context);
  34. hasher.update(exports.normalizeInput(material));
  35. const result = Buffer.alloc(length);
  36. hasher.digest(result);
  37. return result;
  38. }
  39. exports.deriveKey = deriveKey;
  40. /**
  41. * The keyed hash function. See {@link https://docs.rs/blake3/0.1.3/blake3/fn.keyed_hash.html}.
  42. */
  43. function keyedHash(key, input, { length = hash_fn_1.defaultHashLength } = {}) {
  44. const hasher = new native_1.default.Hasher(key);
  45. hasher.update(exports.normalizeInput(input));
  46. const result = Buffer.alloc(length);
  47. hasher.digest(result);
  48. return result;
  49. }
  50. exports.keyedHash = keyedHash;
  51. //# sourceMappingURL=hash-fn.js.map