spd-lib 1.0.7 → 1.0.8
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 +125 -11
- 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) {
|
|
@@ -79,7 +76,8 @@ this.pwd = true;
|
|
|
79
76
|
dataName: dat.dataName,
|
|
80
77
|
nonce: Buffer.from(dat.nonce),
|
|
81
78
|
data: Buffer.from(dat.data),
|
|
82
|
-
hash: dat.hash
|
|
79
|
+
hash: dat.hash,
|
|
80
|
+
dataType: dat.dataType
|
|
83
81
|
}));
|
|
84
82
|
spd.data.forEach(dat => {
|
|
85
83
|
const calculatedHash = crypto.createHash('sha256').update(Buffer.from(dat.data)).digest('hex');
|
|
@@ -93,10 +91,11 @@ this.pwd = true;
|
|
|
93
91
|
async extractData() {
|
|
94
92
|
await sodium.ready;
|
|
95
93
|
let extractedFiles = {};
|
|
96
|
-
this.data.forEach(dat => {
|
|
94
|
+
this.data.forEach(async dat => {
|
|
97
95
|
const decryptedData = sodium.crypto_secretbox_open_easy(dat.data, dat.nonce, this.userKey);
|
|
98
96
|
const decompressedData = zlib.inflateSync(decryptedData);
|
|
99
|
-
|
|
97
|
+
const dt = decompressedData.toString('utf8');
|
|
98
|
+
extractedFiles[dat.dataName] = await this.CSTI(dt, dat.dataType);
|
|
100
99
|
});
|
|
101
100
|
return extractedFiles;
|
|
102
101
|
}
|
|
@@ -140,8 +139,123 @@ this.pwd = true;
|
|
|
140
139
|
const keyPair = sodium.crypto_kx_seed_keypair(pbk.slice(0, sodium.crypto_kx_SEEDBYTES));
|
|
141
140
|
return { pqcKey: { publicKey: keyPair.publicKey, privateKey: keyPair.privateKey }, salt };
|
|
142
141
|
}
|
|
143
|
-
|
|
142
|
+
async TDT(data) {
|
|
143
|
+
const classTypeMap = {
|
|
144
|
+
'[object Array]': 'Array',
|
|
145
|
+
'[object Uint8Array]': 'Uint8Array',
|
|
146
|
+
'[object Uint16Array]': 'Uint16Array',
|
|
147
|
+
'[object Uint32Array]': 'Uint32Array',
|
|
148
|
+
'[object BigInt64Array]': 'BigInt64Array',
|
|
149
|
+
'[object BigUint64Array]': 'BigUint64Array',
|
|
150
|
+
'[object Float32Array]': 'Float32Array',
|
|
151
|
+
'[object Float64Array]': 'Float64Array',
|
|
152
|
+
'[object Map]': 'Map',
|
|
153
|
+
'[object Set]': 'Set',
|
|
154
|
+
'[object Date]': 'Date',
|
|
155
|
+
'[object RegExp]': 'RegExp',
|
|
156
|
+
'[object Error]': 'Error'
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const objectType = Object.prototype.toString.call(data);
|
|
160
|
+
const mappedType = classTypeMap[objectType];
|
|
161
|
+
return mappedType;
|
|
162
|
+
}
|
|
163
|
+
async isNumArr(dataType) {
|
|
164
|
+
if(dataType === 'Uint8Array' || dataType === 'Uint16Array' || dataType === 'Uint32Array' || dataType === 'BigInt64Array' || dataType === 'BigUint64Array' || dataType === 'Float32Array' || dataType === 'Float64Array'){
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
async isSWM(dataType) {
|
|
169
|
+
if(dataType === 'Map' || dataType === 'Set' || dataType === 'WeakMap' || dataType === 'WeakSet'){
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
async isDRE(dataType) {
|
|
174
|
+
if(dataType === 'Date' || dataType === 'RegExp' || dataType === 'Error'){
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
async CITS(data) {
|
|
179
|
+
const dataType = typeof data;
|
|
180
|
+
if (dataType === 'string' || dataType === 'number' || dataType === 'boolean') {
|
|
181
|
+
return [data.toString(), dataType];
|
|
182
|
+
}
|
|
183
|
+
if(typeof data === 'object'){
|
|
184
|
+
const type = await this.TDT(data)
|
|
185
|
+
if(type === 'Array'){
|
|
186
|
+
return [JSON.stringify(data),'Array'];
|
|
187
|
+
}
|
|
188
|
+
if(await this.isNumArr(type)){
|
|
189
|
+
return [JSON.stringify(Array.from(data)),type];
|
|
190
|
+
}
|
|
191
|
+
if(await this.isSWM(type)){
|
|
192
|
+
|
|
193
|
+
return [JSON.stringify([...data]),type];
|
|
194
|
+
}
|
|
195
|
+
if(await this.isDRE(type)){
|
|
196
|
+
return [data.toString(),type];
|
|
197
|
+
}
|
|
198
|
+
return [JSON.stringify(data), typeof data];
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
async CSTI(data,type) {
|
|
202
|
+
if(type === 'string') {
|
|
203
|
+
return data
|
|
204
|
+
}
|
|
205
|
+
if(type === 'number'){
|
|
206
|
+
return parseFloat(data);
|
|
207
|
+
}
|
|
208
|
+
if(type === 'boolean'){
|
|
209
|
+
return (data === 'true')&&(data !== 'false');
|
|
210
|
+
}
|
|
211
|
+
if(type === 'object' || type === 'Array'){
|
|
212
|
+
return JSON.parse(data);
|
|
213
|
+
}
|
|
214
|
+
if(type === 'Uint8Array'){
|
|
215
|
+
return new Uint8Array(JSON.parse(data));
|
|
216
|
+
}
|
|
217
|
+
if(type === 'Uint16Array'){
|
|
218
|
+
return new Uint16Array(JSON.parse(data));
|
|
219
|
+
}
|
|
220
|
+
if(type === 'Uint32Array'){
|
|
144
221
|
|
|
222
|
+
return new Uint32Array(JSON.parse(data));
|
|
223
|
+
}
|
|
224
|
+
if(type === 'BigInt64Array'){
|
|
225
|
+
return new BigInt64Array(JSON.parse(data));
|
|
226
|
+
}
|
|
227
|
+
if(type === 'BigUint64Array'){
|
|
228
|
+
return new BigUint64Array(JSON.parse(data));
|
|
229
|
+
}
|
|
230
|
+
if(type === 'Float32Array'){
|
|
231
|
+
return new Float32Array(JSON.parse(data));
|
|
232
|
+
}
|
|
233
|
+
if(type === 'Float64Array'){
|
|
234
|
+
return new Float64Array(JSON.parse(data));
|
|
235
|
+
}
|
|
236
|
+
if(type === 'Map'){
|
|
237
|
+
return new Map(JSON.parse(data));
|
|
238
|
+
}
|
|
239
|
+
if(type === 'Set'){
|
|
240
|
+
return new Set(JSON.parse(data));
|
|
241
|
+
}
|
|
242
|
+
if(type === 'WeakMap'){
|
|
243
|
+
return new WeakMap(JSON.parse(data));
|
|
244
|
+
}
|
|
245
|
+
if(type === 'WeakSet'){
|
|
246
|
+
return new WeakSet(JSON.parse(data));
|
|
247
|
+
}
|
|
248
|
+
if(type === 'Date'){
|
|
249
|
+
return new Date(data);
|
|
250
|
+
}
|
|
251
|
+
if(type === 'RegExp'){
|
|
252
|
+
return new RegExp(data);
|
|
253
|
+
}
|
|
254
|
+
if(type === 'Error'){
|
|
255
|
+
return new Error(data);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
145
259
|
|
|
146
260
|
module.exports = {
|
|
147
261
|
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.
|