@zudojs/crypto 1.3.2 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (218) hide show
  1. package/README.md +5 -5
  2. package/dist/compare/compare.helper.d.ts +0 -1
  3. package/dist/compare/compare.helper.js +0 -1
  4. package/dist/compare/index.d.ts +0 -1
  5. package/dist/compare/index.js +0 -1
  6. package/dist/cryptoCipher/cryptoCipher.core.d.ts +12 -1
  7. package/dist/cryptoCipher/cryptoCipher.core.js +9 -1
  8. package/dist/cryptoCipher/cryptoCipher.envelope.d.ts +0 -1
  9. package/dist/cryptoCipher/cryptoCipher.envelope.js +0 -1
  10. package/dist/cryptoCipher/cryptoCipher.nonceGuard.d.ts +24 -0
  11. package/dist/cryptoCipher/cryptoCipher.nonceGuard.js +50 -0
  12. package/dist/cryptoCipher/cryptoCipher.string.d.ts +0 -1
  13. package/dist/cryptoCipher/cryptoCipher.string.js +0 -1
  14. package/dist/cryptoCipher/index.d.ts +1 -1
  15. package/dist/cryptoCipher/index.js +1 -1
  16. package/dist/cryptoConstants/cryptoConstants.config.d.ts +0 -1
  17. package/dist/cryptoConstants/cryptoConstants.config.js +0 -1
  18. package/dist/cryptoConstants/cryptoConstants.guard.d.ts +0 -1
  19. package/dist/cryptoConstants/cryptoConstants.guard.js +0 -1
  20. package/dist/cryptoConstants/cryptoConstants.security.d.ts +0 -1
  21. package/dist/cryptoConstants/cryptoConstants.security.js +0 -1
  22. package/dist/cryptoConstants/cryptoConstants.token.d.ts +0 -1
  23. package/dist/cryptoConstants/cryptoConstants.token.js +0 -1
  24. package/dist/cryptoConstants/cryptoConstants.type.d.ts +0 -1
  25. package/dist/cryptoConstants/cryptoConstants.type.js +0 -1
  26. package/dist/cryptoConstants/index.d.ts +0 -1
  27. package/dist/cryptoConstants/index.js +0 -1
  28. package/dist/cryptoEncoding/cryptoEncoding.arrayBuffer.d.ts +0 -1
  29. package/dist/cryptoEncoding/cryptoEncoding.arrayBuffer.js +0 -1
  30. package/dist/cryptoEncoding/cryptoEncoding.compare.d.ts +0 -1
  31. package/dist/cryptoEncoding/cryptoEncoding.compare.js +0 -1
  32. package/dist/cryptoEncoding/cryptoEncoding.core.d.ts +0 -1
  33. package/dist/cryptoEncoding/cryptoEncoding.core.js +0 -1
  34. package/dist/cryptoEncoding/encoding/cryptoEncoding.base64.d.ts +0 -1
  35. package/dist/cryptoEncoding/encoding/cryptoEncoding.base64.js +0 -1
  36. package/dist/cryptoEncoding/encoding/cryptoEncoding.base64url.d.ts +0 -1
  37. package/dist/cryptoEncoding/encoding/cryptoEncoding.base64url.js +0 -1
  38. package/dist/cryptoEncoding/encoding/cryptoEncoding.hex.d.ts +0 -1
  39. package/dist/cryptoEncoding/encoding/cryptoEncoding.hex.js +0 -1
  40. package/dist/cryptoEncoding/encoding/index.d.ts +0 -1
  41. package/dist/cryptoEncoding/encoding/index.js +0 -1
  42. package/dist/cryptoEncoding/index.d.ts +0 -1
  43. package/dist/cryptoEncoding/index.js +0 -1
  44. package/dist/cryptoErrors/cryptoErrors.helper.d.ts +13 -1
  45. package/dist/cryptoErrors/cryptoErrors.helper.js +18 -1
  46. package/dist/cryptoErrors/index.d.ts +0 -1
  47. package/dist/cryptoErrors/index.js +0 -1
  48. package/dist/cryptoFactory/cryptoFactory.core.d.ts +0 -1
  49. package/dist/cryptoFactory/cryptoFactory.core.js +0 -1
  50. package/dist/cryptoFactory/cryptoFactory.encoding.d.ts +0 -1
  51. package/dist/cryptoFactory/cryptoFactory.encoding.js +0 -1
  52. package/dist/cryptoFactory/cryptoFactory.password.d.ts +0 -1
  53. package/dist/cryptoFactory/cryptoFactory.password.js +0 -1
  54. package/dist/cryptoFactory/cryptoFactory.token.d.ts +0 -1
  55. package/dist/cryptoFactory/cryptoFactory.token.js +0 -1
  56. package/dist/cryptoFactory/index.d.ts +0 -1
  57. package/dist/cryptoFactory/index.js +0 -1
  58. package/dist/cryptoHash/cryptoHash.codec.d.ts +0 -1
  59. package/dist/cryptoHash/cryptoHash.codec.js +0 -1
  60. package/dist/cryptoHash/cryptoHash.compare.d.ts +0 -1
  61. package/dist/cryptoHash/cryptoHash.compare.js +0 -1
  62. package/dist/cryptoHash/cryptoHash.convenience.d.ts +0 -1
  63. package/dist/cryptoHash/cryptoHash.convenience.js +0 -1
  64. package/dist/cryptoHash/cryptoHash.core.d.ts +0 -1
  65. package/dist/cryptoHash/cryptoHash.core.js +0 -1
  66. package/dist/cryptoHash/cryptoHmac.core.d.ts +9 -3
  67. package/dist/cryptoHash/cryptoHmac.core.js +15 -5
  68. package/dist/cryptoHash/index.d.ts +0 -1
  69. package/dist/cryptoHash/index.js +0 -1
  70. package/dist/cryptoKey/cryptoKey.factory.d.ts +6 -1
  71. package/dist/cryptoKey/cryptoKey.factory.js +8 -2
  72. package/dist/cryptoKey/cryptoKey.type.d.ts +12 -1
  73. package/dist/cryptoKey/cryptoKey.type.js +0 -1
  74. package/dist/cryptoKey/cryptoKey.utils.d.ts +0 -1
  75. package/dist/cryptoKey/cryptoKey.utils.js +0 -1
  76. package/dist/cryptoKey/index.d.ts +0 -1
  77. package/dist/cryptoKey/index.js +0 -1
  78. package/dist/cryptoKeyDerivation/cryptoKeyDerivation.core.d.ts +0 -1
  79. package/dist/cryptoKeyDerivation/cryptoKeyDerivation.core.js +0 -1
  80. package/dist/cryptoKeyDerivation/cryptoKeyDerivation.salt.d.ts +4 -2
  81. package/dist/cryptoKeyDerivation/cryptoKeyDerivation.salt.js +6 -3
  82. package/dist/cryptoKeyDerivation/cryptoKeyDerivation.type.d.ts +0 -1
  83. package/dist/cryptoKeyDerivation/cryptoKeyDerivation.type.js +0 -1
  84. package/dist/cryptoKeyDerivation/cryptoKeyDerivation.validate.d.ts +6 -1
  85. package/dist/cryptoKeyDerivation/cryptoKeyDerivation.validate.js +17 -11
  86. package/dist/cryptoKeyDerivation/index.d.ts +0 -1
  87. package/dist/cryptoKeyDerivation/index.js +0 -1
  88. package/dist/cryptoPassword/cryptoPassword.codec.d.ts +0 -1
  89. package/dist/cryptoPassword/cryptoPassword.codec.js +0 -1
  90. package/dist/cryptoPassword/cryptoPassword.core.d.ts +10 -1
  91. package/dist/cryptoPassword/cryptoPassword.core.js +13 -4
  92. package/dist/cryptoPassword/cryptoPassword.helper.d.ts +10 -2
  93. package/dist/cryptoPassword/cryptoPassword.helper.js +26 -4
  94. package/dist/cryptoPassword/cryptoPassword.type.d.ts +0 -1
  95. package/dist/cryptoPassword/cryptoPassword.type.js +0 -1
  96. package/dist/cryptoPassword/cryptoPassword.validate.d.ts +4 -6
  97. package/dist/cryptoPassword/cryptoPassword.validate.js +13 -24
  98. package/dist/cryptoPassword/index.d.ts +0 -1
  99. package/dist/cryptoPassword/index.js +0 -1
  100. package/dist/cryptoProvider/cryptoProvider.capability.d.ts +0 -1
  101. package/dist/cryptoProvider/cryptoProvider.capability.js +0 -1
  102. package/dist/cryptoProvider/cryptoProvider.core.d.ts +0 -1
  103. package/dist/cryptoProvider/cryptoProvider.core.js +0 -1
  104. package/dist/cryptoProvider/cryptoProvider.default.d.ts +0 -1
  105. package/dist/cryptoProvider/cryptoProvider.default.js +0 -1
  106. package/dist/cryptoProvider/cryptoProvider.interface.d.ts +0 -1
  107. package/dist/cryptoProvider/cryptoProvider.interface.js +0 -1
  108. package/dist/cryptoProvider/cryptoProvider.type.d.ts +0 -1
  109. package/dist/cryptoProvider/cryptoProvider.type.js +0 -1
  110. package/dist/cryptoProvider/index.d.ts +0 -1
  111. package/dist/cryptoProvider/index.js +0 -1
  112. package/dist/cryptoProvider/types/cryptoCipher.type.d.ts +0 -1
  113. package/dist/cryptoProvider/types/cryptoCipher.type.js +0 -1
  114. package/dist/cryptoProvider/types/cryptoHash.type.d.ts +0 -1
  115. package/dist/cryptoProvider/types/cryptoHash.type.js +0 -1
  116. package/dist/cryptoProvider/types/cryptoKeyDerivation.type.d.ts +0 -1
  117. package/dist/cryptoProvider/types/cryptoKeyDerivation.type.js +0 -1
  118. package/dist/cryptoProvider/types/cryptoRandom.type.d.ts +0 -1
  119. package/dist/cryptoProvider/types/cryptoRandom.type.js +0 -1
  120. package/dist/cryptoProvider/types/cryptoSignature.type.d.ts +9 -3
  121. package/dist/cryptoProvider/types/cryptoSignature.type.js +0 -1
  122. package/dist/cryptoProvider/types/index.d.ts +1 -2
  123. package/dist/cryptoProvider/types/index.js +0 -1
  124. package/dist/cryptoRandom/cryptoRandom.choice.d.ts +0 -1
  125. package/dist/cryptoRandom/cryptoRandom.choice.js +0 -1
  126. package/dist/cryptoRandom/cryptoRandom.core.d.ts +0 -1
  127. package/dist/cryptoRandom/cryptoRandom.core.js +0 -1
  128. package/dist/cryptoRandom/cryptoRandom.string.d.ts +7 -1
  129. package/dist/cryptoRandom/cryptoRandom.string.js +7 -1
  130. package/dist/cryptoRandom/index.d.ts +0 -1
  131. package/dist/cryptoRandom/index.js +0 -1
  132. package/dist/cryptoService/cryptoService.core.d.ts +0 -1
  133. package/dist/cryptoService/cryptoService.core.js +0 -1
  134. package/dist/cryptoService/cryptoService.derivation.d.ts +0 -1
  135. package/dist/cryptoService/cryptoService.derivation.js +0 -1
  136. package/dist/cryptoService/cryptoService.encoding.d.ts +0 -1
  137. package/dist/cryptoService/cryptoService.encoding.js +0 -1
  138. package/dist/cryptoService/index.d.ts +0 -1
  139. package/dist/cryptoService/index.js +0 -1
  140. package/dist/cryptoService/operations/cryptoService.cipher.d.ts +0 -1
  141. package/dist/cryptoService/operations/cryptoService.cipher.js +0 -1
  142. package/dist/cryptoService/operations/cryptoService.hash.d.ts +0 -1
  143. package/dist/cryptoService/operations/cryptoService.hash.js +0 -1
  144. package/dist/cryptoService/operations/cryptoService.password.d.ts +0 -1
  145. package/dist/cryptoService/operations/cryptoService.password.js +0 -1
  146. package/dist/cryptoService/operations/cryptoService.token.d.ts +0 -1
  147. package/dist/cryptoService/operations/cryptoService.token.js +0 -1
  148. package/dist/cryptoService/operations/index.d.ts +0 -1
  149. package/dist/cryptoService/operations/index.js +0 -1
  150. package/dist/cryptoSignature/cryptoSignature.core.d.ts +5 -6
  151. package/dist/cryptoSignature/cryptoSignature.core.js +0 -1
  152. package/dist/cryptoSignature/index.d.ts +0 -1
  153. package/dist/cryptoSignature/index.js +0 -1
  154. package/dist/cryptoToken/cryptoToken.core.d.ts +0 -1
  155. package/dist/cryptoToken/cryptoToken.core.js +0 -1
  156. package/dist/cryptoToken/cryptoToken.hash.d.ts +0 -1
  157. package/dist/cryptoToken/cryptoToken.hash.js +0 -1
  158. package/dist/cryptoToken/cryptoToken.utils.d.ts +0 -1
  159. package/dist/cryptoToken/cryptoToken.utils.js +0 -1
  160. package/dist/cryptoToken/index.d.ts +0 -1
  161. package/dist/cryptoToken/index.js +0 -1
  162. package/dist/cryptoUtils/cryptoUtils.compare.d.ts +0 -1
  163. package/dist/cryptoUtils/cryptoUtils.compare.js +0 -1
  164. package/dist/cryptoUtils/cryptoUtils.conversion.d.ts +0 -1
  165. package/dist/cryptoUtils/cryptoUtils.conversion.js +0 -1
  166. package/dist/cryptoUtils/cryptoUtils.number.d.ts +0 -1
  167. package/dist/cryptoUtils/cryptoUtils.number.js +0 -1
  168. package/dist/cryptoUtils/cryptoUtils.text.d.ts +0 -1
  169. package/dist/cryptoUtils/cryptoUtils.text.js +0 -1
  170. package/dist/cryptoUtils/cryptoUtils.validation.d.ts +0 -1
  171. package/dist/cryptoUtils/cryptoUtils.validation.js +0 -1
  172. package/dist/cryptoUtils/encoding/cryptoUtils.base64.d.ts +0 -1
  173. package/dist/cryptoUtils/encoding/cryptoUtils.base64.js +0 -1
  174. package/dist/cryptoUtils/encoding/cryptoUtils.base64url.d.ts +0 -1
  175. package/dist/cryptoUtils/encoding/cryptoUtils.base64url.js +0 -1
  176. package/dist/cryptoUtils/encoding/cryptoUtils.hex.d.ts +0 -1
  177. package/dist/cryptoUtils/encoding/cryptoUtils.hex.js +0 -1
  178. package/dist/cryptoUtils/encoding/index.d.ts +0 -1
  179. package/dist/cryptoUtils/encoding/index.js +0 -1
  180. package/dist/cryptoUtils/index.d.ts +0 -1
  181. package/dist/cryptoUtils/index.js +0 -1
  182. package/dist/index.d.ts +0 -1
  183. package/dist/index.js +0 -1
  184. package/dist/node/index.d.ts +0 -1
  185. package/dist/node/index.js +0 -1
  186. package/dist/node/nodeCryptoProvider/index.d.ts +0 -1
  187. package/dist/node/nodeCryptoProvider/index.js +0 -1
  188. package/dist/node/nodeCryptoProvider/nodeCryptoProvider.core.d.ts +0 -1
  189. package/dist/node/nodeCryptoProvider/nodeCryptoProvider.core.js +0 -1
  190. package/dist/node/nodeCryptoProvider/nodeCryptoProvider.factory.d.ts +0 -1
  191. package/dist/node/nodeCryptoProvider/nodeCryptoProvider.factory.js +0 -1
  192. package/dist/node/nodeCryptoProvider/nodeCryptoProvider.helper.d.ts +0 -1
  193. package/dist/node/nodeCryptoProvider/nodeCryptoProvider.helper.js +0 -1
  194. package/dist/node/nodeCryptoProvider/operations/index.d.ts +0 -1
  195. package/dist/node/nodeCryptoProvider/operations/index.js +0 -1
  196. package/dist/node/nodeCryptoProvider/operations/nodeCryptoProvider.derivation.d.ts +0 -1
  197. package/dist/node/nodeCryptoProvider/operations/nodeCryptoProvider.derivation.js +0 -1
  198. package/dist/node/nodeCryptoProvider/operations/nodeCryptoProvider.password.d.ts +0 -1
  199. package/dist/node/nodeCryptoProvider/operations/nodeCryptoProvider.password.js +0 -1
  200. package/dist/node/nodeCryptoProvider/primitives/index.d.ts +0 -1
  201. package/dist/node/nodeCryptoProvider/primitives/index.js +0 -1
  202. package/dist/node/nodeCryptoProvider/primitives/nodeCryptoProvider.encryption.d.ts +0 -1
  203. package/dist/node/nodeCryptoProvider/primitives/nodeCryptoProvider.encryption.js +0 -1
  204. package/dist/node/nodeCryptoProvider/primitives/nodeCryptoProvider.hash.d.ts +0 -1
  205. package/dist/node/nodeCryptoProvider/primitives/nodeCryptoProvider.hash.js +0 -1
  206. package/dist/node/nodeCryptoProvider/primitives/nodeCryptoProvider.random.d.ts +0 -1
  207. package/dist/node/nodeCryptoProvider/primitives/nodeCryptoProvider.random.js +0 -1
  208. package/dist/node/nodeCryptoProvider/primitives/nodeCryptoProvider.signing.d.ts +0 -1
  209. package/dist/node/nodeCryptoProvider/primitives/nodeCryptoProvider.signing.js +0 -1
  210. package/dist/node/signing/index.d.ts +0 -1
  211. package/dist/node/signing/index.js +0 -1
  212. package/dist/node/signing/signing.conversion.d.ts +0 -1
  213. package/dist/node/signing/signing.conversion.js +0 -1
  214. package/dist/node/signing/signing.core.d.ts +0 -1
  215. package/dist/node/signing/signing.core.js +0 -1
  216. package/dist/node/signing/signing.utils.d.ts +0 -1
  217. package/dist/node/signing/signing.utils.js +0 -1
  218. package/package.json +3 -3
