spd-lib 1.0.4 → 1.0.6

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 (3) hide show
  1. package/index.js +27 -8
  2. package/package.json +2 -2
  3. package/readme.md +4 -6
package/index.js CHANGED
@@ -4,10 +4,27 @@ const sodium = require('libsodium-wrappers');
4
4
  const crypto = require('crypto');
5
5
 
6
6
  class SPD {
7
- constructor(userKey) {
7
+
8
+
9
+ constructor() {
8
10
  this.data = [];
9
- this.keyPair = sodium.crypto_box_keypair(); // Generate a key pair for encryption/decryption
10
- this.userKey = userKey; // Symmetric key provided by the user
11
+ this.keyPair; // Generate a key pair for encryption/decryption
12
+ this.userKey;
13
+ this.salt;
14
+ this.init();
15
+ }
16
+ async init() {
17
+ await sodium.ready;
18
+ this.keyPair = sodium.crypto_box_keypair()
19
+ }
20
+ async setPassKey(passcode){
21
+ await sodium.ready;
22
+ this.init()
23
+ const { pqcKey, salt } = await new SPD().convertPasscodeToPQCKey(passcode);
24
+ const userKey = pqcKey.publicKey;
25
+ this.userKey = userKey;
26
+ this.salt = salt;
27
+ this.pwd = true;
11
28
  }
12
29
 
13
30
  async addData(name, data) {
@@ -15,6 +32,7 @@ class SPD {
15
32
  throw new Error('Invalid name or data. Both must be non-empty strings.');
16
33
  }
17
34
 
35
+
18
36
  await sodium.ready;
19
37
  const dat = Buffer.from(data);
20
38
  const compressedData = zlib.deflateSync(dat);
@@ -24,8 +42,8 @@ class SPD {
24
42
  this.data.push({ dataName: name, nonce: Array.from(nonce), data: Array.from(encryptedData), hash });
25
43
  }
26
44
 
27
- saveToFile(outputPath, salt) {
28
- if (!outputPath || typeof outputPath !== 'string' || !outputPath.trim() || !salt || !(salt instanceof Uint8Array) || salt.length !== 16) {
45
+ saveToFile(outputPath) {
46
+ if (!outputPath || typeof outputPath !== 'string' || !outputPath.trim() || !this.salt || !(this.salt instanceof Uint8Array) || this.salt.length !== 16) {
29
47
  throw new Error('Invalid output path or salt.');
30
48
  }
31
49
 
@@ -33,7 +51,7 @@ class SPD {
33
51
  publicKey: Array.from(this.keyPair.publicKey),
34
52
  privateKey: Array.from(this.keyPair.privateKey)
35
53
  };
36
- const spdData = JSON.stringify({ keyPair, data: this.data, salt: Array.from(salt) });
54
+ const spdData = JSON.stringify({ keyPair, data: this.data, salt: Array.from(this.salt) });
37
55
  const compressedSpdData = zlib.deflateSync(spdData);
38
56
  fs.writeFileSync(outputPath, compressedSpdData, { mode: 0o600 });
39
57
  }
@@ -51,7 +69,8 @@ class SPD {
51
69
 
52
70
  const { pqcKey } = await new SPD().convertPasscodeToPQCKeySalted(passcode, new Uint8Array(salt));
53
71
  const pbk = pqcKey.publicKey;
54
- const spd = new SPD(pbk);
72
+ const spd = new SPD();
73
+ spd.userKey = pbk;
55
74
  spd.keyPair = {
56
75
  publicKey: Buffer.from(keyPair.publicKey),
57
76
  privateKey: Buffer.from(keyPair.privateKey)
@@ -123,7 +142,7 @@ class SPD {
123
142
  }
124
143
  }
125
144
 
145
+
126
146
  module.exports = {
127
147
  SPD,
128
- sodium,
129
148
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spd-lib",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "SPD or Secure Packaged Data is a compress PQC protected file format to sotre sensitive data localy",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -20,7 +20,7 @@
20
20
  ],
21
21
  "author": "ALS-OPSS",
22
22
  "license": "ISC",
23
- "devDependencies": {
23
+ "dependencies": {
24
24
  "crypto": "^1.0.1",
25
25
  "fs": "^0.0.1-security",
26
26
  "libsodium-wrappers": "^0.7.13"
package/readme.md CHANGED
@@ -14,19 +14,17 @@ npm install spd-lib
14
14
  ## Usage
15
15
 
16
16
  ```javascript
17
- const { SPD, sodium } = require('spd-lib');
17
+ const { SPD } = require('spd-lib');
18
18
 
19
19
  // Example usage
20
20
  (async () => {
21
- await sodium.ready;
22
21
 
23
22
  const passcode = 'your-secure-passcode';
24
- const { pqcKey, salt } = await new SPD().convertPasscodeToPQCKey(passcode);
25
- const userKey = pqcKey.publicKey; // For this example, use the public key as the symmetric key
26
- const spd = new SPD(userKey); // Create a new SPD object
23
+ const spd = new SPD(); // Create a new SPD object // Set the passcode to the SPD object
24
+ await spd.setPassKey(passcode); // Add data to the SPD object
27
25
  await spd.addData('settings', '{"theme":"dark"}'); // Add a file to the SPD object
28
26
  await spd.addData('tabs', '[{"title":"Home","url":"https://example.com"},{"title":"About","url":"https://example.com/about"}]'); // Add another file to the SPD object
29
- spd.saveToFile('output.spd', salt); // Save SPD file to disk with the salt
27
+ spd.saveToFile('output.spd'); // Save SPD file to disk with the salt
30
28
  const loadedSpd = await SPD.loadFromFile('output.spd', passcode); // Load SPD file with the passcode
31
29
  const extractedFiles = await loadedSpd.extractFiles(); // Extract files to memory
32
30
  console.log(extractedFiles); // Print extracted files to console