@signature-kit/crypto 0.1.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 (43) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +21 -0
  3. package/README.md +30 -0
  4. package/dist/base64.d.ts +3 -0
  5. package/dist/base64.d.ts.map +1 -0
  6. package/dist/base64.js +55 -0
  7. package/dist/config.d.ts +46 -0
  8. package/dist/config.d.ts.map +1 -0
  9. package/dist/config.js +78 -0
  10. package/dist/pem.d.ts +3 -0
  11. package/dist/pem.d.ts.map +1 -0
  12. package/dist/pem.js +15 -0
  13. package/dist/pkcs12.d.ts +13 -0
  14. package/dist/pkcs12.d.ts.map +1 -0
  15. package/dist/pkcs12.js +471 -0
  16. package/dist/primitives/aes.d.ts +17 -0
  17. package/dist/primitives/aes.d.ts.map +1 -0
  18. package/dist/primitives/aes.js +219 -0
  19. package/dist/primitives/des.d.ts +23 -0
  20. package/dist/primitives/des.d.ts.map +1 -0
  21. package/dist/primitives/des.js +271 -0
  22. package/dist/primitives/hmac.d.ts +39 -0
  23. package/dist/primitives/hmac.d.ts.map +1 -0
  24. package/dist/primitives/hmac.js +146 -0
  25. package/dist/primitives/padding.d.ts +11 -0
  26. package/dist/primitives/padding.d.ts.map +1 -0
  27. package/dist/primitives/padding.js +29 -0
  28. package/dist/primitives/pbkdf2.d.ts +19 -0
  29. package/dist/primitives/pbkdf2.d.ts.map +1 -0
  30. package/dist/primitives/pbkdf2.js +61 -0
  31. package/dist/primitives/rc2.d.ts +23 -0
  32. package/dist/primitives/rc2.d.ts.map +1 -0
  33. package/dist/primitives/rc2.js +192 -0
  34. package/dist/primitives/sha1.d.ts +15 -0
  35. package/dist/primitives/sha1.d.ts.map +1 -0
  36. package/dist/primitives/sha1.js +102 -0
  37. package/dist/primitives/sha256.d.ts +30 -0
  38. package/dist/primitives/sha256.d.ts.map +1 -0
  39. package/dist/primitives/sha256.js +146 -0
  40. package/dist/primitives/sha512.d.ts +22 -0
  41. package/dist/primitives/sha512.d.ts.map +1 -0
  42. package/dist/primitives/sha512.js +197 -0
  43. package/package.json +61 -0