@@ -3,8 +3,15 @@ import type { HashInput } from "./cryptoHash.core.js";
3
3
  /**
4
4
  * Calculates a keyed HMAC.
5
5
  *
6
- * The key must be at least `KEY_SIZE.MIN_HMAC_KEY_BYTES` (16) bytes so
7
- * that a missing or empty secret cannot silently produce forgeable tags.
6
+ * The key is raw bytes, not text: a string secret must be encoded first
7
+ * (`new TextEncoder().encode(secret)` or `Buffer.from(secret, "utf8")`), and
8
+ * it must be at least `KEY_SIZE.MIN_HMAC_KEY_BYTES` (16) bytes so that a
9
+ * missing or empty secret cannot silently produce forgeable tags.
10
+ *
11
+ * @throws {TypeError} when the key is not a `Uint8Array` or the algorithm
12
+ * name is unknown (argument-shape errors).
13
+ * @throws {CryptoError} `CRYPTO_HASH` when the key is shorter than
14
+ * `KEY_SIZE.MIN_HMAC_KEY_BYTES`.
8
15
  */
9
16
  export declare function hmac(input: HashInput, key: Uint8Array, algorithm?: HmacAlgorithm, encoding?: HashEncoding, provider?: CryptoProvider): Promise<string>;
10
17
  /**
@@ -19,4 +26,3 @@ export declare function hmacSha384(input: HashInput, key: Uint8Array, encoding?:
19
26
  * Calculates a SHA-512 HMAC.
20
27
  */
