@signature-kit/crypto 0.2.0 → 0.2.2

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 (42) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/README.md +28 -11
  3. package/dist/base64.d.ts +1 -1
  4. package/dist/base64.d.ts.map +1 -1
  5. package/dist/base64.js +15 -14
  6. package/dist/config.d.ts +31 -7
  7. package/dist/config.d.ts.map +1 -1
  8. package/dist/config.js +42 -27
  9. package/dist/pem.d.ts +1 -1
  10. package/dist/pem.d.ts.map +1 -1
  11. package/dist/pem.js +14 -8
  12. package/dist/pkcs12.d.ts +2 -10
  13. package/dist/pkcs12.d.ts.map +1 -1
  14. package/dist/pkcs12.js +380 -125
  15. package/dist/primitives/aes.d.ts +1 -14
  16. package/dist/primitives/aes.d.ts.map +1 -1
  17. package/dist/primitives/aes.js +8 -47
  18. package/dist/primitives/des.d.ts +1 -20
  19. package/dist/primitives/des.d.ts.map +1 -1
  20. package/dist/primitives/des.js +6 -67
  21. package/dist/primitives/hmac.d.ts +0 -30
  22. package/dist/primitives/hmac.d.ts.map +1 -1
  23. package/dist/primitives/hmac.js +4 -46
  24. package/dist/primitives/padding.d.ts +1 -8
  25. package/dist/primitives/padding.d.ts.map +1 -1
  26. package/dist/primitives/padding.js +2 -11
  27. package/dist/primitives/pbkdf2.d.ts +1 -17
  28. package/dist/primitives/pbkdf2.d.ts.map +1 -1
  29. package/dist/primitives/pbkdf2.js +8 -30
  30. package/dist/primitives/rc2.d.ts +1 -20
  31. package/dist/primitives/rc2.d.ts.map +1 -1
  32. package/dist/primitives/rc2.js +11 -69
  33. package/dist/primitives/sha1.d.ts +0 -13
  34. package/dist/primitives/sha1.d.ts.map +1 -1
  35. package/dist/primitives/sha1.js +1 -28
  36. package/dist/primitives/sha256.d.ts +0 -26
  37. package/dist/primitives/sha256.d.ts.map +1 -1
  38. package/dist/primitives/sha256.js +0 -34
  39. package/dist/primitives/sha512.d.ts +0 -19
  40. package/dist/primitives/sha512.d.ts.map +1 -1
  41. package/dist/primitives/sha512.js +1 -32
  42. package/package.json +8 -6
