quarkdash 1.0.7 → 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.
- package/README.md +4 -2
- package/dist/cjs/cipher/chacha.js +154 -0
- package/dist/cjs/cipher/chacha.js.map +1 -0
- package/dist/cjs/cipher/cipher.js +34 -0
- package/dist/cjs/cipher/cipher.js.map +1 -0
- package/dist/cjs/cipher/gimli.js +143 -0
- package/dist/cjs/cipher/gimli.js.map +1 -0
- package/dist/cjs/core/kdf.js.map +1 -0
- package/dist/cjs/{mac.js → core/mac.js} +37 -1
- package/dist/cjs/core/mac.js.map +1 -0
- package/dist/cjs/{types.js.map → core/types.js.map} +1 -1
- package/dist/cjs/{utils.js → core/utils.js} +25 -8
- package/dist/cjs/core/utils.js.map +1 -0
- package/dist/cjs/core/wasm_loader.js +100 -0
- package/dist/cjs/core/wasm_loader.js.map +1 -0
- package/dist/cjs/crypto.js +30 -13
- package/dist/cjs/crypto.js.map +1 -1
- package/dist/cjs/{sha.js → hash/sha.js} +3 -2
- package/dist/cjs/hash/sha.js.map +1 -0
- package/dist/cjs/hash/shake.js +318 -0
- package/dist/cjs/hash/shake.js.map +1 -0
- package/dist/cjs/index.js +16 -11
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/{ringlwe.js → session/baselwe.js} +246 -203
- package/dist/cjs/session/baselwe.js.map +1 -0
- package/dist/cjs/session/ringlwe.js +19 -0
- package/dist/cjs/session/ringlwe.js.map +1 -0
- package/dist/cjs/session/rringlwe.js +42 -0
- package/dist/cjs/session/rringlwe.js.map +1 -0
- package/dist/esm/cipher/chacha.js +150 -0
- package/dist/esm/cipher/chacha.js.map +1 -0
- package/dist/esm/cipher/cipher.js +30 -0
- package/dist/esm/cipher/cipher.js.map +1 -0
- package/dist/esm/cipher/gimli.js +139 -0
- package/dist/esm/cipher/gimli.js.map +1 -0
- package/dist/esm/core/kdf.js.map +1 -0
- package/dist/esm/core/mac.js +87 -0
- package/dist/esm/core/mac.js.map +1 -0
- package/dist/esm/{types.js.map → core/types.js.map} +1 -1
- package/dist/esm/{utils.js → core/utils.js} +24 -4
- package/dist/esm/core/utils.js.map +1 -0
- package/dist/esm/core/wasm_loader.js +64 -0
- package/dist/esm/core/wasm_loader.js.map +1 -0
- package/dist/esm/crypto.js +30 -13
- package/dist/esm/crypto.js.map +1 -1
- package/dist/esm/{sha.js → hash/sha.js} +3 -2
- package/dist/esm/hash/sha.js.map +1 -0
- package/dist/esm/hash/shake.js +311 -0
- package/dist/esm/hash/shake.js.map +1 -0
- package/dist/esm/index.js +16 -11
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/{ringlwe.js → session/baselwe.js} +244 -201
- package/dist/esm/session/baselwe.js.map +1 -0
- package/dist/esm/session/ringlwe.js +15 -0
- package/dist/esm/session/ringlwe.js.map +1 -0
- package/dist/esm/session/rringlwe.js +38 -0
- package/dist/esm/session/rringlwe.js.map +1 -0
- package/dist/shake.wasm +0 -0
- package/dist/types/cipher/chacha.d.ts +67 -0
- package/dist/types/cipher/cipher.d.ts +31 -0
- package/dist/types/cipher/gimli.d.ts +64 -0
- package/dist/types/{kdf.d.ts → core/kdf.d.ts} +3 -2
- package/dist/types/{mac.d.ts → core/mac.d.ts} +21 -2
- package/dist/types/{types.d.ts → core/types.d.ts} +5 -2
- package/dist/types/{utils.d.ts → core/utils.d.ts} +13 -0
- package/dist/types/core/wasm_loader.d.ts +5 -0
- package/dist/types/crypto.d.ts +8 -4
- package/dist/types/{sha.d.ts → hash/sha.d.ts} +3 -2
- package/dist/types/hash/shake.d.ts +85 -0
- package/dist/types/index.d.ts +15 -10
- package/dist/types/{ringlwe.d.ts → session/baselwe.d.ts} +87 -91
- package/dist/types/session/ringlwe.d.ts +21 -0
- package/dist/types/session/rringlwe.d.ts +27 -0
- package/package.json +13 -3
- package/wasm/shake.wasm +0 -0
- package/dist/cjs/cipher.js +0 -265
- package/dist/cjs/cipher.js.map +0 -1
- package/dist/cjs/kdf.js.map +0 -1
- package/dist/cjs/mac.js.map +0 -1
- package/dist/cjs/ringlwe.js.map +0 -1
- package/dist/cjs/sha.js.map +0 -1
- package/dist/cjs/shake.js +0 -159
- package/dist/cjs/shake.js.map +0 -1
- package/dist/cjs/utils.js.map +0 -1
- package/dist/esm/cipher.js +0 -259
- package/dist/esm/cipher.js.map +0 -1
- package/dist/esm/kdf.js.map +0 -1
- package/dist/esm/mac.js +0 -51
- package/dist/esm/mac.js.map +0 -1
- package/dist/esm/ringlwe.js.map +0 -1
- package/dist/esm/sha.js.map +0 -1
- package/dist/esm/shake.js +0 -155
- package/dist/esm/shake.js.map +0 -1
- package/dist/esm/utils.js.map +0 -1
- package/dist/types/cipher.d.ts +0 -153
- package/dist/types/shake.d.ts +0 -53
- package/img/cover.png +0 -0
- package/src/cipher.ts +0 -260
- package/src/crypto.ts +0 -306
- package/src/index.ts +0 -27
- package/src/kdf.ts +0 -64
- package/src/mac.ts +0 -65
- package/src/ringlwe.ts +0 -289
- package/src/sha.ts +0 -265
- package/src/shake.ts +0 -163
- package/src/types.ts +0 -75
- package/src/utils.ts +0 -290
- package/tsconfig.cjs.json +0 -7
- package/tsconfig.json +0 -18
- /package/dist/cjs/{kdf.js → core/kdf.js} +0 -0
- /package/dist/cjs/{types.js → core/types.js} +0 -0
- /package/dist/esm/{kdf.js → core/kdf.js} +0 -0
- /package/dist/esm/{types.js → core/types.js} +0 -0
package/src/ringlwe.ts
DELETED
|
@@ -1,289 +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 1000
|
|
8
|
-
* @website https://dev.to/devsdaddy
|
|
9
|
-
*/
|
|
10
|
-
/* Import Required Modules */
|
|
11
|
-
import {ICryptoEncapsulated, ICryptoKeyPair, IKeyExchange} from "./types";
|
|
12
|
-
import {QuarkDashUtils} from "./utils";
|
|
13
|
-
import {SHA256} from "./sha";
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Ring-LWE based key exchange implementation
|
|
17
|
-
*/
|
|
18
|
-
export class QuarkDashKeyExchange implements IKeyExchange {
|
|
19
|
-
// Ring-LWE Constants
|
|
20
|
-
private static readonly N = 256;
|
|
21
|
-
private static readonly Q = 7681n;
|
|
22
|
-
private static readonly ROOT = 7n;
|
|
23
|
-
private static readonly INV_N = this.modInverse(BigInt(this.N), this.Q);
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Get small polygon
|
|
27
|
-
* @returns {bigint[]} Small polygon
|
|
28
|
-
* @private
|
|
29
|
-
*/
|
|
30
|
-
private static smallPoly(): bigint[] {
|
|
31
|
-
const poly = new Array<bigint>(this.N);
|
|
32
|
-
for (let i=0;i<this.N;i++) {
|
|
33
|
-
const r = Math.random();
|
|
34
|
-
if (r<0.33) poly[i] = -1n;
|
|
35
|
-
else if (r<0.66) poly[i] = 0n;
|
|
36
|
-
else poly[i] = 1n;
|
|
37
|
-
}
|
|
38
|
-
return poly;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Uniform polygon
|
|
43
|
-
* @returns {bigint[]}
|
|
44
|
-
* @private
|
|
45
|
-
*/
|
|
46
|
-
private static uniformPoly(): bigint[] {
|
|
47
|
-
const poly = new Array<bigint>(this.N);
|
|
48
|
-
const bytes = QuarkDashUtils.randomBytes(this.N*2);
|
|
49
|
-
for(let i=0;i<this.N;i++) {
|
|
50
|
-
const val = (bytes[2*i] | (bytes[2*i+1]<<8)) % Number(this.Q);
|
|
51
|
-
poly[i] = BigInt(val);
|
|
52
|
-
}
|
|
53
|
-
return poly;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* NTT Operation
|
|
58
|
-
* @param poly {bigint[]} Polygon
|
|
59
|
-
* @private
|
|
60
|
-
*/
|
|
61
|
-
private static ntt(poly: bigint[]): bigint[] {
|
|
62
|
-
const res = [...poly];
|
|
63
|
-
let len = 2;
|
|
64
|
-
while(len <= this.N) {
|
|
65
|
-
const wlen = this.powMod(this.ROOT, BigInt(this.N/len), this.Q);
|
|
66
|
-
for(let i=0;i<this.N;i+=len){
|
|
67
|
-
let w=1n;
|
|
68
|
-
for(let j=0;j<len/2;j++){
|
|
69
|
-
const u=res[i+j];
|
|
70
|
-
const v=(res[i+j+len/2]*w)%this.Q;
|
|
71
|
-
res[i+j]=(u+v)%this.Q;
|
|
72
|
-
res[i+j+len/2]=(u-v+this.Q)%this.Q;
|
|
73
|
-
w=(w*wlen)%this.Q;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
len<<=1;
|
|
77
|
-
}
|
|
78
|
-
return res;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Inverse NTT
|
|
83
|
-
* @param poly {bigint[]} Polygon
|
|
84
|
-
* @private
|
|
85
|
-
*/
|
|
86
|
-
private static invNTT(poly: bigint[]): bigint[] {
|
|
87
|
-
const res = [...poly];
|
|
88
|
-
let len = this.N;
|
|
89
|
-
while(len >= 2){
|
|
90
|
-
const wlen = this.powMod(this.ROOT, BigInt(this.N/len), this.Q);
|
|
91
|
-
for(let i=0;i<this.N;i+=len){
|
|
92
|
-
let w=1n;
|
|
93
|
-
for(let j=0;j<len/2;j++){
|
|
94
|
-
const u=res[i+j];
|
|
95
|
-
const v=res[i+j+len/2];
|
|
96
|
-
res[i+j]=(u+v)%this.Q;
|
|
97
|
-
res[i+j+len/2]=((u-v+this.Q)*w)%this.Q;
|
|
98
|
-
w=(w*wlen)%this.Q;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
len>>=1;
|
|
102
|
-
}
|
|
103
|
-
for(let i=0;i<this.N;i++) res[i]=(res[i]*this.INV_N)%this.Q;
|
|
104
|
-
return res;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Multiply
|
|
109
|
-
* @param a {bigint[]} Polygon
|
|
110
|
-
* @param b {bigint[]} Polygon
|
|
111
|
-
* @returns {bigint[]} Multiplied polygons
|
|
112
|
-
*/
|
|
113
|
-
public static multiply(a: bigint[], b: bigint[]): bigint[] {
|
|
114
|
-
const aNTT = this.ntt(a);
|
|
115
|
-
const bNTT = this.ntt(b);
|
|
116
|
-
const prod = new Array<bigint>(this.N);
|
|
117
|
-
for(let i=0;i<this.N;i++) prod[i]=(aNTT[i]*bNTT[i])%this.Q;
|
|
118
|
-
return this.invNTT(prod);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Serialize polygon
|
|
123
|
-
* @param poly {bigint[]} Polygon
|
|
124
|
-
* @returns {Uint8Array} Polygon buffer
|
|
125
|
-
* @private
|
|
126
|
-
*/
|
|
127
|
-
private static serializePoly(poly: bigint[]): Uint8Array {
|
|
128
|
-
const bytes = new Uint8Array(this.N*2);
|
|
129
|
-
for(let i=0;i<this.N;i++) {
|
|
130
|
-
const val = Number(poly[i]);
|
|
131
|
-
bytes[2*i]=val&0xFF;
|
|
132
|
-
bytes[2*i+1]=(val>>8)&0xFF;
|
|
133
|
-
}
|
|
134
|
-
return bytes;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Deserialize Polygon
|
|
139
|
-
* @param bytes {Uint8Array} Polygon buffer
|
|
140
|
-
* @returns {bigint[]} Polygon
|
|
141
|
-
* @private
|
|
142
|
-
*/
|
|
143
|
-
private static deserializePoly(bytes: Uint8Array): bigint[] {
|
|
144
|
-
const poly = new Array<bigint>(this.N);
|
|
145
|
-
for(let i=0;i<this.N;i++) {
|
|
146
|
-
const val = bytes[2*i] | (bytes[2*i+1]<<8);
|
|
147
|
-
poly[i]=BigInt(val);
|
|
148
|
-
}
|
|
149
|
-
return poly;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Round to bits
|
|
154
|
-
* @param poly {bigint[]} Polygon
|
|
155
|
-
* @returns {Uint8Array} rounded buffer
|
|
156
|
-
* @private
|
|
157
|
-
*/
|
|
158
|
-
private static roundToBits(poly: bigint[]): Uint8Array {
|
|
159
|
-
const result = new Uint8Array(32);
|
|
160
|
-
for(let i=0;i<this.N;i++) {
|
|
161
|
-
const bit = (Number(poly[i]) > Number(this.Q)/2) ? 1 : 0;
|
|
162
|
-
if(bit) result[i>>3] |= (1<<(i&7));
|
|
163
|
-
}
|
|
164
|
-
return result;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Modular exponentiation
|
|
169
|
-
* @param base {bigint} Base
|
|
170
|
-
* @param exp {bigint} exponential
|
|
171
|
-
* @param mod {bigint} module
|
|
172
|
-
* @returns {bigint} Result of modular exponentiation
|
|
173
|
-
* @private
|
|
174
|
-
*/
|
|
175
|
-
private static powMod(base: bigint, exp: bigint, mod: bigint): bigint {
|
|
176
|
-
let result=1n, b=base%mod, e=exp;
|
|
177
|
-
while(e>0n){ if(e&1n) result=(result*b)%mod; b=(b*b)%mod; e>>=1n; }
|
|
178
|
-
return result;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Modular inverse
|
|
183
|
-
* @param a {bigint}
|
|
184
|
-
* @param m {bigint}
|
|
185
|
-
* @returns {bigint} Inversion result
|
|
186
|
-
* @private
|
|
187
|
-
*/
|
|
188
|
-
private static modInverse(a: bigint, m: bigint): bigint {
|
|
189
|
-
let [old_r,r]=[a,m], [old_s,s]=[1n,0n];
|
|
190
|
-
while(r!==0n){ const q=old_r/r; [old_r,r]=[r,old_r-q*r]; [old_s,s]=[s,old_s-q*s]; }
|
|
191
|
-
return (old_s%m+m)%m;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Generate crypto key pair async
|
|
196
|
-
* @returns {ICryptoKeyPair} Crypto key pair
|
|
197
|
-
* TODO: GPU Calculations
|
|
198
|
-
*/
|
|
199
|
-
public async generateKeyPair(): Promise<ICryptoKeyPair> {
|
|
200
|
-
return this.generateKeyPairSync();
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Generate crypto key pair sync
|
|
205
|
-
* @returns {ICryptoKeyPair} Crypto key pair
|
|
206
|
-
*/
|
|
207
|
-
public generateKeyPairSync(): ICryptoKeyPair {
|
|
208
|
-
const a = QuarkDashKeyExchange.uniformPoly();
|
|
209
|
-
const s = QuarkDashKeyExchange.smallPoly();
|
|
210
|
-
const e = QuarkDashKeyExchange.smallPoly();
|
|
211
|
-
const as = QuarkDashKeyExchange.multiply(a,s);
|
|
212
|
-
const b = new Array<bigint>(QuarkDashKeyExchange.N);
|
|
213
|
-
for(let i=0; i<QuarkDashKeyExchange.N; i++) b[i] = (as[i]+e[i]) % QuarkDashKeyExchange.Q;
|
|
214
|
-
const pub = QuarkDashUtils.concatBytes(QuarkDashKeyExchange.serializePoly(a), QuarkDashKeyExchange.serializePoly(b));
|
|
215
|
-
const priv = QuarkDashKeyExchange.serializePoly(s);
|
|
216
|
-
return { publicKey: pub, privateKey: priv };
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Encapsulate async
|
|
221
|
-
* @param publicKey {Uint8Array} Public key buffer
|
|
222
|
-
* @returns {Promise<ICryptoEncapsulated>} Encapsulated data
|
|
223
|
-
* TODO: GPU Calculations
|
|
224
|
-
*/
|
|
225
|
-
public async encapsulate(publicKey: Uint8Array): Promise<ICryptoEncapsulated> {
|
|
226
|
-
return this.encapsulateSync(publicKey);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* Encapsulate sync
|
|
231
|
-
* @param publicKey {Uint8Array} Public key buffer
|
|
232
|
-
* @returns {ICryptoEncapsulated} Encapsulated data
|
|
233
|
-
*/
|
|
234
|
-
public encapsulateSync(publicKey: Uint8Array): ICryptoEncapsulated {
|
|
235
|
-
const aBytes = publicKey.slice(0, QuarkDashKeyExchange.N * 2);
|
|
236
|
-
const bBytes = publicKey.slice(QuarkDashKeyExchange.N * 2);
|
|
237
|
-
const a = QuarkDashKeyExchange.deserializePoly(aBytes);
|
|
238
|
-
const b = QuarkDashKeyExchange.deserializePoly(bBytes);
|
|
239
|
-
const sp = QuarkDashKeyExchange.smallPoly();
|
|
240
|
-
const ep = QuarkDashKeyExchange.smallPoly();
|
|
241
|
-
const uArr = QuarkDashKeyExchange.multiply(a, sp);
|
|
242
|
-
for (let i = 0; i < QuarkDashKeyExchange.N; i++) uArr[i] = (uArr[i] + ep[i]) % QuarkDashKeyExchange.Q;
|
|
243
|
-
const w = QuarkDashKeyExchange.multiply(b, sp);
|
|
244
|
-
const rawSecret = QuarkDashKeyExchange.roundToBits(w);
|
|
245
|
-
const ciphertext = QuarkDashKeyExchange.serializePoly(uArr);
|
|
246
|
-
const sharedSecret = this.hashSharedSecret(rawSecret, publicKey, ciphertext);
|
|
247
|
-
return { ciphertext, sharedSecret };
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* Decapsulate async
|
|
252
|
-
* @param privateKey {Uint8Array} Private key buffer
|
|
253
|
-
* @param peerPublicKey {Uint8Array} Peer public key
|
|
254
|
-
* @param ciphertext {Uint8Array} Cipher text buffer
|
|
255
|
-
* @returns {Promise<Uint8Array>} Buffer data
|
|
256
|
-
* TODO: GPU Calculations
|
|
257
|
-
*/
|
|
258
|
-
public async decapsulate(privateKey: Uint8Array, peerPublicKey: Uint8Array, ciphertext: Uint8Array): Promise<Uint8Array> {
|
|
259
|
-
return this.decapsulateSync(privateKey, peerPublicKey, ciphertext);
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* Decapsulate sync
|
|
264
|
-
* @param privateKey {Uint8Array} Private key buffer
|
|
265
|
-
* @param peerPublicKey{Uint8Array} Peer public key buffer
|
|
266
|
-
* @param ciphertext {Uint8Array} Cipher text buffer
|
|
267
|
-
* @returns {Uint8Array} Buffer data
|
|
268
|
-
*/
|
|
269
|
-
public decapsulateSync(privateKey: Uint8Array, peerPublicKey: Uint8Array, ciphertext: Uint8Array): Uint8Array {
|
|
270
|
-
const s = QuarkDashKeyExchange.deserializePoly(privateKey);
|
|
271
|
-
const u = QuarkDashKeyExchange.deserializePoly(ciphertext);
|
|
272
|
-
const w = QuarkDashKeyExchange.multiply(u, s);
|
|
273
|
-
const rawSecret = QuarkDashKeyExchange.roundToBits(w);
|
|
274
|
-
return this.hashSharedSecret(rawSecret, peerPublicKey, ciphertext);
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* Hash shared secret
|
|
279
|
-
* @param ss {Uint8Array} Shared secret buffer
|
|
280
|
-
* @param publicKey {Uint8Array} Public key buffer
|
|
281
|
-
* @param ciphertext {Uint8Array} Cipher text buffer
|
|
282
|
-
* @returns {Uint8Array} Shared secret hash
|
|
283
|
-
* @private
|
|
284
|
-
*/
|
|
285
|
-
private hashSharedSecret(ss: Uint8Array, publicKey: Uint8Array, ciphertext: Uint8Array): Uint8Array {
|
|
286
|
-
const data = QuarkDashUtils.concatBytes(ss, publicKey, ciphertext);
|
|
287
|
-
return SHA256.hash(data, true) as Uint8Array;
|
|
288
|
-
}
|
|
289
|
-
}
|
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
|
-
}
|