21
28
  export declare function hmacSha512(input: HashInput, key: Uint8Array, encoding?: HashEncoding, provider?: CryptoProvider): Promise<string>;
22
- //# sourceMappingURL=cryptoHmac.core.d.ts.map
@@ -2,22 +2,33 @@ import { getDefaultCryptoProvider } from "../cryptoProvider/cryptoProvider.defau
2
2
  import { assertHmacCapability } from "../cryptoProvider/cryptoProvider.capability.js";
3
3
  import { isHmacAlgorithmName } from "../cryptoProvider/cryptoProvider.type.js";
4
4
  import { KEY_SIZE } from "../cryptoConstants/cryptoConstants.type.js";
5
+ import { hashError } from "../cryptoErrors/cryptoErrors.helper.js";
5
6
  import { encodeDigest } from "./cryptoHash.codec.js";
6
7
  /**
7
8
  * Calculates a keyed HMAC.
8
9
  *
9
- * The key must be at least `KEY_SIZE.MIN_HMAC_KEY_BYTES` (16) bytes so
10
- * that a missing or empty secret cannot silently produce forgeable tags.
10
+ * The key is raw bytes, not text: a string secret must be encoded first
11
+ * (`new TextEncoder().encode(secret)` or `Buffer.from(secret, "utf8")`), and
12
+ * it must be at least `KEY_SIZE.MIN_HMAC_KEY_BYTES` (16) bytes so that a
13
+ * missing or empty secret cannot silently produce forgeable tags.
14
+ *
15
+ * @throws {TypeError} when the key is not a `Uint8Array` or the algorithm
16
+ * name is unknown (argument-shape errors).
17
+ * @throws {CryptoError} `CRYPTO_HASH` when the key is shorter than
18
+ * `KEY_SIZE.MIN_HMAC_KEY_BYTES`.
11
19
  */