@@ -0,0 +1,146 @@
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
+ const INIT_H = new Uint32Array([
8
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
9
+ ]);
10
+ // SHA-256 round constants (first 32 bits of fractional parts of cbrt of first 64 primes)
11
+ const K = new Uint32Array([
12
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
13
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
14
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
15
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
16
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
17
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
18
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
19
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
20
+ ]);
21
+ function rotr32(x, n) {
22
+ return ((x >>> n) | (x << (32 - n))) >>> 0;
23
+ }
24
+ /** Process a single 64-byte block into hash state `h` (mutates in place). */
25
+ function processBlock(h, view, offset) {
26
+ const w = new Uint32Array(64);
27
+ for (let t = 0; t < 16; t++) {
28
+ w[t] = view.getUint32(offset + t * 4, false);
29
+ }
30
+ for (let t = 16; t < 64; t++) {
31
+ const s0 = rotr32(w[t - 15], 7) ^ rotr32(w[t - 15], 18) ^ (w[t - 15] >>> 3);
32
+ const s1 = rotr32(w[t - 2], 17) ^ rotr32(w[t - 2], 19) ^ (w[t - 2] >>> 10);
33
+ w[t] = (w[t - 16] + s0 + w[t - 7] + s1) >>> 0;
34
+ }
35
+ let a = h[0];
36
+ let b = h[1];
37
+ let c = h[2];
38
+ let d = h[3];
39
+ let e = h[4];
40
+ let f = h[5];
41
+ let g = h[6];
42
+ let hh = h[7];
43
+ for (let t = 0; t < 64; t++) {
44
+ const S1 = rotr32(e, 6) ^ rotr32(e, 11) ^ rotr32(e, 25);
45
+ const ch = (e & f) ^ ((~e >>> 0) & g);
46
+ const temp1 = (hh + S1 + ch + K[t] + w[t]) >>> 0;
47
+ const S0 = rotr32(a, 2) ^ rotr32(a, 13) ^ rotr32(a, 22);
48
+ const maj = (a & b) ^ (a & c) ^ (b & c);
49
+ const temp2 = (S0 + maj) >>> 0;
50
+ hh = g;
51
+ g = f;
52
+ f = e;
53
+ e = (d + temp1) >>> 0;
54
+ d = c;
55
+ c = b;
56
+ b = a;
57
+ a = (temp1 + temp2) >>> 0;
58
+ }
59
+ h[0] = (h[0] + a) >>> 0;
60
+ h[1] = (h[1] + b) >>> 0;
61
+ h[2] = (h[2] + c) >>> 0;
62
+ h[3] = (h[3] + d) >>> 0;
63
+ h[4] = (h[4] + e) >>> 0;
64
+ h[5] = (h[5] + f) >>> 0;
65
+ h[6] = (h[6] + g) >>> 0;
66
+ h[7] = (h[7] + hh) >>> 0;
67
+ }
68
+ /**
69
+ * Compute SHA-256 digest of `data`.
70
+ * Returns a 32-byte Uint8Array.
71
+ */
72
+ export function sha256(data) {
73
+ // ---- Pre-processing: padding ----
74
+ const bitLen = data.length * 8;
75
+ const padLen = data.length % 64 < 56 ? 56 - (data.length % 64) : 120 - (data.length % 64);
76
+ const totalLen = data.length + padLen + 8;
77
+ const msg = new Uint8Array(totalLen);
78
+ msg.set(data);
79
+ msg[data.length] = 0x80;
80
+ const view = new DataView(msg.buffer, msg.byteOffset, msg.byteLength);
81
+ const bitLenHi = Math.floor(data.length / 0x20000000) | 0;
82
+ const bitLenLo = bitLen >>> 0;
83
+ view.setUint32(totalLen - 8, bitLenHi, false);
84
+ view.setUint32(totalLen - 4, bitLenLo, false);
85
+ // ---- Processing ----
86
+ const h = new Uint32Array(INIT_H);
87
+ for (let i = 0; i < totalLen; i += 64) {
88
+ processBlock(h, view, i);
89
+ }
90
+ // ---- Produce digest ----
91
+ const digest = new Uint8Array(32);
92
+ const dv = new DataView(digest.buffer);
93
+ for (let i = 0; i < 8; i++) {
94
+ dv.setUint32(i * 4, h[i], false);
95
+ }
96
+ return digest;
97
+ }
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
+ export function sha256WithState(initState, data, prefixLen) {
111
+ // Total message length = prefixLen (pre-processed) + data.length
112
+ const totalDataLen = prefixLen + data.length;
113
+ const bitLen = totalDataLen * 8;
114
+ const padLen = totalDataLen % 64 < 56 ? 56 - (totalDataLen % 64) : 120 - (totalDataLen % 64);
115
+ const totalLen = data.length + padLen + 8;
116
+ const msg = new Uint8Array(totalLen);
117
+ msg.set(data);
118
+ msg[data.length] = 0x80;
119
+ const view = new DataView(msg.buffer, msg.byteOffset, msg.byteLength);
120
+ const bitLenHi = Math.floor(totalDataLen / 0x20000000) | 0;
121
+ const bitLenLo = bitLen >>> 0;
122
+ view.setUint32(totalLen - 8, bitLenHi, false);
123
+ view.setUint32(totalLen - 4, bitLenLo, false);
124
+ // Start from the pre-processed state (copy so we don't mutate)
125
+ const h = new Uint32Array(initState);
126
+ for (let i = 0; i < totalLen; i += 64) {
127
+ processBlock(h, view, i);
128
+ }
129
+ const digest = new Uint8Array(32);
130
+ const dv = new DataView(digest.buffer);
131
+ for (let i = 0; i < 8; i++) {
132
+ dv.setUint32(i * 4, h[i], false);
133
+ }
134
+ return digest;
135
+ }
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
+ export function sha256ProcessBlock(blockBytes) {
142
+ const h = new Uint32Array(INIT_H);
143
+ const view = new DataView(blockBytes.buffer, blockBytes.byteOffset, blockBytes.byteLength);
144
+ processBlock(h, view, 0);
145
+ return h;
146
+ }
@@ -0,0 +1,22 @@
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
+ export declare function sha512(data: Uint8Array): Uint8Array;
17
+ /**
18
+ * Compute SHA-384 digest of `data`.
19
+ * Returns a 48-byte Uint8Array.
20
+ */
21
+ export declare function sha384(data: Uint8Array): Uint8Array;
22
+ //# sourceMappingURL=sha512.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sha512.d.ts","sourceRoot":"","sources":["../../src/primitives/sha512.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAwNH;;;GAGG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CAEnD;AAED;;;GAGG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CAEnD"}
@@ -0,0 +1,197 @@
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
+ const K512 = new Uint32Array([
15
+ 0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd, 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
16
+ 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019, 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
17
+ 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe, 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
18
+ 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1, 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
19
+ 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3, 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
20
+ 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483, 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
21
+ 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210, 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
22
+ 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725, 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
23
+ 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926, 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
24
+ 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8, 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
25
+ 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001, 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
26
+ 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910, 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
27
+ 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53, 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
28
+ 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb, 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
29
+ 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60, 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
30
+ 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9, 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
31
+ 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207, 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
32
+ 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6, 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
33
+ 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493, 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
34
+ 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a, 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817,
35
+ ]);
36
+ // SHA-512 initial hash values
37
+ const INIT_512 = new Uint32Array([
38
+ 0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa73b, 0x3c6ef372, 0xfe94f82b, 0xa54ff53a, 0x5f1d36f1,
39
+ 0x510e527f, 0xade682d1, 0x9b05688c, 0x2b3e6c1f, 0x1f83d9ab, 0xfb41bd6b, 0x5be0cd19, 0x137e2179,
40
+ ]);
41
+ // SHA-384 initial hash values
42
+ const INIT_384 = new Uint32Array([
43
+ 0xcbbb9d5d, 0xc1059ed8, 0x629a292a, 0x367cd507, 0x9159015a, 0x3070dd17, 0x152fecd8, 0xf70e5939,
44
+ 0x67332667, 0xffc00b31, 0x8eb44a87, 0x68581511, 0xdb0c2e0d, 0x64f98fa7, 0x47b5481d, 0xbefa4fa4,
45
+ ]);
46
+ function add64(ah, al, bh, bl) {
47
+ const lo = (al + bl) >>> 0;
48
+ const carry = lo < al >>> 0 ? 1 : 0;
49
+ const hi = (ah + bh + carry) >>> 0;
50
+ return [hi, lo];
51
+ }
52
+ function rotr64(hi, lo, n) {
53
+ if (n < 32) {
54
+ return [((hi >>> n) | (lo << (32 - n))) >>> 0, ((lo >>> n) | (hi << (32 - n))) >>> 0];
55
+ }
56
+ n -= 32;
57
+ return [((lo >>> n) | (hi << (32 - n))) >>> 0, ((hi >>> n) | (lo << (32 - n))) >>> 0];
58
+ }
59
+ function shr64(hi, lo, n) {
60
+ return [(hi >>> n) >>> 0, ((lo >>> n) | (hi << (32 - n))) >>> 0];
61
+ }
62
+ function xor3_64(ah, al, bh, bl, ch_, cl) {
63
+ return [(ah ^ bh ^ ch_) >>> 0, (al ^ bl ^ cl) >>> 0];
64
+ }
65
+ function sha512Core(data, initH) {
66
+ const byteLenHi = Math.floor(data.length / 0x20000000) | 0;
67
+ const byteLenLo = data.length >>> 0;
68
+ const bitLenHi = ((byteLenHi << 3) | (byteLenLo >>> 29)) >>> 0;
69
+ const bitLenLo = (byteLenLo << 3) >>> 0;
70
+ const padLen = data.length % 128 < 112 ? 112 - (data.length % 128) : 240 - (data.length % 128);
71
+ const totalLen = data.length + padLen + 16;
72
+ const msg = new Uint8Array(totalLen);
73
+ msg.set(data);
74
+ msg[data.length] = 0x80;
75
+ const view = new DataView(msg.buffer, msg.byteOffset, msg.byteLength);
76
+ view.setUint32(totalLen - 16, 0, false);
77
+ view.setUint32(totalLen - 12, 0, false);
78
+ view.setUint32(totalLen - 8, bitLenHi, false);
79
+ view.setUint32(totalLen - 4, bitLenLo, false);
80
+ const h = new Uint32Array(16);
81
+ h.set(initH);
82
+ const W = new Uint32Array(160); // 80 × 64-bit
83
+ for (let i = 0; i < totalLen; i += 128) {
84
+ for (let t = 0; t < 16; t++) {
85
+ W[t * 2] = view.getUint32(i + t * 8, false);
86
+ W[t * 2 + 1] = view.getUint32(i + t * 8 + 4, false);
87
+ }
88
+ for (let t = 16; t < 80; t++) {
89
+ // σ1(W[t-2]) = ROTR19 ^ ROTR61 ^ SHR6
90
+ const [r19h, r19l] = rotr64(W[(t - 2) * 2], W[(t - 2) * 2 + 1], 19);
91
+ const [r61h, r61l] = rotr64(W[(t - 2) * 2], W[(t - 2) * 2 + 1], 61);
92
+ const [s6h, s6l] = shr64(W[(t - 2) * 2], W[(t - 2) * 2 + 1], 6);
93
+ const [s1h, s1l] = xor3_64(r19h, r19l, r61h, r61l, s6h, s6l);
94
+ // σ0(W[t-15]) = ROTR1 ^ ROTR8 ^ SHR7
95
+ const [r1h, r1l] = rotr64(W[(t - 15) * 2], W[(t - 15) * 2 + 1], 1);
96
+ const [r8h, r8l] = rotr64(W[(t - 15) * 2], W[(t - 15) * 2 + 1], 8);
97
+ const [s7h, s7l] = shr64(W[(t - 15) * 2], W[(t - 15) * 2 + 1], 7);
98
+ const [s0h, s0l] = xor3_64(r1h, r1l, r8h, r8l, s7h, s7l);
99
+ let [rh, rl] = add64(W[(t - 16) * 2], W[(t - 16) * 2 + 1], s0h, s0l);
100
+ [rh, rl] = add64(rh, rl, W[(t - 7) * 2], W[(t - 7) * 2 + 1]);
101
+ [rh, rl] = add64(rh, rl, s1h, s1l);
102
+ W[t * 2] = rh;
103
+ W[t * 2 + 1] = rl;
104
+ }
105
+ let ah = h[0], al = h[1];
106
+ let bh = h[2], bl = h[3];
107
+ let ch_v = h[4], cl = h[5];
108
+ let dh = h[6], dl = h[7];
109
+ let eh = h[8], el = h[9];
110
+ let fh = h[10], fl = h[11];
111
+ let gh = h[12], gl = h[13];
112
+ let hh = h[14], hl = h[15];
113
+ for (let t = 0; t < 80; t++) {
114
+ // Σ1(e) = ROTR14 ^ ROTR18 ^ ROTR41
115
+ const [r14h, r14l] = rotr64(eh, el, 14);
116
+ const [r18h, r18l] = rotr64(eh, el, 18);
117
+ const [r41h, r41l] = rotr64(eh, el, 41);
118
+ const [S1h, S1l] = xor3_64(r14h, r14l, r18h, r18l, r41h, r41l);
119
+ // Ch(e, f, g) = (e & f) ^ (~e & g)
120
+ const chH = ((eh & fh) ^ ((~eh >>> 0) & gh)) >>> 0;
121
+ const chL = ((el & fl) ^ ((~el >>> 0) & gl)) >>> 0;
122
+ // T1 = h + Σ1(e) + Ch(e,f,g) + K[t] + W[t]
123
+ let [T1h, T1l] = add64(hh, hl, S1h, S1l);
124
+ [T1h, T1l] = add64(T1h, T1l, chH, chL);
125
+ [T1h, T1l] = add64(T1h, T1l, K512[t * 2], K512[t * 2 + 1]);
126
+ [T1h, T1l] = add64(T1h, T1l, W[t * 2], W[t * 2 + 1]);
127
+ // Σ0(a) = ROTR28 ^ ROTR34 ^ ROTR39
128
+ const [r28h, r28l] = rotr64(ah, al, 28);
129
+ const [r34h, r34l] = rotr64(ah, al, 34);
130
+ const [r39h, r39l] = rotr64(ah, al, 39);
131
+ const [S0h, S0l] = xor3_64(r28h, r28l, r34h, r34l, r39h, r39l);
132
+ // Maj(a, b, c) = (a & b) ^ (a & c) ^ (b & c)
133
+ const majH = ((ah & bh) ^ (ah & ch_v) ^ (bh & ch_v)) >>> 0;
134
+ const majL = ((al & bl) ^ (al & cl) ^ (bl & cl)) >>> 0;
135
+ // T2 = Σ0(a) + Maj(a,b,c)
136
+ const [T2h, T2l] = add64(S0h, S0l, majH, majL);
137
+ hh = gh;
138
+ hl = gl;
139
+ gh = fh;
140
+ gl = fl;
141
+ fh = eh;
142
+ fl = el;
143
+ [eh, el] = add64(dh, dl, T1h, T1l);
144
+ dh = ch_v;
145
+ dl = cl;
146
+ ch_v = bh;
147
+ cl = bl;
148
+ bh = ah;
149
+ bl = al;
150
+ [ah, al] = add64(T1h, T1l, T2h, T2l);
151
+ }
152
+ const t0 = add64(h[0], h[1], ah, al);
153
+ h[0] = t0[0];
154
+ h[1] = t0[1];
155
+ const t1 = add64(h[2], h[3], bh, bl);
156
+ h[2] = t1[0];
157
+ h[3] = t1[1];
158
+ const t2 = add64(h[4], h[5], ch_v, cl);
159
+ h[4] = t2[0];
160
+ h[5] = t2[1];
161
+ const t3 = add64(h[6], h[7], dh, dl);
162
+ h[6] = t3[0];
163
+ h[7] = t3[1];
164
+ const t4 = add64(h[8], h[9], eh, el);
165
+ h[8] = t4[0];
166
+ h[9] = t4[1];
167
+ const t5 = add64(h[10], h[11], fh, fl);
168
+ h[10] = t5[0];
169
+ h[11] = t5[1];
170
+ const t6 = add64(h[12], h[13], gh, gl);
171
+ h[12] = t6[0];
172
+ h[13] = t6[1];
173
+ const t7 = add64(h[14], h[15], hh, hl);
174
+ h[14] = t7[0];
175
+ h[15] = t7[1];
176
+ }
177
+ const digest = new Uint8Array(64);
178
+ const dv = new DataView(digest.buffer);
179
+ for (let i = 0; i < 16; i++) {
180
+ dv.setUint32(i * 4, h[i], false);
181
+ }
182
+ return digest;
183
+ }
184
+ /**
185
+ * Compute SHA-512 digest of `data`.
186
+ * Returns a 64-byte Uint8Array.
187
+ */
188
+ export function sha512(data) {
189
+ return sha512Core(data, INIT_512);
190
+ }
191
+ /**
192
+ * Compute SHA-384 digest of `data`.
193
+ * Returns a 48-byte Uint8Array.
194
+ */
195
+ export function sha384(data) {
196
+ return sha512Core(data, INIT_384).subarray(0, 48);
197
+ }
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@signature-kit/crypto",
3
+ "version": "0.1.0",
4
+ "license": "MIT",
5
+ "files": [
6
+ "dist",
7
+ "README.md",
8
+ "LICENSE",
9
+ "CHANGELOG.md"
10
+ ],
11
+ "type": "module",
12
+ "sideEffects": false,
13
+ "exports": {
14
+ "./base64": {
15
+ "types": "./dist/base64.d.ts",
16
+ "import": "./dist/base64.js"
17
+ },
18
+ "./config": {
19
+ "types": "./dist/config.d.ts",
20
+ "import": "./dist/config.js"
21
+ },
22
+ "./pem": {
23
+ "types": "./dist/pem.d.ts",
24
+ "import": "./dist/pem.js"
25
+ },
26
+ "./pkcs12": {
27
+ "types": "./dist/pkcs12.d.ts",
28
+ "import": "./dist/pkcs12.js"
29
+ }
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "scripts": {
35
+ "build": "tsc -b tsconfig.json",
36
+ "typecheck": "tsc -p tsconfig.json --noEmit",
37
+ "test": "vitest run"
38
+ },
39
+ "dependencies": {
40
+ "@signature-kit/asn1": "^0.1.0",
41
+ "effect": "4.0.0-beta.86"
42
+ },
43
+ "nx": {
44
+ "targets": {
45
+ "npm-pack": {
46
+ "executor": "nx:run-commands",
47
+ "options": {
48
+ "command": "npm pack --dry-run --json",
49
+ "cwd": "{projectRoot}"
50
+ }
51
+ },
52
+ "npm-publish": {
53
+ "executor": "nx:run-commands",
54
+ "options": {
55
+ "command": "npm publish --access public",
56
+ "cwd": "{projectRoot}"
57
+ }
58
+ }
59
+ }
60
+ }
61
+ }