@@ -1,27 +1,14 @@
1
- /**
2
- * PBKDF2 pure TypeScript implementation (RFC 2898 / PKCS#5).
3
- *
4
- * Uses HMAC with any supported hash as the pseudo-random function.
5
- * Zero runtime dependencies. Works in any JS environment.
6
- */
7
- import { createHmac, hmac } from "./hmac";
8
- /**
9
- * Derive a key of `dkLen` bytes from a password and salt using PBKDF2.
10
- *
11
- * @param prf - Hash algorithm for HMAC PRF: 'sha1' | 'sha256' | 'sha384' | 'sha512'
12
- * @param password - Password as raw Uint8Array
13
- * @param salt - Cryptographic salt
14
- * @param iterations - Iteration count (must be >= 1)
15
- * @param dkLen - Desired derived key length in bytes
16
- * @returns Derived key as Uint8Array
17
- */
1
+ import { createHmac, hmac } from "./hmac.js";
18
2
  export function pbkdf2(prf, password, salt, iterations, dkLen) {
19
- // Precompute HMAC context for this key — avoids re-deriving ipad/opad per iteration
3
+ if (!Number.isSafeInteger(iterations) ||
4
+ iterations < 1 ||
5
+ !Number.isSafeInteger(dkLen) ||
6
+ dkLen < 1 ||
7
+ dkLen > 0x7fffffff) {
8
+ return new Uint8Array(0);
9
+ }
20
10
  const hmacCtx = createHmac(prf, password);
21
- // hLen = output length of PRF (HMAC hash output)
22
- // Compute once using the standard hmac for length detection
23
11
  const hLen = hmac(prf, password, new Uint8Array(0)).length;
24
- // Number of blocks needed
25
12
  const blockCount = Math.ceil(dkLen / hLen);
26
13
  const dk = new Uint8Array(dkLen);
27
14
  for (let i = 1; i <= blockCount; i++) {
@@ -32,26 +19,17 @@ export function pbkdf2(prf, password, salt, iterations, dkLen) {
32
19
  }
33
20
  return dk;
34
21
  }
35
- /**
36
- * Compute one PRF block: F(Password, Salt, c, i)
37
- * = U1 XOR U2 XOR ... XOR Uc
38
- * where U1 = PRF(Password, Salt || INT(i))
39
- * Uk = PRF(Password, U_{k-1})
40
- */
41
22
  function f(hmacCtx, salt, iterations, blockIndex, hLen) {
42
- // Build Salt || INT(i) (4-byte big-endian block index)
43
23
  const saltWithIndex = new Uint8Array(salt.length + 4);
44
24
  saltWithIndex.set(salt);
45
25
  saltWithIndex[salt.length] = (blockIndex >>> 24) & 0xff;
46
26
  saltWithIndex[salt.length + 1] = (blockIndex >>> 16) & 0xff;
47
27
  saltWithIndex[salt.length + 2] = (blockIndex >>> 8) & 0xff;
48
28
  saltWithIndex[salt.length + 3] = blockIndex & 0xff;
49
- // U1
50
29
  let u = hmacCtx.compute(saltWithIndex);
51
30
  const result = new Uint8Array(hLen);
52
31
  for (let j = 0; j < hLen; j++)
53
32
  result[j] = u[j];
54
- // U2 ... Uc
55
33
  for (let c = 1; c < iterations; c++) {
56
34
  u = hmacCtx.compute(u);
57
35
  for (let j = 0; j < hLen; j++)
@@ -1,23 +1,4 @@
1
- /**
2
- * RC2-CBC pure TypeScript implementation (RFC 2268).
3
- *
4
- * Supports 40-bit (effectiveBits=40) and 128-bit (effectiveBits=128) effective
5
- * key lengths, both in CBC mode with PKCS#7 unpadding.
6
- *
7
- * RC2 is a legacy cipher. It is used here exclusively for PKCS#12
8
- * compatibility (pbeWithSHAAnd40BitRC2-CBC / pbeWithSHAAnd128BitRC2-CBC).
9
- *
10
- * Zero runtime dependencies. Works in any JS environment.
11
- */
12
1
  import { Effect } from "effect";
13
- import { CryptoError } from "../config";
14
- /**
15
- * Decrypt data encrypted with RC2-CBC + PKCS#7 unpadding.
16
- *
17
- * @param key - Raw key bytes
18
- * @param effectiveBits - Effective key length in bits (40 or 128)
19
- * @param iv - 8 bytes
20
- * @param ciphertext - Must be a multiple of 8 bytes
21
- */
2
+ import { CryptoError } from "../config.js";
22
3
  export declare const rc2CbcDecrypt: (key: Uint8Array, effectiveBits: number, iv: Uint8Array, ciphertext: Uint8Array) => Effect.Effect<Uint8Array, CryptoError>;
23
4
  //# sourceMappingURL=rc2.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"rc2.d.ts","sourceRoot":"","sources":["../../src/primitives/rc2.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,WAAW,EAA8C,MAAM,WAAW,CAAC;AAoLpF;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa,GACxB,KAAK,UAAU,EACf,eAAe,MAAM,EACrB,IAAI,UAAU,EACd,YAAY,UAAU,KACrB,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,WAAW,CAgCvC,CAAC"}
1
+ {"version":3,"file":"rc2.d.ts","sourceRoot":"","sources":["../../src/primitives/rc2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,WAAW,EAA8C,MAAM,cAAc,CAAC;AAqIvF,eAAO,MAAM,aAAa,QACnB,UAAU,iBACA,MAAM,MACjB,UAAU,cACF,UAAU,KACrB,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,WAAW,CA0CvC,CAAC"}
@@ -1,18 +1,6 @@
1
- /**
2
- * RC2-CBC pure TypeScript implementation (RFC 2268).
3
- *
4
- * Supports 40-bit (effectiveBits=40) and 128-bit (effectiveBits=128) effective
5
- * key lengths, both in CBC mode with PKCS#7 unpadding.
6
- *
7
- * RC2 is a legacy cipher. It is used here exclusively for PKCS#12
8
- * compatibility (pbeWithSHAAnd40BitRC2-CBC / pbeWithSHAAnd128BitRC2-CBC).
9
- *
10
- * Zero runtime dependencies. Works in any JS environment.
11
- */
12
1
  import { Effect } from "effect";
13
- import { CryptoError, CryptoErrorCodeValue, CryptoOperationValue } from "../config";
14
- import { removePkcs7Padding } from "./padding";
15
- // RC2 permutation table (RFC 2268, Section 2)
2
+ import { CryptoError, CryptoErrorCodeValue, CryptoOperationValue } from "../config.js";
3
+ import { removePkcs7Padding } from "./padding.js";
16
4
  const PITABLE = new Uint8Array([
17
5
  0xd9, 0x78, 0xf9, 0xc4, 0x19, 0xdd, 0xb5, 0xed, 0x28, 0xe9, 0xfd, 0x79, 0x4a, 0xa0, 0xd8, 0x9d,
18
6
  0xc6, 0x7e, 0x37, 0x83, 0x2b, 0x76, 0x53, 0x8e, 0x62, 0x4c, 0x64, 0x88, 0x44, 0x8b, 0xfb, 0xa2,
@@ -31,69 +19,37 @@ const PITABLE = new Uint8Array([
31
19
  0x0d, 0x38, 0x34, 0x1b, 0xab, 0x33, 0xff, 0xb0, 0xbb, 0x48, 0x0c, 0x5f, 0xb9, 0xb1, 0xcd, 0x2e,
32
20
  0xc5, 0xf3, 0xdb, 0x47, 0xe5, 0xa5, 0x9c, 0x77, 0x0a, 0xa6, 0x20, 0x68, 0xfe, 0x7f, 0xc1, 0xad,
33
21
  ]);
34
- /**
35
- * RC2 key expansion.
36
- *
37
- * @param key - Raw key bytes (1..128 bytes)
38
- * @param pkeySz - Effective key length in bits (e.g. 40 or 128)
39
- * @returns 64 16-bit words as a plain number[]
40
- */
41
22
  function rc2ExpandKey(key, pkeySz) {
42
23
  const L = new Uint8Array(128);
43
24
  L.set(key);
44
25
  const t = key.length;
45
26
  const t8 = Math.ceil(pkeySz / 8);
46
27
  const tm = 0xff >> (8 * t8 - pkeySz);
47
- // Step 2: expand
48
28
  for (let i = t; i < 128; i++) {
49
29
  L[i] = PITABLE[(L[i - 1] + L[i - t]) & 0xff];
50
30
  }
51
- // Step 3: limit effective bits
52
31
  L[128 - t8] = PITABLE[L[128 - t8] & tm];
53
- // Step 4: reduce
54
32
  for (let i = 127 - t8; i >= 0; i--) {
55
33
  L[i] = PITABLE[L[i + 1] ^ L[i + t8]];
56
34
  }
57
- // Convert to 64 16-bit words (little-endian)
58
- const K = new Array(64);
35
+ const K = [];
59
36
  for (let i = 0; i < 64; i++) {
60
37
  K[i] = L[i * 2] | (L[i * 2 + 1] << 8);
61
38
  }
62
39
  return K;
63
40
  }
64
- /**
65
- * Decrypt a single 8-byte RC2 block in place.
66
- * Implements the inverse of the 16-round RC2 encryption described in RFC 2268.
67
- *
68
- * Forward mix at key index j (each round processes R0..R3):
69
- * R0 += K[j] + (R3 & R2) + (~R3 & R1); R0 = R0 <<< 1
70
- * R1 += K[j+1] + (R0 & R3) + (~R0 & R2); R1 = R1 <<< 2
71
- * R2 += K[j+2] + (R1 & R0) + (~R1 & R3); R2 = R2 <<< 3
72
- * R3 += K[j+3] + (R2 & R1) + (~R2 & R0); R3 = R3 <<< 5
73
- *
74
- * Inverse mix at key index j (reverse order, unrotate THEN subtract):
75
- * R3 = R3 >>> 5; R3 -= K[j+3] + (R2 & R1) + (~R2 & R0)
76
- * R2 = R2 >>> 3; R2 -= K[j+2] + (R1 & R0) + (~R1 & R3)
77
- * R1 = R1 >>> 2; R1 -= K[j+1] + (R0 & R3) + (~R0 & R2)
78
- * R0 = R0 >>> 1; R0 -= K[j] + (R3 & R2) + (~R3 & R1)
79
- */
80
41
  function rc2DecryptBlock(block, K) {
81
- // Load 4 16-bit words (little-endian)
82
42
  let r0 = (block[0] | (block[1] << 8)) & 0xffff;
83
43
  let r1 = (block[2] | (block[3] << 8)) & 0xffff;
84
44
  let r2 = (block[4] | (block[5] << 8)) & 0xffff;
85
45
  let r3 = (block[6] | (block[7] << 8)) & 0xffff;
86
46
  function invMix(j) {
87
- // Right rotate R3 by 5, then subtract
88
47
  r3 = ((r3 >>> 5) | (r3 << 11)) & 0xffff;
89
48
  r3 = (r3 - (K[j + 3] + (r2 & r1) + (~r2 & r0))) & 0xffff;
90
- // Right rotate R2 by 3, then subtract
91
49
  r2 = ((r2 >>> 3) | (r2 << 13)) & 0xffff;
92
50
  r2 = (r2 - (K[j + 2] + (r1 & r0) + (~r1 & r3))) & 0xffff;
93
- // Right rotate R1 by 2, then subtract
94
51
  r1 = ((r1 >>> 2) | (r1 << 14)) & 0xffff;
95
52
  r1 = (r1 - (K[j + 1] + (r0 & r3) + (~r0 & r2))) & 0xffff;
96
- // Right rotate R0 by 1, then subtract
97
53
  r0 = ((r0 >>> 1) | (r0 << 15)) & 0xffff;
98
54
  r0 = (r0 - (K[j] + (r3 & r2) + (~r3 & r1))) & 0xffff;
99
55
  }
@@ -103,9 +59,6 @@ function rc2DecryptBlock(block, K) {
103
59
  r1 = (r1 - K[r0 & 63]) & 0xffff;
104
60
  r0 = (r0 - K[r3 & 63]) & 0xffff;
105
61
  }
106
- // Reverse of forward: inv-mix×5, inv-mash, inv-mix×6, inv-mash, inv-mix×5
107
- // Forward key index sequence: 0,4,8,12,16 | (mash) | 20,24,28,32,36,40 | (mash) | 44,48,52,56,60
108
- // Inverse: 60,56,52,48,44 | (invMash) | 40,36,32,28,24,20 | (invMash) | 16,12,8,4,0
109
62
  invMix(60);
110
63
  invMix(56);
111
64
  invMix(52);
@@ -124,7 +77,6 @@ function rc2DecryptBlock(block, K) {
124
77
  invMix(8);
125
78
  invMix(4);
126
79
  invMix(0);
127
- // Store back (little-endian)
128
80
  block[0] = r0 & 0xff;
129
81
  block[1] = (r0 >>> 8) & 0xff;
130
82
  block[2] = r1 & 0xff;
@@ -134,23 +86,14 @@ function rc2DecryptBlock(block, K) {
134
86
  block[6] = r3 & 0xff;
135
87
  block[7] = (r3 >>> 8) & 0xff;
136
88
  }
137
- /**
138
- * Pure RC2-CBC decryption. Returns the still-padded plaintext. Never throws.
139
- *
140
- * @param key - Raw key bytes (1..128 bytes)
141
- * @param effectiveBits - Effective key length in bits (40 or 128)
142
- * @param iv - 8 bytes
143
- * @param ciphertext - Must be a multiple of 8 bytes
144
- */
145
89
  function rc2CbcDecryptRaw(key, effectiveBits, iv, ciphertext) {
146
90
  const K = rc2ExpandKey(key, effectiveBits);
147
91
  const plaintext = new Uint8Array(ciphertext.length);
148
92
  let prev = new Uint8Array(iv);
149
93
  for (let i = 0; i < ciphertext.length; i += 8) {
150
94
  const block = new Uint8Array(ciphertext.subarray(i, i + 8));
151
- const ctBlock = new Uint8Array(block); // save for CBC chain
95
+ const ctBlock = new Uint8Array(block);
152
96
  rc2DecryptBlock(block, K);
153
- // CBC: XOR with previous ciphertext block
154
97
  for (let j = 0; j < 8; j++) {
155
98
  plaintext[i + j] = block[j] ^ prev[j];
156
99
  }
@@ -158,15 +101,14 @@ function rc2CbcDecryptRaw(key, effectiveBits, iv, ciphertext) {
158
101
  }
159
102
  return plaintext;
160
103
  }
161
- /**
162
- * Decrypt data encrypted with RC2-CBC + PKCS#7 unpadding.
163
- *
164
- * @param key - Raw key bytes
165
- * @param effectiveBits - Effective key length in bits (40 or 128)
166
- * @param iv - 8 bytes
167
- * @param ciphertext - Must be a multiple of 8 bytes
168
- */
169
104
  export const rc2CbcDecrypt = (key, effectiveBits, iv, ciphertext) => {
105
+ if (!Number.isInteger(effectiveBits) || effectiveBits < 1 || effectiveBits > 1024) {
106
+ return Effect.fail(new CryptoError({
107
+ code: CryptoErrorCodeValue.cipherError,
108
+ reason: "RC2: effective bits must be an integer in the range 1..1024.",
109
+ operation: CryptoOperationValue.cipherRc2,
110
+ }));
111
+ }
170
112
  if (iv.length !== 8) {
171
113
  return Effect.fail(new CryptoError({
172
114
  code: CryptoErrorCodeValue.cipherError,
@@ -1,15 +1,2 @@
1
- /**
2
- * SHA-1 pure TypeScript implementation (FIPS 180-4).
3
- *
4
- * Zero runtime dependencies. Works in any JS environment (browser, Node.js,
5
- * Deno, Cloudflare Workers, Bun).
6
- *
7
- * ⚠️ SHA-1 is cryptographically broken for collision resistance. It is used
8
- * here exclusively for PKCS#12 legacy MAC/KDF compatibility.
9
- */
10
- /**
11
- * Compute SHA-1 digest of `data`.
12
- * Returns a 20-byte Uint8Array.
13
- */
14
1
  export declare function sha1(data: Uint8Array): Uint8Array;
15
2
  //# sourceMappingURL=sha1.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sha1.d.ts","sourceRoot":"","sources":["../../src/primitives/sha1.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAqBH;;;GAGG;AACH,wBAAgB,IAAI,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CAqFjD"}
1
+ {"version":3,"file":"sha1.d.ts","sourceRoot":"","sources":["../../src/primitives/sha1.ts"],"names":[],"mappings":"AAgBA,wBAAgB,IAAI,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CA0EjD"}
@@ -1,57 +1,31 @@
1
- /**
2
- * SHA-1 pure TypeScript implementation (FIPS 180-4).
3
- *
4
- * Zero runtime dependencies. Works in any JS environment (browser, Node.js,
5
- * Deno, Cloudflare Workers, Bun).
6
- *
7
- * ⚠️ SHA-1 is cryptographically broken for collision resistance. It is used
8
- * here exclusively for PKCS#12 legacy MAC/KDF compatibility.
9
- */
10
- // SHA-1 initial hash values (first 32 bits of fractional parts of sqrt(2..5))
11
1
  const H0 = 0x67452301;
12
2
  const H1 = 0xefcdab89;
13
3
  const H2 = 0x98badcfe;
14
4
  const H3 = 0x10325476;
15
5
  const H4 = 0xc3d2e1f0;
16
- /** Rotate left 32-bit integer */
17
6
  function rotl32(x, n) {
18
7
  return ((x << n) | (x >>> (32 - n))) >>> 0;
19
8
  }
20
- /** Add two 32-bit unsigned integers (mod 2^32) */
21
9
  function add32(...nums) {
22
10
  let r = 0;
23
11
  for (const n of nums)
24
12
  r = (r + n) >>> 0;
25
13
  return r;
26
14
  }
27
- /**
28
- * Compute SHA-1 digest of `data`.
29
- * Returns a 20-byte Uint8Array.
30
- */
31
15
  export function sha1(data) {
32
- // ---- Pre-processing: padding ----
33
- const bitLen = data.length * 8;
34
- // Message length in bytes after padding: must be ≡ 56 (mod 64)
35
16
  const padLen = data.length % 64 < 56 ? 56 - (data.length % 64) : 120 - (data.length % 64);
36
17
  const totalLen = data.length + padLen + 8;
37
18
  const msg = new Uint8Array(totalLen);
38
19
  msg.set(data);
39
20
  msg[data.length] = 0x80;
40
- // Append bit length as big-endian 64-bit integer (only lower 32 bits in practice)
41
21
  const view = new DataView(msg.buffer, msg.byteOffset, msg.byteLength);
42
- view.setUint32(totalLen - 8, Math.floor(bitLen / 0x100000000), true);
43
- view.setUint32(totalLen - 4, bitLen >>> 0, false);
44
- // Actually SHA-1 uses big-endian for length
45
- // Re-do: write the 64-bit length big-endian
46
- const bitLenHi = Math.floor(data.length / 0x20000000) | 0; // data.length * 8, upper 32 bits
22
+ const bitLenHi = Math.floor(data.length / 0x20000000) | 0;
47
23
  const bitLenLo = (data.length * 8) >>> 0;
48
24
  view.setUint32(totalLen - 8, bitLenHi, false);
49
25
  view.setUint32(totalLen - 4, bitLenLo, false);
50
- // ---- Processing ----
51
26
  let h0 = H0, h1 = H1, h2 = H2, h3 = H3, h4 = H4;
52
27
  const w = new Uint32Array(80);
53
28
  for (let i = 0; i < totalLen; i += 64) {
54
- // Prepare message schedule
55
29
  for (let t = 0; t < 16; t++) {
56
30
  w[t] = view.getUint32(i + t * 4, false);
57
31
  }
@@ -90,7 +64,6 @@ export function sha1(data) {
90
64
  h3 = add32(h3, d);
91
65
  h4 = add32(h4, e);
92
66
  }
93
- // ---- Produce digest ----
94
67
  const digest = new Uint8Array(20);
95
68
  const dv = new DataView(digest.buffer);
96
69
  dv.setUint32(0, h0, false);
@@ -1,30 +1,4 @@
1
- /**
2
- * SHA-256 pure TypeScript implementation (FIPS 180-4).
3
- *
4
- * Zero runtime dependencies. Works in any JS environment.
5
- */
6
- /**
7
- * Compute SHA-256 digest of `data`.
8
- * Returns a 32-byte Uint8Array.
9
- */
10
1
  export declare function sha256(data: Uint8Array): Uint8Array;
11
- /**
12
- * Compute SHA-256 with a pre-processed initial state.
13
- *
14
- * This is used for HMAC key-schedule precomputation: the first 64-byte block
15
- * (ipad or opad XOR key) is processed once and the resulting state is reused
16
- * for every HMAC call with the same key.
17
- *
18
- * @param initState - 8-element Uint32Array with the pre-processed hash state
19
- * @param data - The message data (appended after the already-processed block)
20
- * @param prefixLen - Number of bytes already processed (for length encoding), typically 64
21
- * @returns 32-byte SHA-256 digest
22
- */
23
2
  export declare function sha256WithState(initState: Uint32Array, data: Uint8Array, prefixLen: number): Uint8Array;
24
- /**
25
- * Process a single full 64-byte block from `blockBytes` into a copy of INIT_H.
26
- * Returns the resulting 8-element hash state.
27
- * Used to precompute HMAC ipad/opad half-states.
28
- */
29
3
  export declare function sha256ProcessBlock(blockBytes: Uint8Array): Uint32Array;
30
4
  //# sourceMappingURL=sha256.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sha256.d.ts","sourceRoot":"","sources":["../../src/primitives/sha256.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAwEH;;;GAGG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CA8BnD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAC7B,SAAS,EAAE,WAAW,EACtB,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,MAAM,GAChB,UAAU,CA8BZ;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,UAAU,GAAG,WAAW,CAKtE"}
1
+ {"version":3,"file":"sha256.d.ts","sourceRoot":"","sources":["../../src/primitives/sha256.ts"],"names":[],"mappings":"AAmEA,wBAAgB,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CA2BnD;AAED,wBAAgB,eAAe,CAC7B,SAAS,EAAE,WAAW,EACtB,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,MAAM,GAChB,UAAU,CA4BZ;AAED,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,UAAU,GAAG,WAAW,CAKtE"}
@@ -1,13 +1,6 @@
1
- /**
2
- * SHA-256 pure TypeScript implementation (FIPS 180-4).
3
- *
4
- * Zero runtime dependencies. Works in any JS environment.
5
- */
6
- // SHA-256 initial hash values (first 32 bits of fractional parts of sqrt of first 8 primes)
7
1
  const INIT_H = new Uint32Array([
8
2
  0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
9
3
  ]);
10
- // SHA-256 round constants (first 32 bits of fractional parts of cbrt of first 64 primes)
11
4
  const K = new Uint32Array([
12
5
  0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
13
6
  0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
@@ -21,7 +14,6 @@ const K = new Uint32Array([
21
14
  function rotr32(x, n) {
22
15
  return ((x >>> n) | (x << (32 - n))) >>> 0;
23
16
  }
24
- /** Process a single 64-byte block into hash state `h` (mutates in place). */
25
17
  function processBlock(h, view, offset) {
26
18
  const w = new Uint32Array(64);
27
19
  for (let t = 0; t < 16; t++) {
@@ -65,12 +57,7 @@ function processBlock(h, view, offset) {
65
57
  h[6] = (h[6] + g) >>> 0;
66
58
  h[7] = (h[7] + hh) >>> 0;
67
59
  }
68
- /**
69
- * Compute SHA-256 digest of `data`.
70
- * Returns a 32-byte Uint8Array.
71
- */
72
60
  export function sha256(data) {
73
- // ---- Pre-processing: padding ----
74
61
  const bitLen = data.length * 8;
75
62
  const padLen = data.length % 64 < 56 ? 56 - (data.length % 64) : 120 - (data.length % 64);
76
63
  const totalLen = data.length + padLen + 8;
@@ -82,12 +69,10 @@ export function sha256(data) {
82
69
  const bitLenLo = bitLen >>> 0;
83
70
  view.setUint32(totalLen - 8, bitLenHi, false);
84
71
  view.setUint32(totalLen - 4, bitLenLo, false);
85
- // ---- Processing ----
86
72
  const h = new Uint32Array(INIT_H);
87
73
  for (let i = 0; i < totalLen; i += 64) {
88
74
  processBlock(h, view, i);
89
75
  }
90
- // ---- Produce digest ----
91
76
  const digest = new Uint8Array(32);
92
77
  const dv = new DataView(digest.buffer);
93
78
  for (let i = 0; i < 8; i++) {
@@ -95,20 +80,7 @@ export function sha256(data) {
95
80
  }
96
81
  return digest;
97
82
  }
98
- /**
99
- * Compute SHA-256 with a pre-processed initial state.
100
- *
101
- * This is used for HMAC key-schedule precomputation: the first 64-byte block
102
- * (ipad or opad XOR key) is processed once and the resulting state is reused
103
- * for every HMAC call with the same key.
104
- *
105
- * @param initState - 8-element Uint32Array with the pre-processed hash state
106
- * @param data - The message data (appended after the already-processed block)
107
- * @param prefixLen - Number of bytes already processed (for length encoding), typically 64
108
- * @returns 32-byte SHA-256 digest
109
- */
110
83
  export function sha256WithState(initState, data, prefixLen) {
111
- // Total message length = prefixLen (pre-processed) + data.length
112
84
  const totalDataLen = prefixLen + data.length;
113
85
  const bitLen = totalDataLen * 8;
114
86
  const padLen = totalDataLen % 64 < 56 ? 56 - (totalDataLen % 64) : 120 - (totalDataLen % 64);
@@ -121,7 +93,6 @@ export function sha256WithState(initState, data, prefixLen) {
121
93
  const bitLenLo = bitLen >>> 0;
122
94
  view.setUint32(totalLen - 8, bitLenHi, false);
123
95
  view.setUint32(totalLen - 4, bitLenLo, false);
124
- // Start from the pre-processed state (copy so we don't mutate)
125
96
  const h = new Uint32Array(initState);
126
97
  for (let i = 0; i < totalLen; i += 64) {
127
98
  processBlock(h, view, i);
@@ -133,11 +104,6 @@ export function sha256WithState(initState, data, prefixLen) {
133
104
  }
134
105
  return digest;
135
106
  }
136
- /**
137
- * Process a single full 64-byte block from `blockBytes` into a copy of INIT_H.
138
- * Returns the resulting 8-element hash state.
139
- * Used to precompute HMAC ipad/opad half-states.
140
- */
141
107
  export function sha256ProcessBlock(blockBytes) {
142
108
  const h = new Uint32Array(INIT_H);
143
109
  const view = new DataView(blockBytes.buffer, blockBytes.byteOffset, blockBytes.byteLength);
@@ -1,22 +1,3 @@
1
- /**
2
- * SHA-512 and SHA-384 pure TypeScript implementation (FIPS 180-4).
3
- *
4
- * SHA-384 uses the same core as SHA-512 with different initial values and
5
- * truncates the output to 48 bytes.
6
- *
7
- * Zero runtime dependencies. Works in any JS environment.
8
- *
9
- * Note: 64-bit arithmetic is emulated with two 32-bit halves (hi/lo) since
10
- * JavaScript integers are 53-bit. No BigInt used here to keep it fast.
11
- */
12
- /**
13
- * Compute SHA-512 digest of `data`.
14
- * Returns a 64-byte Uint8Array.
15
- */
16
1
  export declare function sha512(data: Uint8Array): Uint8Array;
17
- /**
18
- * Compute SHA-384 digest of `data`.
19
- * Returns a 48-byte Uint8Array.
20
- */
21
2
  export declare function sha384(data: Uint8Array): Uint8Array;
22
3
  //# sourceMappingURL=sha512.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sha512.d.ts","sourceRoot":"","sources":["../../src/primitives/sha512.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAoNH;;;GAGG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CAEnD;AAED;;;GAGG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CAEnD"}
1
+ {"version":3,"file":"sha512.d.ts","sourceRoot":"","sources":["../../src/primitives/sha512.ts"],"names":[],"mappings":"AAsMA,wBAAgB,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CAEnD;AAED,wBAAgB,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CAEnD"}
@@ -1,16 +1,3 @@
1
- /**
2
- * SHA-512 and SHA-384 pure TypeScript implementation (FIPS 180-4).
3
- *
4
- * SHA-384 uses the same core as SHA-512 with different initial values and
5
- * truncates the output to 48 bytes.
6
- *
7
- * Zero runtime dependencies. Works in any JS environment.
8
- *
9
- * Note: 64-bit arithmetic is emulated with two 32-bit halves (hi/lo) since
10
- * JavaScript integers are 53-bit. No BigInt used here to keep it fast.
11
- */
12
- // SHA-512 round constants K (first 64 bits of cbrt of first 80 primes)
13
- // Stored as pairs [hi, lo] of 32-bit values (index = t*2 for hi, t*2+1 for lo)
14
1
  const K512 = new Uint32Array([
15
2
  0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd, 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
16
3
  0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019, 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
@@ -33,12 +20,10 @@ const K512 = new Uint32Array([
33
20
  0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493, 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
34
21
  0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a, 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817,
35
22
  ]);
36
- // SHA-512 initial hash values
37
23
  const INIT_512 = new Uint32Array([
38
24
  0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa73b, 0x3c6ef372, 0xfe94f82b, 0xa54ff53a, 0x5f1d36f1,
39
25
  0x510e527f, 0xade682d1, 0x9b05688c, 0x2b3e6c1f, 0x1f83d9ab, 0xfb41bd6b, 0x5be0cd19, 0x137e2179,
40
26
  ]);
41
- // SHA-384 initial hash values
42
27
  const INIT_384 = new Uint32Array([
43
28
  0xcbbb9d5d, 0xc1059ed8, 0x629a292a, 0x367cd507, 0x9159015a, 0x3070dd17, 0x152fecd8, 0xf70e5939,
44
29
  0x67332667, 0xffc00b31, 0x8eb44a87, 0x68581511, 0xdb0c2e0d, 0x64f98fa7, 0x47b5481d, 0xbefa4fa4,
@@ -77,19 +62,17 @@ function sha512Core(data, initH) {
77
62
  view.setUint32(totalLen - 4, bitLenLo, false);
78
63
  const h = new Uint32Array(16);
79
64
  h.set(initH);
80
- const W = new Uint32Array(160); // 80 × 64-bit
65
+ const W = new Uint32Array(160);
81
66
  for (let i = 0; i < totalLen; i += 128) {
82
67
  for (let t = 0; t < 16; t++) {
83
68
  W[t * 2] = view.getUint32(i + t * 8, false);
84
69
  W[t * 2 + 1] = view.getUint32(i + t * 8 + 4, false);
85
70
  }
86
71
  for (let t = 16; t < 80; t++) {
87
- // σ1(W[t-2]) = ROTR19 ^ ROTR61 ^ SHR6
88
72
  const [r19h, r19l] = rotr64(W[(t - 2) * 2], W[(t - 2) * 2 + 1], 19);
89
73
  const [r61h, r61l] = rotr64(W[(t - 2) * 2], W[(t - 2) * 2 + 1], 61);
90
74
  const [s6h, s6l] = shr64(W[(t - 2) * 2], W[(t - 2) * 2 + 1], 6);
91
75
  const [s1h, s1l] = xor3_64(r19h, r19l, r61h, r61l, s6h, s6l);
92
- // σ0(W[t-15]) = ROTR1 ^ ROTR8 ^ SHR7
93
76
  const [r1h, r1l] = rotr64(W[(t - 15) * 2], W[(t - 15) * 2 + 1], 1);
94
77
  const [r8h, r8l] = rotr64(W[(t - 15) * 2], W[(t - 15) * 2 + 1], 8);
95
78
  const [s7h, s7l] = shr64(W[(t - 15) * 2], W[(t - 15) * 2 + 1], 7);
@@ -109,28 +92,22 @@ function sha512Core(data, initH) {
109
92
  let gh = h[12], gl = h[13];
110
93
  let hh = h[14], hl = h[15];
111
94
  for (let t = 0; t < 80; t++) {
112
- // Σ1(e) = ROTR14 ^ ROTR18 ^ ROTR41
113
95
  const [r14h, r14l] = rotr64(eh, el, 14);
114
96
  const [r18h, r18l] = rotr64(eh, el, 18);
115
97
  const [r41h, r41l] = rotr64(eh, el, 41);
116
98
  const [S1h, S1l] = xor3_64(r14h, r14l, r18h, r18l, r41h, r41l);
117
- // Ch(e, f, g) = (e & f) ^ (~e & g)
118
99
  const chH = ((eh & fh) ^ ((~eh >>> 0) & gh)) >>> 0;
119
100
  const chL = ((el & fl) ^ ((~el >>> 0) & gl)) >>> 0;
120
- // T1 = h + Σ1(e) + Ch(e,f,g) + K[t] + W[t]
121
101
  let [T1h, T1l] = add64(hh, hl, S1h, S1l);
122
102
  [T1h, T1l] = add64(T1h, T1l, chH, chL);
123
103
  [T1h, T1l] = add64(T1h, T1l, K512[t * 2], K512[t * 2 + 1]);
124
104
  [T1h, T1l] = add64(T1h, T1l, W[t * 2], W[t * 2 + 1]);
125
- // Σ0(a) = ROTR28 ^ ROTR34 ^ ROTR39
126
105
  const [r28h, r28l] = rotr64(ah, al, 28);
127
106
  const [r34h, r34l] = rotr64(ah, al, 34);
128
107
  const [r39h, r39l] = rotr64(ah, al, 39);
129
108
  const [S0h, S0l] = xor3_64(r28h, r28l, r34h, r34l, r39h, r39l);
130
- // Maj(a, b, c) = (a & b) ^ (a & c) ^ (b & c)
131
109
  const majH = ((ah & bh) ^ (ah & ch_v) ^ (bh & ch_v)) >>> 0;
132
110
  const majL = ((al & bl) ^ (al & cl) ^ (bl & cl)) >>> 0;
133
- // T2 = Σ0(a) + Maj(a,b,c)
134
111
  const [T2h, T2l] = add64(S0h, S0l, majH, majL);
135
112
  hh = gh;
136
113
  hl = gl;
@@ -179,17 +156,9 @@ function sha512Core(data, initH) {
179
156
  }
180
157
  return digest;
181
158
  }
182
- /**
183
- * Compute SHA-512 digest of `data`.
184
- * Returns a 64-byte Uint8Array.
185
- */
186
159
  export function sha512(data) {
187
160
  return sha512Core(data, INIT_512);
188
161
  }
189
- /**
190
- * Compute SHA-384 digest of `data`.
191
- * Returns a 48-byte Uint8Array.
192
- */
193
162
  export function sha384(data) {
194
163
  return sha512Core(data, INIT_384).subarray(0, 48);
195
164
  }
package/package.json CHANGED
@@ -1,7 +1,11 @@
1
1
  {
2
2
  "name": "@signature-kit/crypto",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "license": "MIT",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/Licitei/signature-kit.git"
8
+ },
5
9
  "files": [
6
10
  "dist",
7
11
  "README.md",
@@ -32,20 +36,18 @@
32
36
  "access": "public"
33
37
  },
34
38
  "scripts": {
35
- "build": "tsc -b tsconfig.json",
36
- "typecheck": "tsc -p tsconfig.json --noEmit",
37
39
  "test": "vitest run"
38
40
  },
39
41
  "dependencies": {
40
- "@signature-kit/asn1": "^0.2.0",
41
- "effect": "4.0.0-beta.86"
42
+ "@signature-kit/asn1": "^0.2.1",
43
+ "effect": "4.0.5"
42
44
  },
43
45
  "nx": {
44
46
  "targets": {
45
47
  "npm-pack": {
46
48
  "executor": "nx:run-commands",
47
49
  "options": {
48
- "command": "npm pack --dry-run --json",
50
+ "command": "bun pm pack --dry-run",
49
51
  "cwd": "{projectRoot}"
50
52
  }
51
53
  },