@ansonlai/docx-redline-js 0.7.1 → 0.8.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.
package/core/sha256.js ADDED
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Pure-JS synchronous SHA-256 (FIPS 180-4) implementation.
3
+ *
4
+ * Self-contained, deterministic, and free from Node.js or browser crypto built-ins.
5
+ * Accepts string, Uint8Array, Buffer, or ArrayBuffer and returns a 64-character hex string.
6
+ */
7
+
8
+ const K = new Uint32Array([
9
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
10
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
11
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
12
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
13
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
14
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
15
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
16
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
17
+ ]);
18
+
19
+ const textEncoder = new TextEncoder();
20
+
21
+ /**
22
+ * Computes the SHA-256 digest of binary or text data.
23
+ *
24
+ * @param {Uint8Array|ArrayBuffer|ArrayBufferView|string} input - Input data to hash
25
+ * @returns {string} 64-character lowercase hex string
26
+ */
27
+ export function sha256(input) {
28
+ let bytes;
29
+ if (typeof input === 'string') {
30
+ bytes = textEncoder.encode(input);
31
+ } else if (input instanceof Uint8Array) {
32
+ bytes = input;
33
+ } else if (input instanceof ArrayBuffer) {
34
+ bytes = new Uint8Array(input);
35
+ } else if (ArrayBuffer.isView(input)) {
36
+ bytes = new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
37
+ } else {
38
+ throw new TypeError('sha256 input must be a string, Uint8Array, Buffer, or ArrayBuffer.');
39
+ }
40
+
41
+ const byteLen = bytes.length;
42
+ const bitLen = byteLen * 8;
43
+
44
+ // Pad length: input + 1 (0x80) + zeros + 8 bytes (64-bit length) to multiple of 64 bytes
45
+ const paddedLen = Math.ceil((byteLen + 9) / 64) * 64;
46
+ const padded = new Uint8Array(paddedLen);
47
+ padded.set(bytes, 0);
48
+ padded[byteLen] = 0x80;
49
+
50
+ const dataView = new DataView(padded.buffer, padded.byteOffset, padded.byteLength);
51
+ // Write 64-bit big-endian length in bits
52
+ const highBits = Math.floor(bitLen / 0x100000000);
53
+ const lowBits = (bitLen >>> 0);
54
+ dataView.setUint32(paddedLen - 8, highBits, false);
55
+ dataView.setUint32(paddedLen - 4, lowBits, false);
56
+
57
+ // Initial hash values
58
+ let h0 = 0x6a09e667;
59
+ let h1 = 0xbb67ae85;
60
+ let h2 = 0x3c6ef372;
61
+ let h3 = 0xa54ff53a;
62
+ let h4 = 0x510e527f;
63
+ let h5 = 0x9b05688c;
64
+ let h6 = 0x1f83d9ab;
65
+ let h7 = 0x5be0cd19;
66
+
67
+ const W = new Uint32Array(64);
68
+
69
+ for (let offset = 0; offset < paddedLen; offset += 64) {
70
+ for (let t = 0; t < 16; t++) {
71
+ W[t] = dataView.getUint32(offset + t * 4, false);
72
+ }
73
+ for (let t = 16; t < 64; t++) {
74
+ const w15 = W[t - 15];
75
+ const s0 = ((w15 >>> 7) | (w15 << 25)) ^ ((w15 >>> 18) | (w15 << 14)) ^ (w15 >>> 3);
76
+ const w2 = W[t - 2];
77
+ const s1 = ((w2 >>> 17) | (w2 << 15)) ^ ((w2 >>> 19) | (w2 << 13)) ^ (w2 >>> 10);
78
+ W[t] = (W[t - 16] + s0 + W[t - 7] + s1) | 0;
79
+ }
80
+
81
+ let a = h0;
82
+ let b = h1;
83
+ let c = h2;
84
+ let d = h3;
85
+ let e = h4;
86
+ let f = h5;
87
+ let g = h6;
88
+ let h = h7;
89
+
90
+ for (let t = 0; t < 64; t++) {
91
+ const S1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
92
+ const ch = (e & f) ^ (~e & g);
93
+ const temp1 = (h + S1 + ch + K[t] + W[t]) | 0;
94
+ const S0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
95
+ const maj = (a & b) ^ (a & c) ^ (b & c);
96
+ const temp2 = (S0 + maj) | 0;
97
+
98
+ h = g;
99
+ g = f;
100
+ f = e;
101
+ e = (d + temp1) | 0;
102
+ d = c;
103
+ c = b;
104
+ b = a;
105
+ a = (temp1 + temp2) | 0;
106
+ }
107
+
108
+ h0 = (h0 + a) | 0;
109
+ h1 = (h1 + b) | 0;
110
+ h2 = (h2 + c) | 0;
111
+ h3 = (h3 + d) | 0;
112
+ h4 = (h4 + e) | 0;
113
+ h5 = (h5 + f) | 0;
114
+ h6 = (h6 + g) | 0;
115
+ h7 = (h7 + h) | 0;
116
+ }
117
+
118
+ const hex = n => (n >>> 0).toString(16).padStart(8, '0');
119
+ return hex(h0) + hex(h1) + hex(h2) + hex(h3) + hex(h4) + hex(h5) + hex(h6) + hex(h7);
120
+ }