quarkdash 1.0.8 → 1.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 (114) hide show
  1. package/README.md +4 -2
  2. package/dist/cjs/cipher/chacha.js +154 -0
  3. package/dist/cjs/cipher/chacha.js.map +1 -0
  4. package/dist/cjs/cipher/cipher.js +34 -0
  5. package/dist/cjs/cipher/cipher.js.map +1 -0
  6. package/dist/cjs/cipher/gimli.js +143 -0
  7. package/dist/cjs/cipher/gimli.js.map +1 -0
  8. package/dist/cjs/core/kdf.js.map +1 -0
  9. package/dist/cjs/{mac.js → core/mac.js} +37 -1
  10. package/dist/cjs/core/mac.js.map +1 -0
  11. package/dist/cjs/{types.js.map → core/types.js.map} +1 -1
  12. package/dist/cjs/{utils.js → core/utils.js} +25 -8
  13. package/dist/cjs/core/utils.js.map +1 -0
  14. package/dist/cjs/core/wasm_loader.js +100 -0
  15. package/dist/cjs/core/wasm_loader.js.map +1 -0
  16. package/dist/cjs/crypto.js +30 -13
  17. package/dist/cjs/crypto.js.map +1 -1
  18. package/dist/cjs/{sha.js → hash/sha.js} +3 -2
  19. package/dist/cjs/hash/sha.js.map +1 -0
  20. package/dist/cjs/hash/shake.js +318 -0
  21. package/dist/cjs/hash/shake.js.map +1 -0
  22. package/dist/cjs/index.js +16 -11
  23. package/dist/cjs/index.js.map +1 -1
  24. package/dist/cjs/{ringlwe.js → session/baselwe.js} +186 -177
  25. package/dist/cjs/session/baselwe.js.map +1 -0
  26. package/dist/cjs/session/ringlwe.js +19 -0
  27. package/dist/cjs/session/ringlwe.js.map +1 -0
  28. package/dist/cjs/session/rringlwe.js +42 -0
  29. package/dist/cjs/session/rringlwe.js.map +1 -0
  30. package/dist/esm/cipher/chacha.js +150 -0
  31. package/dist/esm/cipher/chacha.js.map +1 -0
  32. package/dist/esm/cipher/cipher.js +30 -0
  33. package/dist/esm/cipher/cipher.js.map +1 -0
  34. package/dist/esm/cipher/gimli.js +139 -0
  35. package/dist/esm/cipher/gimli.js.map +1 -0
  36. package/dist/esm/core/kdf.js.map +1 -0
  37. package/dist/esm/core/mac.js +87 -0
  38. package/dist/esm/core/mac.js.map +1 -0
  39. package/dist/esm/{types.js.map → core/types.js.map} +1 -1
  40. package/dist/esm/{utils.js → core/utils.js} +24 -4
  41. package/dist/esm/core/utils.js.map +1 -0
  42. package/dist/esm/core/wasm_loader.js +64 -0
  43. package/dist/esm/core/wasm_loader.js.map +1 -0
  44. package/dist/esm/crypto.js +30 -13
  45. package/dist/esm/crypto.js.map +1 -1
  46. package/dist/esm/{sha.js → hash/sha.js} +3 -2
  47. package/dist/esm/hash/sha.js.map +1 -0
  48. package/dist/esm/hash/shake.js +311 -0
  49. package/dist/esm/hash/shake.js.map +1 -0
  50. package/dist/esm/index.js +16 -11
  51. package/dist/esm/index.js.map +1 -1
  52. package/dist/esm/{ringlwe.js → session/baselwe.js} +184 -175
  53. package/dist/esm/session/baselwe.js.map +1 -0
  54. package/dist/esm/session/ringlwe.js +15 -0
  55. package/dist/esm/session/ringlwe.js.map +1 -0
  56. package/dist/esm/session/rringlwe.js +38 -0
  57. package/dist/esm/session/rringlwe.js.map +1 -0
  58. package/dist/shake.wasm +0 -0
  59. package/dist/types/cipher/chacha.d.ts +67 -0
  60. package/dist/types/cipher/cipher.d.ts +31 -0
  61. package/dist/types/cipher/gimli.d.ts +64 -0
  62. package/dist/types/{kdf.d.ts → core/kdf.d.ts} +3 -2
  63. package/dist/types/{mac.d.ts → core/mac.d.ts} +21 -2
  64. package/dist/types/{types.d.ts → core/types.d.ts} +5 -2
  65. package/dist/types/{utils.d.ts → core/utils.d.ts} +13 -0
  66. package/dist/types/core/wasm_loader.d.ts +5 -0
  67. package/dist/types/crypto.d.ts +8 -4
  68. package/dist/types/{sha.d.ts → hash/sha.d.ts} +3 -2
  69. package/dist/types/hash/shake.d.ts +85 -0
  70. package/dist/types/index.d.ts +15 -10
  71. package/dist/types/{ringlwe.d.ts → session/baselwe.d.ts} +49 -59
  72. package/dist/types/session/ringlwe.d.ts +21 -0
  73. package/dist/types/session/rringlwe.d.ts +27 -0
  74. package/package.json +12 -4
  75. package/wasm/shake.wasm +0 -0
  76. package/dist/cjs/cipher.js +0 -265
  77. package/dist/cjs/cipher.js.map +0 -1
  78. package/dist/cjs/kdf.js.map +0 -1
  79. package/dist/cjs/mac.js.map +0 -1
  80. package/dist/cjs/ringlwe.js.map +0 -1
  81. package/dist/cjs/sha.js.map +0 -1
  82. package/dist/cjs/shake.js +0 -159
  83. package/dist/cjs/shake.js.map +0 -1
  84. package/dist/cjs/utils.js.map +0 -1
  85. package/dist/esm/cipher.js +0 -259
  86. package/dist/esm/cipher.js.map +0 -1
  87. package/dist/esm/kdf.js.map +0 -1
  88. package/dist/esm/mac.js +0 -51
  89. package/dist/esm/mac.js.map +0 -1
  90. package/dist/esm/ringlwe.js.map +0 -1
  91. package/dist/esm/sha.js.map +0 -1
  92. package/dist/esm/shake.js +0 -155
  93. package/dist/esm/shake.js.map +0 -1
  94. package/dist/esm/utils.js.map +0 -1
  95. package/dist/types/cipher.d.ts +0 -153
  96. package/dist/types/shake.d.ts +0 -53
  97. package/docutil.ts +0 -84
  98. package/img/cover.png +0 -0
  99. package/src/cipher.ts +0 -260
  100. package/src/crypto.ts +0 -306
  101. package/src/index.ts +0 -27
  102. package/src/kdf.ts +0 -64
  103. package/src/mac.ts +0 -65
  104. package/src/ringlwe.ts +0 -340
  105. package/src/sha.ts +0 -265
  106. package/src/shake.ts +0 -163
  107. package/src/types.ts +0 -75
  108. package/src/utils.ts +0 -290
  109. package/tsconfig.cjs.json +0 -7
  110. package/tsconfig.json +0 -18
  111. /package/dist/cjs/{kdf.js → core/kdf.js} +0 -0
  112. /package/dist/cjs/{types.js → core/types.js} +0 -0
  113. /package/dist/esm/{kdf.js → core/kdf.js} +0 -0
  114. /package/dist/esm/{types.js → core/types.js} +0 -0
