spd-lib 1.2.1 → 1.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.
- package/dist/index.js +415 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1 +1,415 @@
|
|
|
1
|
-
const fs
|
|
1
|
+
const fs = require('fs')
|
|
2
|
+
const zlib = require('zlib');
|
|
3
|
+
const sodium = require('libsodium-wrappers');
|
|
4
|
+
const crypto = require('crypto');
|
|
5
|
+
const argon2 = require('argon2');
|
|
6
|
+
|
|
7
|
+
class SPD {
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
constructor() {
|
|
11
|
+
this.data = [];
|
|
12
|
+
this.keyPair; // Generate a key pair for encryption/decryption
|
|
13
|
+
this.userKey;
|
|
14
|
+
this.salt;
|
|
15
|
+
this.init();
|
|
16
|
+
}
|
|
17
|
+
async init() {
|
|
18
|
+
await sodium.ready;
|
|
19
|
+
this.keyPair = sodium.crypto_kx_keypair()
|
|
20
|
+
}
|
|
21
|
+
async setPassKey(passcode){
|
|
22
|
+
await sodium.ready;
|
|
23
|
+
this.init()
|
|
24
|
+
const { pqcKey, salt } = await new SPD().convertPasscodeToPQCKey(passcode);
|
|
25
|
+
const userKey = pqcKey.publicKey;
|
|
26
|
+
this.userKey = userKey;
|
|
27
|
+
this.salt = salt;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async migrateFromFile(spdPath,passcode)
|
|
31
|
+
{
|
|
32
|
+
|
|
33
|
+
const spd = await SPD.loadFromFile(spdPath,passcode)
|
|
34
|
+
const extractData_OLD = async ()=>{
|
|
35
|
+
return new Promise(async(res,rej)=>{
|
|
36
|
+
try{
|
|
37
|
+
await sodium.ready;
|
|
38
|
+
let extractedFiles = {};
|
|
39
|
+
this.data.forEach(async dat => {
|
|
40
|
+
try{
|
|
41
|
+
const decryptedData = sodium.crypto_secretbox_open_easy(dat.data, dat.nonce, this.userKey);
|
|
42
|
+
const decompressedData = zlib.inflateSync(decryptedData,{level:9});
|
|
43
|
+
const dt = decompressedData.toString('utf8');
|
|
44
|
+
extractedFiles[dat.dataName] = await this.CSTI(dt, dat.dataType);
|
|
45
|
+
}catch{
|
|
46
|
+
rej()
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
res(extractedFiles);
|
|
50
|
+
}catch{
|
|
51
|
+
rej()
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
spd.extractData_OLD = extractData_OLD
|
|
56
|
+
const _data = await spd.extractData_OLD()
|
|
57
|
+
const n_spd = new SPD()
|
|
58
|
+
await n_spd.setPassKey(passcode)
|
|
59
|
+
for(let key in _data)
|
|
60
|
+
{
|
|
61
|
+
await n_spd.addData(key,_data[key])
|
|
62
|
+
}
|
|
63
|
+
n_spd.saveToFile(spdPath)
|
|
64
|
+
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async migrateFromString(spdData,passcode)
|
|
68
|
+
{
|
|
69
|
+
const spd = await SPD.loadFromString(spdData,passcode)
|
|
70
|
+
const extractData_OLD = async ()=>{
|
|
71
|
+
return new Promise(async(res,rej)=>{
|
|
72
|
+
try{
|
|
73
|
+
await sodium.ready;
|
|
74
|
+
let extractedFiles = {};
|
|
75
|
+
this.data.forEach(async dat => {
|
|
76
|
+
try{
|
|
77
|
+
const decryptedData = sodium.crypto_secretbox_open_easy(dat.data, dat.nonce, this.userKey);
|
|
78
|
+
const decompressedData = zlib.inflateSync(decryptedData,{level:9});
|
|
79
|
+
const dt = decompressedData.toString('utf8');
|
|
80
|
+
extractedFiles[dat.dataName] = await this.CSTI(dt, dat.dataType);
|
|
81
|
+
}catch{
|
|
82
|
+
rej()
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
res(extractedFiles);
|
|
86
|
+
}catch{
|
|
87
|
+
rej()
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
spd.extractData_OLD = extractData_OLD
|
|
92
|
+
const _data = await spd.extractData_OLD()
|
|
93
|
+
const n_spd = new SPD()
|
|
94
|
+
await n_spd.setPassKey(passcode)
|
|
95
|
+
for(let key in _data)
|
|
96
|
+
{
|
|
97
|
+
await n_spd.addData(key,_data[key])
|
|
98
|
+
}
|
|
99
|
+
return n_spd.saveData()
|
|
100
|
+
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async addData(name, data) {
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
const dmap = await this.CITS(data)
|
|
107
|
+
await sodium.ready;
|
|
108
|
+
const dat = Buffer.from(dmap[0]);
|
|
109
|
+
const compressedData = zlib.deflateSync(dat,{
|
|
110
|
+
level:9
|
|
111
|
+
});
|
|
112
|
+
const nonce = sodium.randombytes_buf(sodium.crypto_box_NONCEBYTES);
|
|
113
|
+
const encryptedData = sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(
|
|
114
|
+
compressedData,
|
|
115
|
+
null,
|
|
116
|
+
null,
|
|
117
|
+
nonce,
|
|
118
|
+
this.userKey
|
|
119
|
+
)
|
|
120
|
+
//sodium.crypto_secretbox_easy(compressedData, nonce, this.userKey);
|
|
121
|
+
const hash = crypto.createHash('sha3-512').update(encryptedData).digest('hex');
|
|
122
|
+
|
|
123
|
+
this.data.push({ dataName: name, nonce: Array.from(nonce), data: Array.from(encryptedData), hash, dataType: dmap[1] });
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
saveToFile(outputPath) {
|
|
127
|
+
if (!outputPath || typeof outputPath !== 'string' || !outputPath.trim() || !this.salt || !(this.salt instanceof Uint8Array) || this.salt.length !== 16) {
|
|
128
|
+
throw new Error('Invalid output path or salt.');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const spdData = JSON.stringify({ data: this.data, salt: Array.from(this.salt) });
|
|
132
|
+
const compressedSpdData = zlib.deflateSync(spdData,{
|
|
133
|
+
level:9
|
|
134
|
+
});
|
|
135
|
+
fs.writeFileSync(outputPath, compressedSpdData, { mode: 0o600 });
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
static async loadFromFile(spdPath, passcode) {
|
|
140
|
+
return new Promise(async (res,rej)=>{
|
|
141
|
+
try{
|
|
142
|
+
if (!spdPath || typeof spdPath !== 'string' || !spdPath.trim() || !passcode || typeof passcode !== 'string' || !passcode.trim()) {
|
|
143
|
+
rej(new Error('Invalid SPD path or passcode.'));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
await sodium.ready;
|
|
147
|
+
|
|
148
|
+
const compressedSpdData = fs.readFileSync(spdPath);
|
|
149
|
+
const spdData = zlib.inflateSync(compressedSpdData,{level:9}).toString('utf8');
|
|
150
|
+
|
|
151
|
+
const { data, salt } = JSON.parse(spdData);
|
|
152
|
+
|
|
153
|
+
const { pqcKey } = await new SPD().convertPasscodeToPQCKeySalted(passcode, new Uint8Array(salt));
|
|
154
|
+
const pbk = pqcKey.publicKey;
|
|
155
|
+
const spd = new SPD();
|
|
156
|
+
spd.userKey = pbk;
|
|
157
|
+
spd.keyPair = {
|
|
158
|
+
publicKey: pbk.publicKey
|
|
159
|
+
};
|
|
160
|
+
spd.data = data.map(dat => ({
|
|
161
|
+
dataName: dat.dataName,
|
|
162
|
+
nonce: Buffer.from(dat.nonce),
|
|
163
|
+
data: Buffer.from(dat.data),
|
|
164
|
+
hash: dat.hash,
|
|
165
|
+
dataType: dat.dataType
|
|
166
|
+
}));
|
|
167
|
+
spd.data.forEach(dat => {
|
|
168
|
+
//const calculatedHash = crypto.createHash('sha256').update(Buffer.from(dat.data)).digest('hex');
|
|
169
|
+
const calculatedHash = crypto.createHash('sha3-512').update(Buffer.from(dat.data)).digest('hex');
|
|
170
|
+
|
|
171
|
+
if (calculatedHash !== dat.hash) {
|
|
172
|
+
rej(new Error(`Data integrity check failed for ${dat.dataName}`));
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
res(spd);
|
|
176
|
+
}catch{
|
|
177
|
+
rej()
|
|
178
|
+
}
|
|
179
|
+
})
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async extractData() {
|
|
183
|
+
return new Promise(async(res,rej)=>{
|
|
184
|
+
try{
|
|
185
|
+
await sodium.ready;
|
|
186
|
+
let extractedFiles = {};
|
|
187
|
+
this.data.forEach(async dat => {
|
|
188
|
+
try{
|
|
189
|
+
const decryptedData = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(null,dat.data,null, dat.nonce, this.userKey);
|
|
190
|
+
const decompressedData = zlib.inflateSync(decryptedData,{level:9});
|
|
191
|
+
|
|
192
|
+
const dt = decompressedData.toString('utf8');
|
|
193
|
+
extractedFiles[dat.dataName] = await this.CSTI(dt, dat.dataType);
|
|
194
|
+
}catch{
|
|
195
|
+
rej()
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
res(extractedFiles);
|
|
199
|
+
}catch{
|
|
200
|
+
rej()
|
|
201
|
+
}
|
|
202
|
+
})
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
static async derivePBK(passcode, salt) {
|
|
208
|
+
if (!passcode || typeof passcode !== 'string' || !passcode.trim() || !salt || !(salt instanceof Uint8Array) || salt.length !== 16) {
|
|
209
|
+
throw new Error('Invalid passcode or salt.');
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return new Promise(async(resolve, reject) => {
|
|
213
|
+
argon2.hash(passcode, { salt:Buffer.from(salt), type: argon2.argon2id }).then((data)=>{
|
|
214
|
+
|
|
215
|
+
resolve({ pbk: data, salt: salt });
|
|
216
|
+
}).catch((err)=>{
|
|
217
|
+
console.log(err)
|
|
218
|
+
reject(err);
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
saveData() {
|
|
225
|
+
const spdData = JSON.stringify({ data: this.data, salt: Array.from(this.salt) });
|
|
226
|
+
const compressedSpdData = zlib.deflateSync(spdData,{level:9});
|
|
227
|
+
return compressedSpdData;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
static async loadFromString(spdData, passcode) {
|
|
231
|
+
return new Promise(async (res,rej)=>{
|
|
232
|
+
try{
|
|
233
|
+
if (!spdData || typeof spdData !== 'string' || !spdData.trim() || !passcode || typeof passcode !== 'string' || !passcode.trim()) {
|
|
234
|
+
rej(new Error('Invalid SPD path or passcode.'));
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
await sodium.ready;
|
|
238
|
+
const spdDataBuffer = Buffer.from(spdData, 'base64');
|
|
239
|
+
const spdData2 = zlib.inflateSync(spdDataBuffer,{level:9}).toString('utf8');
|
|
240
|
+
const { data, salt } = JSON.parse(spdData2);
|
|
241
|
+
const { pqcKey } = await new SPD().convertPasscodeToPQCKeySalted(passcode, new Uint8Array(salt));
|
|
242
|
+
const pbk = pqcKey.publicKey;
|
|
243
|
+
const spd = new SPD();
|
|
244
|
+
spd.userKey = pbk;
|
|
245
|
+
spd.keyPair = {
|
|
246
|
+
publicKey: pbk.publicKey
|
|
247
|
+
};
|
|
248
|
+
spd.data = data.map(dat => ({
|
|
249
|
+
dataName: dat.dataName,
|
|
250
|
+
nonce: Buffer.from(dat.nonce),
|
|
251
|
+
data: Buffer.from(dat.data),
|
|
252
|
+
hash: dat.hash,
|
|
253
|
+
dataType: dat.dataType
|
|
254
|
+
}));
|
|
255
|
+
spd.data.forEach(dat => {
|
|
256
|
+
const calculatedHash = crypto.createHash('sha3-512').update(Buffer.from(dat.data)).digest('hex');
|
|
257
|
+
if (calculatedHash !== dat.hash) {
|
|
258
|
+
rej(new Error(`Data integrity check failed for ${dat.dataName}`));
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
res(spd);
|
|
262
|
+
}catch{
|
|
263
|
+
rej()
|
|
264
|
+
}
|
|
265
|
+
})
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async convertPasscodeToPQCKeySalted(passcode, salt) {
|
|
269
|
+
if (!passcode || typeof passcode !== 'string' || !passcode.trim() || passcode.length < 8 || !salt || !(salt instanceof Uint8Array) || salt.length !== 16) {
|
|
270
|
+
throw new Error('Invalid passcode or salt.');
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const { pbk } = await SPD.derivePBK(passcode, salt);
|
|
274
|
+
|
|
275
|
+
await sodium.ready;
|
|
276
|
+
|
|
277
|
+
const keyPair = sodium.crypto_kx_seed_keypair(pbk.slice(0, sodium.crypto_kx_SEEDBYTES));
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
return { pqcKey: { publicKey: keyPair.publicKey }, salt };
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
async convertPasscodeToPQCKey(passcode) {
|
|
284
|
+
if (!passcode || typeof passcode !== 'string' || !passcode.trim() || passcode.length < 8) {
|
|
285
|
+
throw new Error('Invalid passcode.');
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const { pbk, salt } = await SPD.derivePBK(passcode, crypto.getRandomValues(new Uint8Array(16)));
|
|
289
|
+
|
|
290
|
+
await sodium.ready;
|
|
291
|
+
const keyPair = sodium.crypto_kx_seed_keypair(pbk.slice(0, sodium.crypto_kx_SEEDBYTES));
|
|
292
|
+
|
|
293
|
+
return { pqcKey: { publicKey: keyPair.publicKey }, salt };
|
|
294
|
+
}
|
|
295
|
+
async TDT(data) {
|
|
296
|
+
const classTypeMap = {
|
|
297
|
+
'[object Array]': 'Array',
|
|
298
|
+
'[object Uint8Array]': 'Uint8Array',
|
|
299
|
+
'[object Uint16Array]': 'Uint16Array',
|
|
300
|
+
'[object Uint32Array]': 'Uint32Array',
|
|
301
|
+
'[object BigInt64Array]': 'BigInt64Array',
|
|
302
|
+
'[object BigUint64Array]': 'BigUint64Array',
|
|
303
|
+
'[object Float32Array]': 'Float32Array',
|
|
304
|
+
'[object Float64Array]': 'Float64Array',
|
|
305
|
+
'[object Map]': 'Map',
|
|
306
|
+
'[object Set]': 'Set',
|
|
307
|
+
'[object Date]': 'Date',
|
|
308
|
+
'[object RegExp]': 'RegExp',
|
|
309
|
+
'[object Error]': 'Error'
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
const objectType = Object.prototype.toString.call(data);
|
|
313
|
+
const mappedType = classTypeMap[objectType];
|
|
314
|
+
return mappedType;
|
|
315
|
+
}
|
|
316
|
+
async isNumArr(dataType) {
|
|
317
|
+
if(dataType === 'Uint8Array' || dataType === 'Uint16Array' || dataType === 'Uint32Array' || dataType === 'BigInt64Array' || dataType === 'BigUint64Array' || dataType === 'Float32Array' || dataType === 'Float64Array'){
|
|
318
|
+
return true;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
async isSWM(dataType) {
|
|
322
|
+
if(dataType === 'Map' || dataType === 'Set' || dataType === 'WeakMap' || dataType === 'WeakSet'){
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
async isDRE(dataType) {
|
|
327
|
+
if(dataType === 'Date' || dataType === 'RegExp' || dataType === 'Error'){
|
|
328
|
+
return true;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
async CITS(data) {
|
|
332
|
+
const dataType = typeof data;
|
|
333
|
+
if (dataType === 'string' || dataType === 'number' || dataType === 'boolean') {
|
|
334
|
+
return [data.toString(), dataType];
|
|
335
|
+
}
|
|
336
|
+
if(typeof data === 'object'){
|
|
337
|
+
const type = await this.TDT(data)
|
|
338
|
+
if(type === 'Array'){
|
|
339
|
+
return [JSON.stringify(data),'Array'];
|
|
340
|
+
}
|
|
341
|
+
if(await this.isNumArr(type)){
|
|
342
|
+
return [JSON.stringify(Array.from(data)),type];
|
|
343
|
+
}
|
|
344
|
+
if(await this.isSWM(type)){
|
|
345
|
+
|
|
346
|
+
return [JSON.stringify([...data]),type];
|
|
347
|
+
}
|
|
348
|
+
if(await this.isDRE(type)){
|
|
349
|
+
return [data.toString(),type];
|
|
350
|
+
}
|
|
351
|
+
return [JSON.stringify(data), typeof data];
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
async CSTI(data,type) {
|
|
355
|
+
if(type === 'string') {
|
|
356
|
+
return data
|
|
357
|
+
}
|
|
358
|
+
if(type === 'number'){
|
|
359
|
+
return parseFloat(data);
|
|
360
|
+
}
|
|
361
|
+
if(type === 'boolean'){
|
|
362
|
+
return (data === 'true')&&(data !== 'false');
|
|
363
|
+
}
|
|
364
|
+
if(type === 'object' || type === 'Array'){
|
|
365
|
+
return JSON.parse(data);
|
|
366
|
+
}
|
|
367
|
+
if(type === 'Uint8Array'){
|
|
368
|
+
return new Uint8Array(JSON.parse(data));
|
|
369
|
+
}
|
|
370
|
+
if(type === 'Uint16Array'){
|
|
371
|
+
return new Uint16Array(JSON.parse(data));
|
|
372
|
+
}
|
|
373
|
+
if(type === 'Uint32Array'){
|
|
374
|
+
|
|
375
|
+
return new Uint32Array(JSON.parse(data));
|
|
376
|
+
}
|
|
377
|
+
if(type === 'BigInt64Array'){
|
|
378
|
+
return new BigInt64Array(JSON.parse(data));
|
|
379
|
+
}
|
|
380
|
+
if(type === 'BigUint64Array'){
|
|
381
|
+
return new BigUint64Array(JSON.parse(data));
|
|
382
|
+
}
|
|
383
|
+
if(type === 'Float32Array'){
|
|
384
|
+
return new Float32Array(JSON.parse(data));
|
|
385
|
+
}
|
|
386
|
+
if(type === 'Float64Array'){
|
|
387
|
+
return new Float64Array(JSON.parse(data));
|
|
388
|
+
}
|
|
389
|
+
if(type === 'Map'){
|
|
390
|
+
return new Map(JSON.parse(data));
|
|
391
|
+
}
|
|
392
|
+
if(type === 'Set'){
|
|
393
|
+
return new Set(JSON.parse(data));
|
|
394
|
+
}
|
|
395
|
+
if(type === 'WeakMap'){
|
|
396
|
+
return new WeakMap(JSON.parse(data));
|
|
397
|
+
}
|
|
398
|
+
if(type === 'WeakSet'){
|
|
399
|
+
return new WeakSet(JSON.parse(data));
|
|
400
|
+
}
|
|
401
|
+
if(type === 'Date'){
|
|
402
|
+
return new Date(data);
|
|
403
|
+
}
|
|
404
|
+
if(type === 'RegExp'){
|
|
405
|
+
return new RegExp(data);
|
|
406
|
+
}
|
|
407
|
+
if(type === 'Error'){
|
|
408
|
+
return new Error(data);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
module.exports = {
|
|
414
|
+
SPD,
|
|
415
|
+
};
|