@gjsify/crypto 0.3.13 → 0.3.14

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.
@@ -1,15 +1,14 @@
1
+ //#region src/constants.ts
1
2
  const constants = {
2
- // RSA padding modes (stubs for compatibility)
3
- RSA_PKCS1_PADDING: 1,
4
- RSA_NO_PADDING: 3,
5
- RSA_PKCS1_OAEP_PADDING: 4,
6
- RSA_PKCS1_PSS_PADDING: 6,
7
- // DiffieHellman error codes
8
- DH_CHECK_P_NOT_SAFE_PRIME: 2,
9
- DH_CHECK_P_NOT_PRIME: 1,
10
- DH_UNABLE_TO_CHECK_GENERATOR: 4,
11
- DH_NOT_SUITABLE_GENERATOR: 8
12
- };
13
- export {
14
- constants
3
+ RSA_PKCS1_PADDING: 1,
4
+ RSA_NO_PADDING: 3,
5
+ RSA_PKCS1_OAEP_PADDING: 4,
6
+ RSA_PKCS1_PSS_PADDING: 6,
7
+ DH_CHECK_P_NOT_SAFE_PRIME: 2,
8
+ DH_CHECK_P_NOT_PRIME: 1,
9
+ DH_UNABLE_TO_CHECK_GENERATOR: 4,
10
+ DH_NOT_SUITABLE_GENERATOR: 8
15
11
  };
12
+
13
+ //#endregion
14
+ export { constants };
@@ -1,47 +1,65 @@
1
1
  import { Buffer } from "node:buffer";
2
+
3
+ //#region src/crypto-utils.ts
4
+ /**
5
+ * Normalize a hash algorithm name to lowercase without hyphens.
6
+ * e.g. "SHA-256" → "sha256", "MD5" → "md5"
7
+ */
2
8
  function normalizeAlgorithm(algorithm) {
3
- return algorithm.toLowerCase().replace(/-/g, "");
9
+ return algorithm.toLowerCase().replace(/-/g, "");
4
10
  }
11
+ /** Hash digest output sizes in bytes. */
5
12
  const DIGEST_SIZES = {
6
- md5: 16,
7
- sha1: 20,
8
- sha256: 32,
9
- sha384: 48,
10
- sha512: 64
13
+ md5: 16,
14
+ sha1: 20,
15
+ sha256: 32,
16
+ sha384: 48,
17
+ sha512: 64
11
18
  };
19
+ /** Hash block sizes in bytes (used for HMAC key padding). */
12
20
  const BLOCK_SIZES = {
13
- md5: 64,
14
- sha1: 64,
15
- sha256: 64,
16
- sha384: 128,
17
- sha512: 128
21
+ md5: 64,
22
+ sha1: 64,
23
+ sha256: 64,
24
+ sha384: 128,
25
+ sha512: 128
18
26
  };
19
- const SUPPORTED_ALGORITHMS = /* @__PURE__ */ new Set(["md5", "sha1", "sha256", "sha384", "sha512"]);
27
+ /** Set of supported hash algorithm names (normalized). */
28
+ const SUPPORTED_ALGORITHMS = new Set([
29
+ "md5",
30
+ "sha1",
31
+ "sha256",
32
+ "sha384",
33
+ "sha512"
34
+ ]);
35
+ /**
36
+ * Get hash digest size in bytes for a given algorithm.
37
+ * Accepts unnormalized names (e.g. "SHA-256").
38
+ */
20
39
  function hashSize(algo) {
21
- const normalized = normalizeAlgorithm(algo);
22
- const size = DIGEST_SIZES[normalized];
23
- if (size === void 0) {
24
- throw new Error(`Unknown hash algorithm: ${algo}`);
25
- }
26
- return size;
40
+ const normalized = normalizeAlgorithm(algo);
41
+ const size = DIGEST_SIZES[normalized];
42
+ if (size === undefined) {
43
+ throw new Error(`Unknown hash algorithm: ${algo}`);
44
+ }
45
+ return size;
27
46
  }
47
+ /**
48
+ * Convert various input types to Buffer.
49
+ * Handles string, Buffer, Uint8Array, DataView, and ArrayBuffer.
50
+ */
28
51
  function toBuffer(input, encoding) {
29
- if (typeof input === "string") {
30
- return Buffer.from(input, encoding || "utf8");
31
- }
32
- if (input instanceof DataView) {
33
- return Buffer.from(input.buffer, input.byteOffset, input.byteLength);
34
- }
35
- if (input instanceof ArrayBuffer) {
36
- return Buffer.from(input);
37
- }
38
- return Buffer.from(input);
52
+ if (typeof input === "string") {
53
+ return Buffer.from(input, encoding || "utf8");
54
+ }
55
+ if (input instanceof DataView) {
56
+ return Buffer.from(input.buffer, input.byteOffset, input.byteLength);
57
+ }
58
+ if (input instanceof ArrayBuffer) {
59
+ return Buffer.from(input);
60
+ }
61
+ return Buffer.from(input);
39
62
  }
40
- export {
41
- BLOCK_SIZES,
42
- DIGEST_SIZES,
43
- SUPPORTED_ALGORITHMS,
44
- hashSize,
45
- normalizeAlgorithm,
46
- toBuffer
47
- };
63
+
64
+ //#endregion
65
+ export { BLOCK_SIZES, DIGEST_SIZES, SUPPORTED_ALGORITHMS, hashSize, normalizeAlgorithm, toBuffer };