12
20
  export async function hmac(input, key, algorithm = "sha256", encoding = "hex", provider = getDefaultCryptoProvider()) {
13
21
  if (!isHmacAlgorithmName(algorithm)) {
14
22
  throw new TypeError(`Unsupported HMAC algorithm: ${String(algorithm)}.`);
15
23
  }
16
24
  if (!(key instanceof Uint8Array)) {
17
- throw new TypeError("HMAC key must be a Uint8Array.");
25
+ throw new TypeError("HMAC key must be a Uint8Array of raw key bytes" +
26
+ (typeof key === "string"
27
+ ? '; encode a string secret first, e.g. new TextEncoder().encode(secret) or Buffer.from(secret, "utf8").'
28
+ : "."));
18
29
  }
19
30
  if (key.byteLength < KEY_SIZE.MIN_HMAC_KEY_BYTES) {
20
- throw new RangeError(`HMAC key must be at least ${KEY_SIZE.MIN_HMAC_KEY_BYTES} bytes.`);
31
+ throw hashError(`HMAC key must be at least ${KEY_SIZE.MIN_HMAC_KEY_BYTES} bytes, received ${key.byteLength}.`, `hmac-${algorithm}`);
21
32
  }
22
33
  assertHmacCapability(provider);
23
34
  const digest = await provider.hmac(algorithm, key, input);
@@ -41,4 +52,3 @@ export async function hmacSha384(input, key, encoding = "hex", provider) {
41
52
  export async function hmacSha512(input, key, encoding = "hex", provider) {
42
53
  return hmac(input, key, "sha512", encoding, provider);
43
54
  }
44
- //# sourceMappingURL=cryptoHmac.core.js.map
@@ -8,4 +8,3 @@ export * from "./cryptoHash.convenience.js";
8
8
  export * from "./cryptoHmac.core.js";
9
9
  export * from "./cryptoHash.compare.js";
10
10
  export * from "./cryptoHash.codec.js";
11
- //# sourceMappingURL=index.d.ts.map
@@ -8,4 +8,3 @@ export * from "./cryptoHash.convenience.js";
8
8
  export * from "./cryptoHmac.core.js";
9
9
  export * from "./cryptoHash.compare.js";
10
10
  export * from "./cryptoHash.codec.js";
11
- //# sourceMappingURL=index.js.map
@@ -30,9 +30,15 @@ export declare function createCryptoKey(bytes: Uint8Array, options: CryptoKeyOpt
30
30
  /**
31
31
  * Generates a cryptographically secure random symmetric key.
32
32
  *
33
+ * `length` is in **bytes** (`generateCryptoKey(32, …)` is a 256-bit key);
34
+ * the returned key reports `byteLength: 32` and `length: 256` (bits).
35
+ *
33
36
  * Only symmetric algorithms (AES-GCM, HMAC, KDF outputs) are accepted:
34
37
  * random bytes are not a usable Ed25519 key, so asymmetric algorithms are
35
38
  * rejected. Use `generateEd25519KeyPair` for signing keys.
39
+ *
40
+ * @throws {CryptoError} `CRYPTO_KEY` when the algorithm is asymmetric or
41
+ * `length` is not a positive integer.
36
42
  */
37
43
  export declare function generateCryptoKey(length: number, options: CryptoKeyOptions, provider?: CryptoProvider): Promise<CryptoKey>;
38
44
  /**
@@ -45,4 +51,3 @@ export declare function defaultKeyLength(algorithm: CryptoAlgorithm): number;
45
51
  * This operation is only permitted for extractable keys.
46
52
  */
47
53
  export declare function exportCryptoKey(key: CryptoKey): Uint8Array;
48
- //# sourceMappingURL=cryptoKey.factory.d.ts.map
@@ -75,6 +75,7 @@ export async function createCryptoKey(bytes, options, provider = getDefaultCrypt
75
75
  extractable: options.extractable ?? false,
76
76
  createdAt,
77
77
  length: keyBytes.byteLength * 8,
78
+ byteLength: keyBytes.byteLength,
78
79
  fingerprint,
79
80
  bytes: () => new Uint8Array(keyBytes),
80
81
  });
@@ -82,16 +83,22 @@ export async function createCryptoKey(bytes, options, provider = getDefaultCrypt
82
83
  /**
83
84
  * Generates a cryptographically secure random symmetric key.
84
85
  *
86
+ * `length` is in **bytes** (`generateCryptoKey(32, …)` is a 256-bit key);
87
+ * the returned key reports `byteLength: 32` and `length: 256` (bits).
88
+ *
85
89
  * Only symmetric algorithms (AES-GCM, HMAC, KDF outputs) are accepted:
86
90
  * random bytes are not a usable Ed25519 key, so asymmetric algorithms are
87
91
  * rejected. Use `generateEd25519KeyPair` for signing keys.
92
+ *
93
+ * @throws {CryptoError} `CRYPTO_KEY` when the algorithm is asymmetric or
94
+ * `length` is not a positive integer.
88
95
  */
89
96
  export async function generateCryptoKey(length, options, provider = getDefaultCryptoProvider()) {
90
97
  if (!isSymmetricKeyAlgorithm(options.algorithm)) {
91
98
  throw keyError(`Random key generation is only supported for symmetric algorithms, not "${String(options.algorithm)}".`, CryptoOperation.KEY_GENERATION, typeof options.algorithm === "string" ? options.algorithm : undefined);
92
99
  }
93
100
  if (!Number.isInteger(length) || length <= 0) {
94
- throw new RangeError("Cryptographic key length must be a positive integer.");
101
+ throw keyError("Cryptographic key length must be a positive integer number of bytes.", CryptoOperation.KEY_GENERATION, options.algorithm);
95
102
  }
96
103
  assertRandomCapability(provider);
97
104
  const bytes = await provider.randomBytes(length);
@@ -137,4 +144,3 @@ async function generateKeyId(provider) {
137
144
  const idBytes = await provider.randomBytes(16);
138
145
  return encode(idBytes, "hex");
139
146
  }
140
- //# sourceMappingURL=cryptoKey.factory.js.map
@@ -17,6 +17,15 @@ export declare enum CryptoKeyUsage {
17
17
  *
18
18
  * The underlying byte array is copied when a key is created and when
19
19
  * its bytes are requested to prevent accidental mutation.
20
+ *
21
+ * Units: `length` is in **bits** (256 for a 32-byte key) and `byteLength`
22
+ * in bytes; `generateCryptoKey(length)` takes bytes.
23
+ *
24
+ * `extractable` gates the public export path (`exportCryptoKey`) and
25
+ * conversion to a signing key. `bytes()` is the raw accessor the providers
26
+ * use to perform operations with the key, so it is available regardless of
27
+ * `extractable`; treat it as internal and hand keys to the package's
28
+ * functions rather than reading them out.
20
29
  */
21
30
  export interface CryptoKey {
22
31
  readonly algorithm: CryptoAlgorithm;
@@ -24,7 +33,10 @@ export interface CryptoKey {
24
33
  readonly usages: readonly CryptoKeyUsage[];
25
34
  readonly extractable: boolean;
26
35
  readonly createdAt: number;
36
+ /** Key size in bits. */
27
37
  readonly length: number;
38
+ /** Key size in bytes (`length / 8`). */
39
+ readonly byteLength: number;
28
40
  readonly fingerprint: string;
29
41
  readonly bytes: () => Uint8Array;
30
42
  }
@@ -41,4 +53,3 @@ export interface CryptoKeyOptions {
41
53
  * Checks whether a value is a CryptoKey.
42
54
  */
43
55
  export declare function isCryptoKey(value: unknown): value is CryptoKey;
44
- //# sourceMappingURL=cryptoKey.type.d.ts.map
@@ -29,4 +29,3 @@ export function isCryptoKey(value) {
29
29
  typeof key.fingerprint === "string" &&
30
30
  typeof key.bytes === "function");
31
31
  }
32
- //# sourceMappingURL=cryptoKey.type.js.map
@@ -12,4 +12,3 @@ export declare function getCryptoKeyFingerprint(key: CryptoKey): string;
12
12
  * Compares two cryptographic keys by identity.
13
13
  */
14
14
  export declare function cryptoKeysEqual(left: CryptoKey, right: CryptoKey): boolean;
15
- //# sourceMappingURL=cryptoKey.utils.d.ts.map
@@ -17,4 +17,3 @@ export function cryptoKeysEqual(left, right) {
17
17
  left.fingerprint === right.fingerprint &&
18
18
  left.algorithm === right.algorithm);
19
19
  }
20
- //# sourceMappingURL=cryptoKey.utils.js.map
@@ -6,4 +6,3 @@
6
6
  export * from "./cryptoKey.type.js";
7
7
  export * from "./cryptoKey.factory.js";
8
8
  export * from "./cryptoKey.utils.js";
9
- //# sourceMappingURL=index.d.ts.map
@@ -6,4 +6,3 @@
6
6
  export * from "./cryptoKey.type.js";
7
7
  export * from "./cryptoKey.factory.js";
8
8
  export * from "./cryptoKey.utils.js";
9
- //# sourceMappingURL=index.js.map
@@ -19,4 +19,3 @@ export declare function deriveScrypt(password: string | Uint8Array, options?: Sc
19
19
  * Derives a key using the selected key derivation algorithm.
20
20
  */
21
21
  export declare function deriveKey(password: string | Uint8Array, algorithm: CryptoAlgorithm, options?: Pbkdf2Options | ScryptOptions): Promise<DerivedKeyResult>;
22
- //# sourceMappingURL=cryptoKeyDerivation.core.d.ts.map
@@ -104,4 +104,3 @@ export async function deriveKey(password, algorithm, options = {}) {
104
104
  throw new TypeError(`Unsupported key derivation algorithm: ${String(algorithm)}.`);
105
105
  }
106
106
  }
107
- //# sourceMappingURL=cryptoKeyDerivation.core.js.map
@@ -1,6 +1,8 @@
1
1
  import type { CryptoProvider } from "../cryptoProvider/index.js";
2
2
  /**
3
- * Creates a random salt.
3
+ * Creates a random salt of `length` bytes (default 16).
4
+ *
5
+ * @throws {CryptoError} `CRYPTO_DERIVATION` when `length` is not an integer
6
+ * of at least 16.
4
7
  */
5
8
  export declare function generateSalt(length?: number, provider?: CryptoProvider): Promise<Uint8Array>;
6
- //# sourceMappingURL=cryptoKeyDerivation.salt.d.ts.map
@@ -1,13 +1,16 @@
1
1
  import { getDefaultCryptoProvider } from "../cryptoProvider/cryptoProvider.default.js";
2
2
  import { assertRandomCapability } from "../cryptoProvider/cryptoProvider.capability.js";
3
+ import { keyDerivationError } from "../cryptoErrors/cryptoErrors.helper.js";
3
4
  /**
4
- * Creates a random salt.
5
+ * Creates a random salt of `length` bytes (default 16).
6
+ *
7
+ * @throws {CryptoError} `CRYPTO_DERIVATION` when `length` is not an integer
8
+ * of at least 16.
5
9
  */
6
10
  export async function generateSalt(length = 16, provider = getDefaultCryptoProvider()) {
7
11
  assertRandomCapability(provider);
8
12
  if (!Number.isInteger(length) || length < 16) {
9
- throw new RangeError("Salt length must be an integer of at least 16 bytes.");
13
+ throw keyDerivationError("Salt length must be an integer of at least 16 bytes.", undefined);
10
14
  }
11
15
  return provider.randomBytes(length);
12
16
  }
13
- //# sourceMappingURL=cryptoKeyDerivation.salt.js.map
@@ -24,4 +24,3 @@ export interface ScryptOptions {
24
24
  readonly maxMemory?: number;
25
25
  readonly provider?: CryptoProvider;
26
26
  }
27
- //# sourceMappingURL=cryptoKeyDerivation.type.d.ts.map
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=cryptoKeyDerivation.type.js.map
@@ -5,6 +5,9 @@
5
5
  * `PASSWORD_HASH.LIMITS.MAX_PBKDF2_ITERATIONS` and `keyLength` by
6
6
  * `PASSWORD_HASH.LIMITS.MAX_DERIVED_KEY_BYTES`, so a work factor read from
7
7
  * configuration cannot request unbounded CPU time.
8
+ *
9
+ * @throws {CryptoError} `CRYPTO_DERIVATION` for an out-of-range work factor,
10
+ * key length or salt; `TypeError` for an unknown digest name.
8
11
  */
9
12
  export declare function validatePbkdf2Options(iterations: number, keyLength: number, salt: Uint8Array, digest?: unknown): void;
10
13
  /**
@@ -15,6 +18,8 @@ export declare function validatePbkdf2Options(iterations: number, keyLength: num
15
18
  * memory) by `MAX_SCRYPT_MEMORY_BYTES`. Without these ceilings the memory
16
19
  * "bound" passed to the provider was derived from the very parameters it
17
20
  * was meant to bound, so a cost of 2^30 allocated terabytes.
21
+ *
22
+ * @throws {CryptoError} `CRYPTO_DERIVATION` for any parameter outside its
23
+ * bound.
18
24
  */
19
25
  export declare function validateScryptOptions(keyLength: number, cost: number, blockSize: number, parallelization: number, salt: Uint8Array, maxMemory?: number): void;
20
- //# sourceMappingURL=cryptoKeyDerivation.validate.d.ts.map
@@ -1,5 +1,6 @@
1
1
  import { PASSWORD_HASH } from "../cryptoConstants/cryptoConstants.security.js";
2
2
  import { isPbkdf2Digest } from "../cryptoProvider/cryptoProvider.type.js";
3
+ import { keyDerivationError } from "../cryptoErrors/cryptoErrors.helper.js";
3
4
  const LIMITS = PASSWORD_HASH.LIMITS;
4
5
  /**
5
6
  * Validates PBKDF2 key derivation options.
@@ -8,20 +9,23 @@ const LIMITS = PASSWORD_HASH.LIMITS;
8
9
  * `PASSWORD_HASH.LIMITS.MAX_PBKDF2_ITERATIONS` and `keyLength` by
9
10
  * `PASSWORD_HASH.LIMITS.MAX_DERIVED_KEY_BYTES`, so a work factor read from
10
11
  * configuration cannot request unbounded CPU time.
12
+ *
13
+ * @throws {CryptoError} `CRYPTO_DERIVATION` for an out-of-range work factor,
14
+ * key length or salt; `TypeError` for an unknown digest name.
11
15
  */
12
16
  export function validatePbkdf2Options(iterations, keyLength, salt, digest = "sha256") {
13
17
  if (!Number.isInteger(iterations) ||
14
18
  iterations < PASSWORD_HASH.PBKDF2.MIN_ITERATIONS ||
15
19
  iterations > LIMITS.MAX_PBKDF2_ITERATIONS) {
16
- throw new RangeError(`PBKDF2 iterations must be an integer between ${PASSWORD_HASH.PBKDF2.MIN_ITERATIONS} and ${LIMITS.MAX_PBKDF2_ITERATIONS}.`);
20
+ throw keyDerivationError(`PBKDF2 iterations must be an integer between ${PASSWORD_HASH.PBKDF2.MIN_ITERATIONS} and ${LIMITS.MAX_PBKDF2_ITERATIONS}.`, "pbkdf2");
17
21
  }
18
22
  if (!Number.isInteger(keyLength) ||
19
23
  keyLength < 16 ||
20
24
  keyLength > LIMITS.MAX_DERIVED_KEY_BYTES) {
21
- throw new RangeError(`PBKDF2 keyLength must be an integer between 16 and ${LIMITS.MAX_DERIVED_KEY_BYTES} bytes.`);
25
+ throw keyDerivationError(`PBKDF2 keyLength must be an integer between 16 and ${LIMITS.MAX_DERIVED_KEY_BYTES} bytes.`, "pbkdf2");
22
26
  }
23
27
  if (!(salt instanceof Uint8Array) || salt.byteLength < 16) {
24
- throw new RangeError("PBKDF2 salt must be at least 16 bytes.");
28
+ throw keyDerivationError("PBKDF2 salt must be at least 16 bytes.", "pbkdf2");
25
29
  }
26
30
  if (!isPbkdf2Digest(digest)) {
27
31
  throw new TypeError(`Unsupported PBKDF2 digest: ${String(digest)}.`);
@@ -35,38 +39,40 @@ export function validatePbkdf2Options(iterations, keyLength, salt, digest = "sha
35
39
  * memory) by `MAX_SCRYPT_MEMORY_BYTES`. Without these ceilings the memory
36
40
  * "bound" passed to the provider was derived from the very parameters it
37
41
  * was meant to bound, so a cost of 2^30 allocated terabytes.
42
+ *
43
+ * @throws {CryptoError} `CRYPTO_DERIVATION` for any parameter outside its
44
+ * bound.
38
45
  */
39
46
  export function validateScryptOptions(keyLength, cost, blockSize, parallelization, salt, maxMemory) {
40
47
  if (!Number.isInteger(keyLength) ||
41
48
  keyLength < 16 ||
42
49
  keyLength > LIMITS.MAX_DERIVED_KEY_BYTES) {
43
- throw new RangeError(`scrypt keyLength must be an integer between 16 and ${LIMITS.MAX_DERIVED_KEY_BYTES} bytes.`);
50
+ throw keyDerivationError(`scrypt keyLength must be an integer between 16 and ${LIMITS.MAX_DERIVED_KEY_BYTES} bytes.`, "scrypt");
44
51
  }
45
52
  if (!Number.isInteger(cost) ||
46
53
  cost < 2 ||
47
54
  cost > LIMITS.MAX_SCRYPT_COST ||
48
55
  (cost & (cost - 1)) !== 0) {
49
- throw new RangeError(`scrypt cost must be a power of two between 2 and ${LIMITS.MAX_SCRYPT_COST}.`);
56
+ throw keyDerivationError(`scrypt cost must be a power of two between 2 and ${LIMITS.MAX_SCRYPT_COST}.`, "scrypt");
50
57
  }
51
58
  if (!Number.isInteger(blockSize) ||
52
59
  blockSize <= 0 ||
53
60
  blockSize > LIMITS.MAX_SCRYPT_BLOCK_SIZE) {
54
- throw new RangeError(`scrypt blockSize must be an integer between 1 and ${LIMITS.MAX_SCRYPT_BLOCK_SIZE}.`);
61
+ throw keyDerivationError(`scrypt blockSize must be an integer between 1 and ${LIMITS.MAX_SCRYPT_BLOCK_SIZE}.`, "scrypt");
55
62
  }
56
63
  if (!Number.isInteger(parallelization) ||
57
64
  parallelization <= 0 ||
58
65
  parallelization > LIMITS.MAX_SCRYPT_PARALLELIZATION) {
59
- throw new RangeError(`scrypt parallelization must be an integer between 1 and ${LIMITS.MAX_SCRYPT_PARALLELIZATION}.`);
66
+ throw keyDerivationError(`scrypt parallelization must be an integer between 1 and ${LIMITS.MAX_SCRYPT_PARALLELIZATION}.`, "scrypt");
60
67
  }
61
68
  if (128 * cost * blockSize > LIMITS.MAX_SCRYPT_MEMORY_BYTES) {
62
- throw new RangeError(`scrypt cost * blockSize exceeds the memory bound of ${LIMITS.MAX_SCRYPT_MEMORY_BYTES} bytes.`);
69
+ throw keyDerivationError(`scrypt cost * blockSize exceeds the memory bound of ${LIMITS.MAX_SCRYPT_MEMORY_BYTES} bytes.`, "scrypt");
63
70
  }
64
71
  if (!(salt instanceof Uint8Array) || salt.byteLength < 16) {
65
- throw new RangeError("scrypt salt must be at least 16 bytes.");
72
+ throw keyDerivationError("scrypt salt must be at least 16 bytes.", "scrypt");
66
73
  }
67
74
  if (maxMemory !== undefined &&
68
75
  (!Number.isInteger(maxMemory) || maxMemory <= 0)) {
69
- throw new RangeError("scrypt maxMemory must be a positive integer.");
76
+ throw keyDerivationError("scrypt maxMemory must be a positive integer.", "scrypt");
70
77
  }
71
78
  }
72
- //# sourceMappingURL=cryptoKeyDerivation.validate.js.map
@@ -7,4 +7,3 @@ export * from "./cryptoKeyDerivation.core.js";
7
7
  export * from "./cryptoKeyDerivation.type.js";
8
8
  export * from "./cryptoKeyDerivation.salt.js";
9
9
  export * from "./cryptoKeyDerivation.validate.js";
10
- //# sourceMappingURL=index.d.ts.map
@@ -7,4 +7,3 @@ export * from "./cryptoKeyDerivation.core.js";
7
7
  export * from "./cryptoKeyDerivation.type.js";
8
8
  export * from "./cryptoKeyDerivation.salt.js";
9
9
  export * from "./cryptoKeyDerivation.validate.js";
10
- //# sourceMappingURL=index.js.map
@@ -25,4 +25,3 @@ export declare function decodePasswordHash(encoded: string): PasswordHashParamet
25
25
  * Encodes password hashing parameters into a portable string.
26
26
  */
27
27
  export declare function encodePasswordHash(parameters: PasswordHashParameters): string;
28
- //# sourceMappingURL=cryptoPassword.codec.d.ts.map
@@ -155,4 +155,3 @@ function decodePayload(payload) {
155
155
  }
156
156
  return { salt, hash };
157
157
  }
158
- //# sourceMappingURL=cryptoPassword.codec.js.map
@@ -2,15 +2,25 @@ import type { CryptoProvider } from "../cryptoProvider/index.js";
2
2
  import type { PasswordHashOptions, PasswordHashResult } from "./cryptoPassword.type.js";
3
3
  /**
4
4
  * Options for `hashPassword` including an optional provider override.
5
+ *
6
+ * `minLength` opts into a minimum password length; pass
7
+ * `PASSWORD_POLICY.MIN_LENGTH` (8) to apply the package policy. It is
8
+ * opt-in because `hashPassword` is also used to re-hash existing
9
+ * credentials that predate the policy; enforce it at registration and
10
+ * password change.
5
11
  */
6
12
  export interface HashPasswordOptions extends PasswordHashOptions {
7
13
  readonly provider?: CryptoProvider;
14
+ readonly minLength?: number;
8
15
  }
9
16
  /**
10
17
  * Hashes a password using scrypt.
11
18
  *
12
19
  * `saltBytes` and `keyBytes` are honoured, and the returned `salt`/`hash`
13
20
  * are exactly the values inside `encoded`.
21
+ *
22
+ * @throws {CryptoError} `CRYPTO_HASH` when the password is outside the
23
+ * length bounds (user-facing, 400) or a work factor is out of range.
14
24
  */
15
25
  export declare function hashPassword(password: string, options?: HashPasswordOptions): Promise<PasswordHashResult>;
16
26
  /**
@@ -21,4 +31,3 @@ export declare function hashPassword(password: string, options?: HashPasswordOpt
21
31
  * password also yields false.
22
32
  */
23
33
  export declare function verifyPassword(password: string, encoded: string, provider?: CryptoProvider): Promise<boolean>;
24
- //# sourceMappingURL=cryptoPassword.core.d.ts.map
@@ -2,17 +2,27 @@ import { getDefaultCryptoProvider } from "../cryptoProvider/cryptoProvider.defau
2
2
  import { assertPasswordHashingCapability, assertRandomCapability, } from "../cryptoProvider/cryptoProvider.capability.js";
3
3
  import { CryptoAlgorithm } from "../cryptoConstants/cryptoConstants.type.js";
4
4
  import { PASSWORD_HASH } from "../cryptoConstants/cryptoConstants.security.js";
5
- import { assertPassword, validateParameters, } from "./cryptoPassword.validate.js";
5
+ import { validateParameters } from "./cryptoPassword.validate.js";
6
6
  import { decodePasswordHash, PASSWORD_FORMAT_VERSION, } from "./cryptoPassword.codec.js";
7
- import { assertNewHashCost } from "./cryptoPassword.helper.js";
7
+ import { assertNewHashCost, assertPassword, } from "./cryptoPassword.helper.js";
8
+ import { PASSWORD_POLICY } from "../cryptoConstants/cryptoConstants.security.js";
8
9
  /**
9
10
  * Hashes a password using scrypt.
10
11
  *
11
12
  * `saltBytes` and `keyBytes` are honoured, and the returned `salt`/`hash`
12
13
  * are exactly the values inside `encoded`.
14
+ *
15
+ * @throws {CryptoError} `CRYPTO_HASH` when the password is outside the
16
+ * length bounds (user-facing, 400) or a work factor is out of range.
13
17
  */
14
18
  export async function hashPassword(password, options = {}) {
15
- assertPassword(password);
19
+ if (options.minLength !== undefined &&
20
+ (!Number.isInteger(options.minLength) ||
21
+ options.minLength < 1 ||
22
+ options.minLength > PASSWORD_POLICY.MAX_LENGTH)) {
23
+ throw new RangeError(`minLength must be an integer between 1 and ${PASSWORD_POLICY.MAX_LENGTH}.`);
24
+ }
25
+ assertPassword(password, options.minLength);
16
26
  const provider = options.provider ?? getDefaultCryptoProvider();
17
27
  assertPasswordHashingCapability(provider);
18
28
  assertRandomCapability(provider);
@@ -70,4 +80,3 @@ export async function verifyPassword(password, encoded, provider = getDefaultCry
70
80
  return false;
71
81
  }
72
82
  }
73
- //# sourceMappingURL=cryptoPassword.core.js.map
@@ -19,7 +19,15 @@ export declare function isValidPassword(password: string, minimumLength?: number
19
19
  * older stored hashes stay verifiable; minting a new hash with `cost: 2`
20
20
  * produced a hash that is effectively free to crack.
21
21
  *
22
- * @throws {RangeError} when `cost` is below the floor.
22
+ * @throws {CryptoError} `CRYPTO_HASH` when `cost` is below the floor.
23
23
  */
24
24
  export declare function assertNewHashCost(cost: number): void;
25
- //# sourceMappingURL=cryptoPassword.helper.d.ts.map
25
+ /**
26
+ * Asserts that a value is a password string within the length bounds:
27
+ * non-empty, at least `minLength` code units when one is given, and at
28
+ * most `PASSWORD_POLICY.MAX_LENGTH`.
29
+ *
30
+ * Length violations are user-facing `CryptoError`s (`CRYPTO_HASH`,
31
+ * `statusCode` 400, `expose` true); a non-string is a `TypeError`.
32
+ */
33
+ export declare function assertPassword(password: string, minLength?: number): void;
@@ -1,4 +1,5 @@
1
- import { PASSWORD_HASH } from "../cryptoConstants/cryptoConstants.security.js";
1
+ import { PASSWORD_HASH, PASSWORD_POLICY, } from "../cryptoConstants/cryptoConstants.security.js";
2
+ import { passwordHashError } from "../cryptoErrors/cryptoErrors.helper.js";
2
3
  import { decodePasswordHash } from "./cryptoPassword.codec.js";
3
4
  const PASSWORD_MINIMUM_DEFAULT_LENGTH = 8;
4
5
  /**
@@ -39,11 +40,32 @@ export function isValidPassword(password, minimumLength = PASSWORD_MINIMUM_DEFAU
39
40
  * older stored hashes stay verifiable; minting a new hash with `cost: 2`
40
41
  * produced a hash that is effectively free to crack.
41
42
  *
42
- * @throws {RangeError} when `cost` is below the floor.
43
+ * @throws {CryptoError} `CRYPTO_HASH` when `cost` is below the floor.
43
44
  */
44
45
  export function assertNewHashCost(cost) {
45
46
  if (typeof cost === "number" && cost < PASSWORD_HASH.SCRYPT.MIN_COST) {
46
- throw new RangeError(`scrypt cost for a new password hash must be at least ${PASSWORD_HASH.SCRYPT.MIN_COST}.`);
47
+ throw passwordHashError(`scrypt cost for a new password hash must be at least ${PASSWORD_HASH.SCRYPT.MIN_COST}.`);
48
+ }
49
+ }
50
+ /**
51
+ * Asserts that a value is a password string within the length bounds:
52
+ * non-empty, at least `minLength` code units when one is given, and at
53
+ * most `PASSWORD_POLICY.MAX_LENGTH`.
54
+ *
55
+ * Length violations are user-facing `CryptoError`s (`CRYPTO_HASH`,
56
+ * `statusCode` 400, `expose` true); a non-string is a `TypeError`.
57
+ */
58
+ export function assertPassword(password, minLength) {
59
+ if (typeof password !== "string") {
60
+ throw new TypeError("Password must be a string.");
61
+ }
62
+ if (password.length === 0) {
63
+ throw new TypeError("Password cannot be empty.");
64
+ }
65
+ if (minLength !== undefined && password.length < minLength) {
66
+ throw passwordHashError(`Password must be at least ${minLength} characters.`, { userFacing: true });
67
+ }
68
+ if (password.length > PASSWORD_POLICY.MAX_LENGTH) {
69
+ throw passwordHashError(`Password must not exceed ${PASSWORD_POLICY.MAX_LENGTH} characters.`, { userFacing: true });
47
70
  }
48
71
  }
49
- //# sourceMappingURL=cryptoPassword.helper.js.map
@@ -62,4 +62,3 @@ export interface Pbkdf2PasswordHashParameters {
62
62
  * Discriminate on `algorithm` to access algorithm-specific fields.
63
63
  */
64
64
  export type PasswordHashParameters = ScryptPasswordHashParameters | Pbkdf2PasswordHashParameters;
65
- //# sourceMappingURL=cryptoPassword.type.d.ts.map
@@ -1,2 +1 @@
1
1
  import { CryptoAlgorithm } from "../cryptoConstants/cryptoConstants.type.js";
2
- //# sourceMappingURL=cryptoPassword.type.js.map
@@ -2,6 +2,8 @@
2
2
  * Validates the structural constraints of scrypt password hashing
3
3
  * parameters, including upper bounds so that parameters read back from a
4
4
  * stored hash cannot force unbounded CPU or memory usage.
5
+ *
6
+ * @throws {CryptoError} `CRYPTO_HASH` for any parameter outside its bound.
5
7
  */
6
8
  export declare function validateParameters(parameters: {
7
9
  readonly saltBytes: number;
@@ -12,6 +14,8 @@ export declare function validateParameters(parameters: {
12
14
  }): void;
13
15
  /**
14
16
  * Validates PBKDF2 password hashing parameters, including upper bounds.
17
+ *
18
+ * @throws {CryptoError} `CRYPTO_HASH` for any parameter outside its bound.
15
19
  */
16
20
  export declare function validatePbkdf2Parameters(parameters: {
17
21
  readonly saltBytes: number;
@@ -26,9 +30,3 @@ export declare function parsePositiveInteger(value: string, name: string): numbe
26
30
  * Decodes a non-empty, canonical Base64URL string into bytes.
27
31
  */
28
32
  export declare function decodeBase64Url(value: string): Uint8Array;
29
- /**
30
- * Asserts that a value is a password string within the policy length
31
- * bounds (non-empty, at most `PASSWORD_POLICY.MAX_LENGTH` code units).
32
- */
33
- export declare function assertPassword(password: string): void;
34
- //# sourceMappingURL=cryptoPassword.validate.d.ts.map