spd-lib 1.0.8 → 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.
Files changed (2) hide show
  1. package/index.js +46 -6
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -45,8 +45,7 @@ this.salt = salt;
45
45
  }
46
46
 
47
47
  const keyPair = {
48
- publicKey: Array.from(this.keyPair.publicKey),
49
- privateKey: Array.from(this.keyPair.privateKey)
48
+ publicKey: Array.from(this.keyPair.publicKey)
50
49
  };
51
50
  const spdData = JSON.stringify({ keyPair, data: this.data, salt: Array.from(this.salt) });
52
51
  const compressedSpdData = zlib.deflateSync(spdData);
@@ -69,8 +68,7 @@ this.salt = salt;
69
68
  const spd = new SPD();
70
69
  spd.userKey = pbk;
71
70
  spd.keyPair = {
72
- publicKey: Buffer.from(keyPair.publicKey),
73
- privateKey: Buffer.from(keyPair.privateKey)
71
+ publicKey: Buffer.from(keyPair.publicKey)
74
72
  };
75
73
  spd.data = data.map(dat => ({
76
74
  dataName: dat.dataName,
@@ -118,6 +116,48 @@ this.salt = salt;
118
116
  });
119
117
  }
120
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
+ static 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
+
121
161
  async convertPasscodeToPQCKeySalted(passcode, salt) {
122
162
  if (!passcode || typeof passcode !== 'string' || !passcode.trim() || passcode.length < 8 || !salt || !(salt instanceof Uint8Array) || salt.length !== 16) {
123
163
  throw new Error('Invalid passcode or salt.');
@@ -126,7 +166,7 @@ this.salt = salt;
126
166
  const { pbk } = await SPD.derivePBK(passcode, salt);
127
167
  await sodium.ready;
128
168
  const keyPair = sodium.crypto_kx_seed_keypair(pbk.slice(0, sodium.crypto_kx_SEEDBYTES));
129
- return { pqcKey: { publicKey: keyPair.publicKey, privateKey: keyPair.privateKey }, salt };
169
+ return { pqcKey: { publicKey: keyPair.publicKey }, salt };
130
170
  }
131
171
 
132
172
  async convertPasscodeToPQCKey(passcode) {
@@ -137,7 +177,7 @@ this.salt = salt;
137
177
  const { pbk, salt } = await SPD.derivePBK(passcode, crypto.getRandomValues(new Uint8Array(16)));
138
178
  await sodium.ready;
139
179
  const keyPair = sodium.crypto_kx_seed_keypair(pbk.slice(0, sodium.crypto_kx_SEEDBYTES));
140
- return { pqcKey: { publicKey: keyPair.publicKey, privateKey: keyPair.privateKey }, salt };
180
+ return { pqcKey: { publicKey: keyPair.publicKey }, salt };
141
181
  }
142
182
  async TDT(data) {
143
183
  const classTypeMap = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spd-lib",
3
- "version": "1.0.8",
3
+ "version": "1.1.0",
4
4
  "description": "SPD or Secure Packaged Data is a compress PQC protected file format to store sensitive data localy",
5
5
  "main": "index.js",
6
6
  "scripts": {