spd-lib 1.0.7 → 1.0.9
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/index.js +171 -17
- package/package.json +1 -1
- package/readme.md +114 -21
package/index.js
CHANGED
|
@@ -24,22 +24,19 @@ class SPD {
|
|
|
24
24
|
const userKey = pqcKey.publicKey;
|
|
25
25
|
this.userKey = userKey;
|
|
26
26
|
this.salt = salt;
|
|
27
|
-
this.pwd = true;
|
|
28
27
|
}
|
|
29
28
|
|
|
30
29
|
async addData(name, data) {
|
|
31
|
-
|
|
32
|
-
throw new Error('Invalid name or data. Both must be non-empty strings.');
|
|
33
|
-
}
|
|
34
|
-
|
|
30
|
+
|
|
35
31
|
|
|
32
|
+
const dmap = await this.CITS(data)
|
|
36
33
|
await sodium.ready;
|
|
37
|
-
const dat = Buffer.from(
|
|
34
|
+
const dat = Buffer.from(dmap[0]);
|
|
38
35
|
const compressedData = zlib.deflateSync(dat);
|
|
39
36
|
const nonce = sodium.randombytes_buf(sodium.crypto_box_NONCEBYTES);
|
|
40
37
|
const encryptedData = sodium.crypto_secretbox_easy(compressedData, nonce, this.userKey);
|
|
41
38
|
const hash = crypto.createHash('sha256').update(encryptedData).digest('hex');
|
|
42
|
-
this.data.push({ dataName: name, nonce: Array.from(nonce), data: Array.from(encryptedData), hash });
|
|
39
|
+
this.data.push({ dataName: name, nonce: Array.from(nonce), data: Array.from(encryptedData), hash, dataType: dmap[1] });
|
|
43
40
|
}
|
|
44
41
|
|
|
45
42
|
saveToFile(outputPath) {
|
|
@@ -48,8 +45,7 @@ this.pwd = true;
|
|
|
48
45
|
}
|
|
49
46
|
|
|
50
47
|
const keyPair = {
|
|
51
|
-
publicKey: Array.from(this.keyPair.publicKey)
|
|
52
|
-
privateKey: Array.from(this.keyPair.privateKey)
|
|
48
|
+
publicKey: Array.from(this.keyPair.publicKey)
|
|
53
49
|
};
|
|
54
50
|
const spdData = JSON.stringify({ keyPair, data: this.data, salt: Array.from(this.salt) });
|
|
55
51
|
const compressedSpdData = zlib.deflateSync(spdData);
|
|
@@ -72,14 +68,14 @@ this.pwd = true;
|
|
|
72
68
|
const spd = new SPD();
|
|
73
69
|
spd.userKey = pbk;
|
|
74
70
|
spd.keyPair = {
|
|
75
|
-
publicKey: Buffer.from(keyPair.publicKey)
|
|
76
|
-
privateKey: Buffer.from(keyPair.privateKey)
|
|
71
|
+
publicKey: Buffer.from(keyPair.publicKey)
|
|
77
72
|
};
|
|
78
73
|
spd.data = data.map(dat => ({
|
|
79
74
|
dataName: dat.dataName,
|
|
80
75
|
nonce: Buffer.from(dat.nonce),
|
|
81
76
|
data: Buffer.from(dat.data),
|
|
82
|
-
hash: dat.hash
|
|
77
|
+
hash: dat.hash,
|
|
78
|
+
dataType: dat.dataType
|
|
83
79
|
}));
|
|
84
80
|
spd.data.forEach(dat => {
|
|
85
81
|
const calculatedHash = crypto.createHash('sha256').update(Buffer.from(dat.data)).digest('hex');
|
|
@@ -93,10 +89,11 @@ this.pwd = true;
|
|
|
93
89
|
async extractData() {
|
|
94
90
|
await sodium.ready;
|
|
95
91
|
let extractedFiles = {};
|
|
96
|
-
this.data.forEach(dat => {
|
|
92
|
+
this.data.forEach(async dat => {
|
|
97
93
|
const decryptedData = sodium.crypto_secretbox_open_easy(dat.data, dat.nonce, this.userKey);
|
|
98
94
|
const decompressedData = zlib.inflateSync(decryptedData);
|
|
99
|
-
|
|
95
|
+
const dt = decompressedData.toString('utf8');
|
|
96
|
+
extractedFiles[dat.dataName] = await this.CSTI(dt, dat.dataType);
|
|
100
97
|
});
|
|
101
98
|
return extractedFiles;
|
|
102
99
|
}
|
|
@@ -119,6 +116,48 @@ this.pwd = true;
|
|
|
119
116
|
});
|
|
120
117
|
}
|
|
121
118
|
|
|
119
|
+
saveData() {
|
|
120
|
+
const keyPair = {
|
|
121
|
+
publicKey: Array.from(this.keyPair.publicKey)
|
|
122
|
+
};
|
|
123
|
+
const spdData = JSON.stringify({ keyPair, data: this.data, salt: Array.from(this.salt) });
|
|
124
|
+
const compressedSpdData = zlib.deflateSync(spdData);
|
|
125
|
+
return compressedSpdData;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async loadFromString(spdData, passcode) {
|
|
129
|
+
if (!spdData || typeof spdData !== 'string' || !spdData.trim() || !passcode || typeof passcode !== 'string' || !passcode.trim()) {
|
|
130
|
+
throw new Error('Invalid SPD path or passcode.');
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
await sodium.ready;
|
|
134
|
+
const spdDataBuffer = Buffer.from(spdData, 'base64');
|
|
135
|
+
const spdData2 = zlib.inflateSync(spdDataBuffer).toString('utf8');
|
|
136
|
+
const { keyPair, data, salt } = JSON.parse(spdData2);
|
|
137
|
+
|
|
138
|
+
const { pqcKey } = await new SPD().convertPasscodeToPQCKeySalted(passcode, new Uint8Array(salt));
|
|
139
|
+
const pbk = pqcKey.publicKey;
|
|
140
|
+
const spd = new SPD();
|
|
141
|
+
spd.userKey = pbk;
|
|
142
|
+
spd.keyPair = {
|
|
143
|
+
publicKey: Buffer.from(keyPair.publicKey)
|
|
144
|
+
};
|
|
145
|
+
spd.data = data.map(dat => ({
|
|
146
|
+
dataName: dat.dataName,
|
|
147
|
+
nonce: Buffer.from(dat.nonce),
|
|
148
|
+
data: Buffer.from(dat.data),
|
|
149
|
+
hash: dat.hash,
|
|
150
|
+
dataType: dat.dataType
|
|
151
|
+
}));
|
|
152
|
+
spd.data.forEach(dat => {
|
|
153
|
+
const calculatedHash = crypto.createHash('sha256').update(Buffer.from(dat.data)).digest('hex');
|
|
154
|
+
if (calculatedHash !== dat.hash) {
|
|
155
|
+
throw new Error(`Data integrity check failed for ${dat.dataName}`);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
return spd;
|
|
159
|
+
}
|
|
160
|
+
|
|
122
161
|
async convertPasscodeToPQCKeySalted(passcode, salt) {
|
|
123
162
|
if (!passcode || typeof passcode !== 'string' || !passcode.trim() || passcode.length < 8 || !salt || !(salt instanceof Uint8Array) || salt.length !== 16) {
|
|
124
163
|
throw new Error('Invalid passcode or salt.');
|
|
@@ -127,7 +166,7 @@ this.pwd = true;
|
|
|
127
166
|
const { pbk } = await SPD.derivePBK(passcode, salt);
|
|
128
167
|
await sodium.ready;
|
|
129
168
|
const keyPair = sodium.crypto_kx_seed_keypair(pbk.slice(0, sodium.crypto_kx_SEEDBYTES));
|
|
130
|
-
return { pqcKey: { publicKey: keyPair.publicKey
|
|
169
|
+
return { pqcKey: { publicKey: keyPair.publicKey }, salt };
|
|
131
170
|
}
|
|
132
171
|
|
|
133
172
|
async convertPasscodeToPQCKey(passcode) {
|
|
@@ -138,10 +177,125 @@ this.pwd = true;
|
|
|
138
177
|
const { pbk, salt } = await SPD.derivePBK(passcode, crypto.getRandomValues(new Uint8Array(16)));
|
|
139
178
|
await sodium.ready;
|
|
140
179
|
const keyPair = sodium.crypto_kx_seed_keypair(pbk.slice(0, sodium.crypto_kx_SEEDBYTES));
|
|
141
|
-
return { pqcKey: { publicKey: keyPair.publicKey
|
|
180
|
+
return { pqcKey: { publicKey: keyPair.publicKey }, salt };
|
|
142
181
|
}
|
|
143
|
-
|
|
182
|
+
async TDT(data) {
|
|
183
|
+
const classTypeMap = {
|
|
184
|
+
'[object Array]': 'Array',
|
|
185
|
+
'[object Uint8Array]': 'Uint8Array',
|
|
186
|
+
'[object Uint16Array]': 'Uint16Array',
|
|
187
|
+
'[object Uint32Array]': 'Uint32Array',
|
|
188
|
+
'[object BigInt64Array]': 'BigInt64Array',
|
|
189
|
+
'[object BigUint64Array]': 'BigUint64Array',
|
|
190
|
+
'[object Float32Array]': 'Float32Array',
|
|
191
|
+
'[object Float64Array]': 'Float64Array',
|
|
192
|
+
'[object Map]': 'Map',
|
|
193
|
+
'[object Set]': 'Set',
|
|
194
|
+
'[object Date]': 'Date',
|
|
195
|
+
'[object RegExp]': 'RegExp',
|
|
196
|
+
'[object Error]': 'Error'
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const objectType = Object.prototype.toString.call(data);
|
|
200
|
+
const mappedType = classTypeMap[objectType];
|
|
201
|
+
return mappedType;
|
|
202
|
+
}
|
|
203
|
+
async isNumArr(dataType) {
|
|
204
|
+
if(dataType === 'Uint8Array' || dataType === 'Uint16Array' || dataType === 'Uint32Array' || dataType === 'BigInt64Array' || dataType === 'BigUint64Array' || dataType === 'Float32Array' || dataType === 'Float64Array'){
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
async isSWM(dataType) {
|
|
209
|
+
if(dataType === 'Map' || dataType === 'Set' || dataType === 'WeakMap' || dataType === 'WeakSet'){
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
async isDRE(dataType) {
|
|
214
|
+
if(dataType === 'Date' || dataType === 'RegExp' || dataType === 'Error'){
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
async CITS(data) {
|
|
219
|
+
const dataType = typeof data;
|
|
220
|
+
if (dataType === 'string' || dataType === 'number' || dataType === 'boolean') {
|
|
221
|
+
return [data.toString(), dataType];
|
|
222
|
+
}
|
|
223
|
+
if(typeof data === 'object'){
|
|
224
|
+
const type = await this.TDT(data)
|
|
225
|
+
if(type === 'Array'){
|
|
226
|
+
return [JSON.stringify(data),'Array'];
|
|
227
|
+
}
|
|
228
|
+
if(await this.isNumArr(type)){
|
|
229
|
+
return [JSON.stringify(Array.from(data)),type];
|
|
230
|
+
}
|
|
231
|
+
if(await this.isSWM(type)){
|
|
232
|
+
|
|
233
|
+
return [JSON.stringify([...data]),type];
|
|
234
|
+
}
|
|
235
|
+
if(await this.isDRE(type)){
|
|
236
|
+
return [data.toString(),type];
|
|
237
|
+
}
|
|
238
|
+
return [JSON.stringify(data), typeof data];
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
async CSTI(data,type) {
|
|
242
|
+
if(type === 'string') {
|
|
243
|
+
return data
|
|
244
|
+
}
|
|
245
|
+
if(type === 'number'){
|
|
246
|
+
return parseFloat(data);
|
|
247
|
+
}
|
|
248
|
+
if(type === 'boolean'){
|
|
249
|
+
return (data === 'true')&&(data !== 'false');
|
|
250
|
+
}
|
|
251
|
+
if(type === 'object' || type === 'Array'){
|
|
252
|
+
return JSON.parse(data);
|
|
253
|
+
}
|
|
254
|
+
if(type === 'Uint8Array'){
|
|
255
|
+
return new Uint8Array(JSON.parse(data));
|
|
256
|
+
}
|
|
257
|
+
if(type === 'Uint16Array'){
|
|
258
|
+
return new Uint16Array(JSON.parse(data));
|
|
259
|
+
}
|
|
260
|
+
if(type === 'Uint32Array'){
|
|
144
261
|
|
|
262
|
+
return new Uint32Array(JSON.parse(data));
|
|
263
|
+
}
|
|
264
|
+
if(type === 'BigInt64Array'){
|
|
265
|
+
return new BigInt64Array(JSON.parse(data));
|
|
266
|
+
}
|
|
267
|
+
if(type === 'BigUint64Array'){
|
|
268
|
+
return new BigUint64Array(JSON.parse(data));
|
|
269
|
+
}
|
|
270
|
+
if(type === 'Float32Array'){
|
|
271
|
+
return new Float32Array(JSON.parse(data));
|
|
272
|
+
}
|
|
273
|
+
if(type === 'Float64Array'){
|
|
274
|
+
return new Float64Array(JSON.parse(data));
|
|
275
|
+
}
|
|
276
|
+
if(type === 'Map'){
|
|
277
|
+
return new Map(JSON.parse(data));
|
|
278
|
+
}
|
|
279
|
+
if(type === 'Set'){
|
|
280
|
+
return new Set(JSON.parse(data));
|
|
281
|
+
}
|
|
282
|
+
if(type === 'WeakMap'){
|
|
283
|
+
return new WeakMap(JSON.parse(data));
|
|
284
|
+
}
|
|
285
|
+
if(type === 'WeakSet'){
|
|
286
|
+
return new WeakSet(JSON.parse(data));
|
|
287
|
+
}
|
|
288
|
+
if(type === 'Date'){
|
|
289
|
+
return new Date(data);
|
|
290
|
+
}
|
|
291
|
+
if(type === 'RegExp'){
|
|
292
|
+
return new RegExp(data);
|
|
293
|
+
}
|
|
294
|
+
if(type === 'Error'){
|
|
295
|
+
return new Error(data);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
145
299
|
|
|
146
300
|
module.exports = {
|
|
147
301
|
SPD,
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,44 +1,137 @@
|
|
|
1
|
-
```markdown
|
|
2
1
|
# Secure Packaged Data (SPD)
|
|
3
2
|
|
|
4
|
-
Secure Packaged Data (SPD)
|
|
3
|
+
The Secure Packaged Data (SPD) module provides functionality to securely store and retrieve data using encryption, compression, and hashing. This README explains how to use the SPD class and its methods.
|
|
5
4
|
|
|
6
5
|
## Installation
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
First, ensure you have Node.js installed. Then, install the required packages:
|
|
9
8
|
|
|
10
|
-
```
|
|
9
|
+
```sh
|
|
11
10
|
npm install spd-lib
|
|
12
11
|
```
|
|
13
12
|
|
|
14
13
|
## Usage
|
|
15
14
|
|
|
15
|
+
Below are the primary functionalities of the SPD class.
|
|
16
|
+
|
|
17
|
+
### Initialization
|
|
18
|
+
|
|
19
|
+
Create an instance of the SPD class:
|
|
20
|
+
|
|
16
21
|
```javascript
|
|
17
22
|
const { SPD } = require('spd-lib');
|
|
18
23
|
|
|
19
|
-
|
|
24
|
+
const spd = new SPD();
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Setting Passcode
|
|
28
|
+
|
|
29
|
+
Set a passcode for encrypting the data:
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
await spd.setPassKey('your-passcode');
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Adding Data
|
|
36
|
+
|
|
37
|
+
Add data to the SPD instance:
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
await spd.addData('dataName', yourData);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Saving Data to File
|
|
44
|
+
|
|
45
|
+
Save the encrypted and compressed data to a file:
|
|
46
|
+
|
|
47
|
+
```javascript
|
|
48
|
+
spd.saveToFile('path/to/output.spd');
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Loading Data from File
|
|
52
|
+
|
|
53
|
+
Load the data from an SPD file:
|
|
54
|
+
|
|
55
|
+
```javascript
|
|
56
|
+
const loadedSPD = await SPD.loadFromFile('path/to/output.spd', 'your-passcode');
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Extracting Data
|
|
60
|
+
|
|
61
|
+
Extract and decrypt the data from the SPD instance:
|
|
62
|
+
|
|
63
|
+
```javascript
|
|
64
|
+
const extractedData = await loadedSPD.extractData();
|
|
65
|
+
console.log(extractedData);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Example
|
|
69
|
+
|
|
70
|
+
Here is a complete example of how to use the SPD module:
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
const { SPD } = require('./path/to/spd.js');
|
|
74
|
+
|
|
20
75
|
(async () => {
|
|
76
|
+
const spd = new SPD();
|
|
77
|
+
await spd.setPassKey('your-passcode');
|
|
21
78
|
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
79
|
+
const myData = {
|
|
80
|
+
name: "Alice",
|
|
81
|
+
age: 30,
|
|
82
|
+
isMember: true,
|
|
83
|
+
preferences: ["reading", "gaming"]
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
await spd.addData('userData', myData);
|
|
87
|
+
|
|
88
|
+
spd.saveToFile('path/to/output.spd');
|
|
89
|
+
|
|
90
|
+
const loadedSPD = await SPD.loadFromFile('path/to/output.spd', 'your-passcode');
|
|
91
|
+
const extractedData = await loadedSPD.extractData();
|
|
92
|
+
|
|
93
|
+
console.log(extractedData);
|
|
31
94
|
})();
|
|
32
95
|
```
|
|
33
96
|
|
|
34
|
-
##
|
|
97
|
+
## Methods
|
|
98
|
+
|
|
99
|
+
### `async setPassKey(passcode)`
|
|
100
|
+
|
|
101
|
+
Sets the passcode for encrypting and decrypting data.
|
|
102
|
+
|
|
103
|
+
- **Parameters**: `passcode` (string) - The passcode to be used.
|
|
104
|
+
|
|
105
|
+
### `async addData(name, data)`
|
|
106
|
+
|
|
107
|
+
Adds data to the SPD instance.
|
|
108
|
+
|
|
109
|
+
- **Parameters**:
|
|
110
|
+
- `name` (string) - The name of the data.
|
|
111
|
+
- `data` (any) - The data to be stored.
|
|
112
|
+
|
|
113
|
+
### `saveToFile(outputPath)`
|
|
114
|
+
|
|
115
|
+
Saves the encrypted and compressed data to a file.
|
|
116
|
+
|
|
117
|
+
- **Parameters**: `outputPath` (string) - The path to save the file.
|
|
118
|
+
|
|
119
|
+
### `static async loadFromFile(spdPath, passcode)`
|
|
120
|
+
|
|
121
|
+
Loads the SPD data from a file.
|
|
122
|
+
|
|
123
|
+
- **Parameters**:
|
|
124
|
+
- `spdPath` (string) - The path to the SPD file.
|
|
125
|
+
- `passcode` (string) - The passcode to decrypt the data.
|
|
126
|
+
|
|
127
|
+
- **Returns**: An instance of SPD with the loaded data.
|
|
128
|
+
|
|
129
|
+
### `async extractData()`
|
|
130
|
+
|
|
131
|
+
Extracts and decrypts the data from the SPD instance.
|
|
35
132
|
|
|
36
|
-
- **
|
|
37
|
-
- **Compression**: Data is compressed before encryption to reduce storage size.
|
|
38
|
-
- **Data Integrity**: Hashing techniques ensure the integrity of stored data.
|
|
39
|
-
- **Asynchronous Operations**: Utilizes asynchronous programming for file I/O and cryptographic operations.
|
|
133
|
+
- **Returns**: An object with the decrypted data.
|
|
40
134
|
|
|
41
135
|
## License
|
|
42
136
|
|
|
43
|
-
This project is licensed under the
|
|
44
|
-
```
|
|
137
|
+
This project is licensed under the MIT License.
|