package/src/ringlwe.ts DELETED
@@ -1,340 +0,0 @@
1
- /**
2
- * QuarkDash Ring-LWE Implementation
3
- *
4
- * @git https://github.com/devsdaddy/quarkdash
5
- * @version 1.0.0
6
- * @author Elijah Rastorguev
7
- * @build 1002
8
- * @website https://dev.to/devsdaddy
9
- * @updated 12.04.2026
10
- */
11
- /* Import Required Modules */
12
- import {ICryptoEncapsulated, ICryptoKeyPair, IKeyExchange} from "./types";
13
- import {QuarkDashUtils} from "./utils";
14
- import {SHA256} from "./sha";
15
-
16
- /**
17
- * Ring-LWE based key exchange implementation
18
- */
19
- export class QuarkDashKeyExchange implements IKeyExchange {
20
- // Ring-LWE Constants
21
- private static readonly N = 256;
22
- private static readonly Q = 7681n;
23
- private static readonly ROOT = 7n;
24
- private static readonly INV_N = this.modInverse(BigInt(this.N), this.Q);
25
-
26
- /**
27
- * Generate crypto key pair async
28
- * @returns {ICryptoKeyPair} Crypto key pair
29
- * TODO: GPU Calculations
30
- */
31
- public async generateKeyPair(): Promise<ICryptoKeyPair> {
32
- return this.generateKeyPairSync();
33
- }
34
-
35
- /**
36
- * Generate crypto key pair sync
37
- * @returns {ICryptoKeyPair} Crypto key pair
38
- */
39
- public generateKeyPairSync(): ICryptoKeyPair {
40
- const a = QuarkDashKeyExchange.uniformPoly();
41
- const s = QuarkDashKeyExchange.smallPoly();
42
- const e = QuarkDashKeyExchange.errorPoly();
43
- const as = QuarkDashKeyExchange.multiply(a, s);
44
- const b = new Array<bigint>(QuarkDashKeyExchange.N);
45
- for (let i = 0; i < QuarkDashKeyExchange.N; i++) {
46
- b[i] = (as[i] + e[i]) % QuarkDashKeyExchange.Q;
47
- }
48
- const publicKey = QuarkDashUtils.concatBytes(
49
- QuarkDashKeyExchange.serializePoly(a),
50
- QuarkDashKeyExchange.serializePoly(b)
51
- );
52
- const privateKey = QuarkDashKeyExchange.serializePoly(s);
53
- return { publicKey, privateKey };
54
- }
55
-
56
- /**
57
- * Encapsulate async
58
- * @param publicKey {Uint8Array} Public key buffer
59
- * @returns {Promise<ICryptoEncapsulated>} Encapsulated data
60
- * TODO: GPU Calculations
61
- */
62
- public async encapsulate(publicKey: Uint8Array): Promise<ICryptoEncapsulated> {
63
- return this.encapsulateSync(publicKey);
64
- }
65
-
66
- /**
67
- * Encapsulate sync
68
- * @param publicKey {Uint8Array} Public key buffer
69
- * @returns {ICryptoEncapsulated} Encapsulated data
70
- */
71
- public encapsulateSync(publicKey: Uint8Array): ICryptoEncapsulated {
72
- const aBytes = publicKey.slice(0, QuarkDashKeyExchange.N * 2);
73
- const bBytes = publicKey.slice(QuarkDashKeyExchange.N * 2);
74
- const a = QuarkDashKeyExchange.deserializePoly(aBytes);
75
- const b = QuarkDashKeyExchange.deserializePoly(bBytes);
76
- const sp = QuarkDashKeyExchange.smallPoly();
77
- const ep = QuarkDashKeyExchange.errorPoly();
78
- const uArr = QuarkDashKeyExchange.multiply(a, sp);
79
- for (let i = 0; i < QuarkDashKeyExchange.N; i++) {
80
- uArr[i] = (uArr[i] + ep[i]) % QuarkDashKeyExchange.Q;
81
- }
82
- const w = QuarkDashKeyExchange.multiply(b, sp);
83
- const rawSecret = QuarkDashKeyExchange.roundToBits(w);
84
- const ciphertext = QuarkDashKeyExchange.serializePoly(uArr);
85
- const sharedSecret = QuarkDashKeyExchange.hashSharedSecret(rawSecret, publicKey, ciphertext);
86
- return { ciphertext, sharedSecret };
87
- }
88
-
89
- /**
90
- * Decapsulate async
91
- * @param privateKey {Uint8Array} Private key buffer
92
- * @param peerPublicKey {Uint8Array} Peer public key
93
- * @param ciphertext {Uint8Array} Cipher text buffer
94
- * @returns {Promise<Uint8Array>} Buffer data
95
- * TODO: GPU Calculations
96
- */
97
- public async decapsulate(privateKey: Uint8Array, peerPublicKey: Uint8Array, ciphertext: Uint8Array): Promise<Uint8Array> {
98
- return this.decapsulateSync(privateKey, peerPublicKey, ciphertext);
99
- }
100
-
101
- /**
102
- * Decapsulate sync
103
- * @param privateKey {Uint8Array} Private key buffer
104
- * @param peerPublicKey{Uint8Array} Peer public key buffer
105
- * @param ciphertext {Uint8Array} Cipher text buffer
106
- * @returns {Uint8Array} Buffer data
107
- */
108
- public decapsulateSync(privateKey: Uint8Array, peerPublicKey: Uint8Array, ciphertext: Uint8Array): Uint8Array {
109
- const s = QuarkDashKeyExchange.deserializePoly(privateKey);
110
- const u = QuarkDashKeyExchange.deserializePoly(ciphertext);
111
- const w = QuarkDashKeyExchange.multiply(u, s);
112
- const rawSecret = QuarkDashKeyExchange.roundToBits(w);
113
- return QuarkDashKeyExchange.hashSharedSecret(rawSecret, peerPublicKey, ciphertext);
114
- }
115
-
116
- /**
117
- * Get small polygon
118
- * @returns {bigint[]} Small polygon
119
- * @private
120
- */
121
- private static smallPoly(): bigint[] {
122
- const poly = new Array<bigint>(this.N);
123
- const bytesNeeded = Math.ceil(this.N * 2 / 8);
124
- const randomBytes = QuarkDashUtils.randomBytes(bytesNeeded);
125
- for (let i = 0; i < this.N; i++) {
126
- const byteIdx = Math.floor(i * 2 / 8);
127
- const bitShift = (i * 2) % 8;
128
- const val = (randomBytes[byteIdx] >> bitShift) & 0x03; // 0..3
129
- if (val === 0) poly[i] = -1n;
130
- else if (val === 1) poly[i] = 0n;
131
- else if (val === 2) poly[i] = 1n;
132
- else {
133
- poly[i] = 1n;
134
- }
135
- }
136
- return poly;
137
- }
138
-
139
- /**
140
- * Uniform polygon
141
- * @returns {bigint[]}
142
- * @private
143
- */
144
- private static uniformPoly(): bigint[] {
145
- const poly = new Array<bigint>(this.N);
146
- const bytes = QuarkDashUtils.randomBytes(this.N * 2);
147
- for (let i = 0; i < this.N; i++) {
148
- const val = (bytes[2 * i] | (bytes[2 * i + 1] << 8)) % Number(this.Q);
149
- poly[i] = BigInt(val);
150
- }
151
- return poly;
152
- }
153
-
154
- /**
155
- * Error polygon
156
- * @private
157
- */
158
- private static errorPoly(): bigint[] {
159
- const poly = new Array<bigint>(this.N);
160
- const SIGMA = 3.19;
161
- for (let i = 0; i < this.N; i++) {
162
- let sum = 0;
163
- const randBytes = QuarkDashUtils.randomBytes(12);
164
- for (let j = 0; j < 12; j++) {
165
- sum += randBytes[j];
166
- }
167
- // Центрируем и масштабируем к [-6,6]
168
- const centered = (sum / 255) - 6;
169
- const error = Math.floor(centered * SIGMA);
170
- poly[i] = BigInt(Math.max(-Number(this.Q), Math.min(Number(this.Q) - 1, error)));
171
- }
172
- return poly;
173
- }
174
-
175
- /**
176
- * NTT Operation
177
- * @param poly {bigint[]} Polygon
178
- * @private
179
- */
180
- private static ntt(poly: bigint[]): bigint[] {
181
- const res = [...poly];
182
- let len = 2;
183
- while (len <= this.N) {
184
- const wlen = this.powMod(this.ROOT, BigInt(this.N / len), this.Q);
185
- for (let i = 0; i < this.N; i += len) {
186
- let w = 1n;
187
- for (let j = 0; j < len / 2; j++) {
188
- const u = res[i + j];
189
- const v = (res[i + j + len / 2] * w) % this.Q;
190
- res[i + j] = (u + v) % this.Q;
191
- res[i + j + len / 2] = (u - v + this.Q) % this.Q;
192
- w = (w * wlen) % this.Q;
193
- }
194
- }
195
- len <<= 1;
196
- }
197
- return res;
198
- }
199
-
200
- /**
201
- * Inverse NTT
202
- * @param poly {bigint[]} Polygon
203
- * @private
204
- */
205
- private static invNTT(poly: bigint[]): bigint[] {
206
- const res = [...poly];
207
- let len = this.N;
208
- while (len >= 2) {
209
- const wlen = this.powMod(this.ROOT, BigInt(this.N / len), this.Q);
210
- for (let i = 0; i < this.N; i += len) {
211
- let w = 1n;
212
- for (let j = 0; j < len / 2; j++) {
213
- const u = res[i + j];
214
- const v = res[i + j + len / 2];
215
- res[i + j] = (u + v) % this.Q;
216
- res[i + j + len / 2] = ((u - v + this.Q) * w) % this.Q;
217
- w = (w * wlen) % this.Q;
218
- }
219
- }
220
- len >>= 1;
221
- }
222
- for (let i = 0; i < this.N; i++) {
223
- res[i] = (res[i] * this.INV_N) % this.Q;
224
- }
225
- return res;
226
- }
227
-
228
- /**
229
- * Multiply
230
- * @param a {bigint[]} Polygon
231
- * @param b {bigint[]} Polygon
232
- * @returns {bigint[]} Multiplied polygons
233
- */
234
- public static multiply(a: bigint[], b: bigint[]): bigint[] {
235
- const aNTT = this.ntt(a);
236
- const bNTT = this.ntt(b);
237
- const prod = new Array<bigint>(this.N);
238
- for (let i = 0; i < this.N; i++) {
239
- prod[i] = (aNTT[i] * bNTT[i]) % this.Q;
240
- }
241
- return this.invNTT(prod);
242
- }
243
-
244
- /**
245
- * Serialize polygon
246
- * @param poly {bigint[]} Polygon
247
- * @returns {Uint8Array} Polygon buffer
248
- * @private
249
- */
250
- private static serializePoly(poly: bigint[]): Uint8Array {
251
- const bytes = new Uint8Array(this.N * 2);
252
- for (let i = 0; i < this.N; i++) {
253
- const val = Number(poly[i]);
254
- bytes[2 * i] = val & 0xFF;
255
- bytes[2 * i + 1] = (val >> 8) & 0xFF;
256
- }
257
- return bytes;
258
- }
259
-
260
- /**
261
- * Deserialize Polygon
262
- * @param bytes {Uint8Array} Polygon buffer
263
- * @returns {bigint[]} Polygon
264
- * @private
265
- */
266
- private static deserializePoly(bytes: Uint8Array): bigint[] {
267
- const poly = new Array<bigint>(this.N);
268
- for (let i = 0; i < this.N; i++) {
269
- const val = bytes[2 * i] | (bytes[2 * i + 1] << 8);
270
- poly[i] = BigInt(val);
271
- }
272
- return poly;
273
- }
274
-
275
- /**
276
- * Round to bits
277
- * @param poly {bigint[]} Polygon
278
- * @returns {Uint8Array} rounded buffer
279
- * @private
280
- */
281
- private static roundToBits(poly: bigint[]): Uint8Array {
282
- const result = new Uint8Array(32);
283
- for (let i = 0; i < this.N; i++) {
284
- const bit = (Number(poly[i]) > Number(this.Q) / 2) ? 1 : 0;
285
- if (bit) result[i >> 3] |= (1 << (i & 7));
286
- }
287
- return result;
288
- }
289
-
290
- /**
291
- * Modular exponentiation
292
- * @param base {bigint} Base
293
- * @param exp {bigint} exponential
294
- * @param mod {bigint} module
295
- * @returns {bigint} Result of modular exponentiation
296
- * @private
297
- */
298
- private static powMod(base: bigint, exp: bigint, mod: bigint): bigint {
299
- let result = 1n;
300
- let b = base % mod;
301
- let e = exp;
302
- while (e > 0n) {
303
- if (e & 1n) result = (result * b) % mod;
304
- b = (b * b) % mod;
305
- e >>= 1n;
306
- }
307
- return result;
308
- }
309
-
310
- /**
311
- * Modular inverse
312
- * @param a {bigint}
313
- * @param m {bigint}
314
- * @returns {bigint} Inversion result
315
- * @private
316
- */
317
- private static modInverse(a: bigint, m: bigint): bigint {
318
- let [old_r, r] = [a, m];
319
- let [old_s, s] = [1n, 0n];
320
- while (r !== 0n) {
321
- const q = old_r / r;
322
- [old_r, r] = [r, old_r - q * r];
323
- [old_s, s] = [s, old_s - q * s];
324
- }
325
- return (old_s % m + m) % m;
326
- }
327
-
328
- /**
329
- * Hash shared secret
330
- * @param ss {Uint8Array} Shared secret buffer
331
- * @param publicKey {Uint8Array} Public key buffer
332
- * @param ciphertext {Uint8Array} Cipher text buffer
333
- * @returns {Uint8Array} Shared secret hash
334
- * @private
335
- */
336
- private static hashSharedSecret(ss: Uint8Array, publicKey: Uint8Array, ciphertext: Uint8Array): Uint8Array {
337
- const data = QuarkDashUtils.concatBytes(ss, publicKey, ciphertext);
338
- return SHA256.hash(data, true) as Uint8Array;
339
- }
340
- }
package/src/sha.ts DELETED
@@ -1,265 +0,0 @@
1
- /**
2
- * QuarkDash SHA Implementations
3
- *
4
- * @git https://github.com/devsdaddy/quarkdash
5
- * @version 1.0.0
6
- * @author Elijah Rastorguev
7
- * @build 1000
8
- * @website https://dev.to/devsdaddy
9
- */
10
- /**
11
- * SHA-256 Implementation
12
- */
13
- export class SHA256 {
14
- // Constants
15
- private static readonly K: number[] = [
16
- 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
17
- 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
18
- 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
19
- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
20
- 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
21
- 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
22
- 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
23
- 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
24
- ];
25
-
26
- /**
27
- * Right Rotate
28
- * @param value {number} Value to rotate
29
- * @param amount {number} Rotate amount
30
- * @private
31
- */
32
- private static rightRotate(value: number, amount: number): number {
33
- return (value >>> amount) | (value << (32 - amount));
34
- }
35
-
36
- /**
37
- * Get SHA256 Hash
38
- * @param data {string|Uint8Array} Raw string or bytes array
39
- * @param returnBytes {boolean} Returns HEX String or Uint8Array
40
- * @returns {string|Uint8Array} HEX String or Uint8Array
41
- */
42
- static hash(data: string | Uint8Array, returnBytes : boolean = false): string | Uint8Array {
43
- const msgBytes = typeof data === 'string' ? new TextEncoder().encode(data) : data;
44
-
45
- // Initial hash values
46
- let h0 = 0x6a09e667;
47
- let h1 = 0xbb67ae85;
48
- let h2 = 0x3c6ef372;
49
- let h3 = 0xa54ff53a;
50
- let h4 = 0x510e527f;
51
- let h5 = 0x9b05688c;
52
- let h6 = 0x1f83d9ab;
53
- let h7 = 0x5be0cd19;
54
-
55
- // Pre-processing: padding
56
- const ml = msgBytes.length * 8;
57
- const padded = new Uint8Array(((ml + 64 + 511) & ~511) / 8);
58
- padded.set(msgBytes);
59
- padded[msgBytes.length] = 0x80;
60
-
61
- // Append length
62
- const dv = new DataView(padded.buffer);
63
- dv.setUint32(padded.length - 8, 0, false);
64
- dv.setUint32(padded.length - 4, ml, false);
65
-
66
- // Process chunks
67
- for (let i = 0; i < padded.length; i += 64) {
68
- const w = new Array(64).fill(0);
69
-
70
- // Prepare message schedule
71
- for (let j = 0; j < 16; j++) {
72
- w[j] = dv.getUint32(i + j * 4, false);
73
- }
74
-
75
- for (let j = 16; j < 64; j++) {
76
- const s0 = this.rightRotate(w[j - 15], 7) ^ this.rightRotate(w[j - 15], 18) ^ (w[j - 15] >>> 3);
77
- const s1 = this.rightRotate(w[j - 2], 17) ^ this.rightRotate(w[j - 2], 19) ^ (w[j - 2] >>> 10);
78
- w[j] = (w[j - 16] + s0 + w[j - 7] + s1) >>> 0;
79
- }
80
-
81
- // Initialize working variables
82
- let a = h0;
83
- let b = h1;
84
- let c = h2;
85
- let d = h3;
86
- let e = h4;
87
- let f = h5;
88
- let g = h6;
89
- let h = h7;
90
-
91
- // Main loop
92
- for (let j = 0; j < 64; j++) {
93
- const S1 = this.rightRotate(e, 6) ^ this.rightRotate(e, 11) ^ this.rightRotate(e, 25);
94
- const ch = (e & f) ^ ((~e) & g);
95
- const temp1 = (h + S1 + ch + this.K[j] + w[j]) >>> 0;
96
- const S0 = this.rightRotate(a, 2) ^ this.rightRotate(a, 13) ^ this.rightRotate(a, 22);
97
- const maj = (a & b) ^ (a & c) ^ (b & c);
98
- const temp2 = (S0 + maj) >>> 0;
99
-
100
- h = g;
101
- g = f;
102
- f = e;
103
- e = (d + temp1) >>> 0;
104
- d = c;
105
- c = b;
106
- b = a;
107
- a = (temp1 + temp2) >>> 0;
108
- }
109
-
110
- // Update hash values
111
- h0 = (h0 + a) >>> 0;
112
- h1 = (h1 + b) >>> 0;
113
- h2 = (h2 + c) >>> 0;
114
- h3 = (h3 + d) >>> 0;
115
- h4 = (h4 + e) >>> 0;
116
- h5 = (h5 + f) >>> 0;
117
- h6 = (h6 + g) >>> 0;
118
- h7 = (h7 + h) >>> 0;
119
- }
120
-
121
- // Produce final hash
122
- const result = new Uint8Array(32);
123
- const resultView = new DataView(result.buffer);
124
- resultView.setUint32(0, h0, false);
125
- resultView.setUint32(4, h1, false);
126
- resultView.setUint32(8, h2, false);
127
- resultView.setUint32(12, h3, false);
128
- resultView.setUint32(16, h4, false);
129
- resultView.setUint32(20, h5, false);
130
- resultView.setUint32(24, h6, false);
131
- resultView.setUint32(28, h7, false);
132
-
133
- return (returnBytes) ? result as Uint8Array : Array.from(result).map(b => b.toString(16).padStart(2, '0')).join('') as string;
134
- }
135
- }
136
-
137
- /**
138
- * SHA-512 Implementation
139
- */
140
- export class SHA512 {
141
- // Constants
142
- private static readonly K: bigint[] = [
143
- 0x428a2f98d728ae22n, 0x7137449123ef65cdn, 0xb5c0fbcfec4d3b2fn, 0xe9b5dba58189dbbcn,
144
- 0x3956c25bf348b538n, 0x59f111f1b605d019n, 0x923f82a4af194f9bn, 0xab1c5ed5da6d8118n,
145
- 0xd807aa98a3030242n, 0x12835b0145706fben, 0x243185be4ee4b28cn, 0x550c7dc3d5ffb4e2n,
146
- 0x72be5d74f27b896fn, 0x80deb1fe3b1696b1n, 0x9bdc06a725c71235n, 0xc19bf174cf692694n,
147
- 0xe49b69c19ef14ad2n, 0xefbe4786384f25e3n, 0x0fc19dc68b8cd5b5n, 0x240ca1cc77ac9c65n,
148
- 0x2de92c6f592b0275n, 0x4a7484aa6ea6e483n, 0x5cb0a9dcbd41fbd4n, 0x76f988da831153b5n,
149
- 0x983e5152ee66dfabn, 0xa831c66d2db43210n, 0xb00327c898fb213fn, 0xbf597fc7beef0ee4n,
150
- 0xc6e00bf33da88fc2n, 0xd5a79147930aa725n, 0x06ca6351e003826fn, 0x142929670a0e6e70n,
151
- 0x27b70a8546d22ffcn, 0x2e1b21385c26c926n, 0x4d2c6dfc5ac42aedn, 0x53380d139d95b3dfn,
152
- 0x650a73548baf63den, 0x766a0abb3c77b2a8n, 0x81c2c92e47edaee6n, 0x92722c851482353bn,
153
- 0xa2bfe8a14cf10364n, 0xa81a664bbc423001n, 0xc24b8b70d0f89791n, 0xc76c51a30654be30n,
154
- 0xd192e819d6ef5218n, 0xd69906245565a910n, 0xf40e35855771202an, 0x106aa07032bbd1b8n,
155
- 0x19a4c116b8d2d0c8n, 0x1e376c085141ab53n, 0x2748774cdf8eeb99n, 0x34b0bcb5e19b48a8n,
156
- 0x391c0cb3c5c95a63n, 0x4ed8aa4ae3418acbn, 0x5b9cca4f7763e373n, 0x682e6ff3d6b2b8a3n,
157
- 0x748f82ee5defb2fcn, 0x78a5636f43172f60n, 0x84c87814a1f0ab72n, 0x8cc702081a6439ecn,
158
- 0x90befffa23631e28n, 0xa4506cebde82bde9n, 0xbef9a3f7b2c67915n, 0xc67178f2e372532bn,
159
- 0xca273eceea26619cn, 0xd186b8c721c0c207n, 0xeada7dd6cde0eb1en, 0xf57d4f7fee6ed178n,
160
- 0x06f067aa72176fban, 0x0a637dc5a2c898a6n, 0x113f9804bef90daen, 0x1b710b35131c471bn,
161
- 0x28db77f523047d84n, 0x32caab7b40c72493n, 0x3c9ebe0a15c9bebcn, 0x431d67c49c100d4cn,
162
- 0x4cc5d4becb3e42b6n, 0x597f299cfc657e2an, 0x5fcb6fab3ad6faecn, 0x6c44198c4a475817n
163
- ];
164
-
165
- /**
166
- * Bitint right rotate
167
- * @param value {number} Value
168
- * @param amount {number} Rotate amount
169
- * @private
170
- */
171
- private static rightRotate(value: bigint, amount: number): bigint {
172
- return (value >> BigInt(amount)) | (value << (64n - BigInt(amount)));
173
- }
174
-
175
- /**
176
- * Get SHA512 Hash
177
- * @param data {string|Uint8Array} Raw string or bytes array
178
- * @param returnBytes {boolean} Returns HEX String or Uint8Array
179
- * @returns {string|Uint8Array} HEX String or Uint8Array
180
- */
181
- static hash(data: string | Uint8Array, returnBytes : boolean = false): string | Uint8Array {
182
- const msgBytes = typeof data === "string" ? new TextEncoder().encode(data) : data;
183
-
184
- // Initial hash values
185
- let h0 = 0x6a09e667f3bcc908n;
186
- let h1 = 0xbb67ae8584caa73bn;
187
- let h2 = 0x3c6ef372fe94f82bn;
188
- let h3 = 0xa54ff53a5f1d36f1n;
189
- let h4 = 0x510e527fade682d1n;
190
- let h5 = 0x9b05688c2b3e6c1fn;
191
- let h6 = 0x1f83d9abfb41bd6bn;
192
- let h7 = 0x5be0cd19137e2179n;
193
-
194
- const ml = BigInt(msgBytes.length * 8);
195
- const padded = new Uint8Array((((Number(ml) + 128 + 1023) & ~1023) / 8));
196
- padded.set(msgBytes);
197
- padded[msgBytes.length] = 0x80;
198
-
199
- const dv = new DataView(padded.buffer);
200
- dv.setBigUint64(padded.length - 8, ml, false);
201
-
202
- for (let i = 0; i < padded.length; i += 128) {
203
- const w = new Array(80).fill(0n);
204
-
205
- for (let j = 0; j < 16; j++) {
206
- w[j] = dv.getBigUint64(i + j * 8, false);
207
- }
208
-
209
- for (let j = 16; j < 80; j++) {
210
- const s0 = this.rightRotate(w[j - 15], 1) ^ this.rightRotate(w[j - 15], 8) ^ (w[j - 15] >> 7n);
211
- const s1 = this.rightRotate(w[j - 2], 19) ^ this.rightRotate(w[j - 2], 61) ^ (w[j - 2] >> 6n);
212
- w[j] = (w[j - 16] + s0 + w[j - 7] + s1) & 0xffffffffffffffffn;
213
- }
214
-
215
- let a = h0;
216
- let b = h1;
217
- let c = h2;
218
- let d = h3;
219
- let e = h4;
220
- let f = h5;
221
- let g = h6;
222
- let h = h7;
223
-
224
- for (let j = 0; j < 80; j++) {
225
- const S1 = this.rightRotate(e, 14) ^ this.rightRotate(e, 18) ^ this.rightRotate(e, 41);
226
- const ch = (e & f) ^ ((~e) & g);
227
- const temp1 = (h + S1 + ch + this.K[j] + w[j]) & 0xffffffffffffffffn;
228
- const S0 = this.rightRotate(a, 28) ^ this.rightRotate(a, 34) ^ this.rightRotate(a, 39);
229
- const maj = (a & b) ^ (a & c) ^ (b & c);
230
- const temp2 = (S0 + maj) & 0xffffffffffffffffn;
231
-
232
- h = g;
233
- g = f;
234
- f = e;
235
- e = (d + temp1) & 0xffffffffffffffffn;
236
- d = c;
237
- c = b;
238
- b = a;
239
- a = (temp1 + temp2) & 0xffffffffffffffffn;
240
- }
241
-
242
- h0 = (h0 + a) & 0xffffffffffffffffn;
243
- h1 = (h1 + b) & 0xffffffffffffffffn;
244
- h2 = (h2 + c) & 0xffffffffffffffffn;
245
- h3 = (h3 + d) & 0xffffffffffffffffn;
246
- h4 = (h4 + e) & 0xffffffffffffffffn;
247
- h5 = (h5 + f) & 0xffffffffffffffffn;
248
- h6 = (h6 + g) & 0xffffffffffffffffn;
249
- h7 = (h7 + h) & 0xffffffffffffffffn;
250
- }
251
-
252
- const result = new Uint8Array(64);
253
- const resultView = new DataView(result.buffer);
254
- resultView.setBigUint64(0, h0, false);
255
- resultView.setBigUint64(8, h1, false);
256
- resultView.setBigUint64(16, h2, false);
257
- resultView.setBigUint64(24, h3, false);
258
- resultView.setBigUint64(32, h4, false);
259
- resultView.setBigUint64(40, h5, false);
260
- resultView.setBigUint64(48, h6, false);
261
- resultView.setBigUint64(56, h7, false);
262
-
263
- return (returnBytes) ? result as Uint8Array : Array.from(result).map(b => b.toString(16).padStart(2, '0')).join('') as string;
264
- }
265
- }