spd-lib 1.1.2 → 1.1.4
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 +21 -10
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -51,8 +51,10 @@ this.salt = salt;
|
|
|
51
51
|
|
|
52
52
|
|
|
53
53
|
static async loadFromFile(spdPath, passcode) {
|
|
54
|
+
return new Promise(async (res,rej)=>{
|
|
55
|
+
try{
|
|
54
56
|
if (!spdPath || typeof spdPath !== 'string' || !spdPath.trim() || !passcode || typeof passcode !== 'string' || !passcode.trim()) {
|
|
55
|
-
|
|
57
|
+
rej(new Error('Invalid SPD path or passcode.'));
|
|
56
58
|
}
|
|
57
59
|
|
|
58
60
|
await sodium.ready;
|
|
@@ -77,10 +79,14 @@ this.salt = salt;
|
|
|
77
79
|
spd.data.forEach(dat => {
|
|
78
80
|
const calculatedHash = crypto.createHash('sha256').update(Buffer.from(dat.data)).digest('hex');
|
|
79
81
|
if (calculatedHash !== dat.hash) {
|
|
80
|
-
|
|
82
|
+
rej(new Error(`Data integrity check failed for ${dat.dataName}`));
|
|
81
83
|
}
|
|
82
84
|
});
|
|
83
|
-
|
|
85
|
+
res(spd);
|
|
86
|
+
}catch{
|
|
87
|
+
rej()
|
|
88
|
+
}
|
|
89
|
+
})
|
|
84
90
|
}
|
|
85
91
|
|
|
86
92
|
async extractData() {
|
|
@@ -120,15 +126,16 @@ this.salt = salt;
|
|
|
120
126
|
}
|
|
121
127
|
|
|
122
128
|
static async loadFromString(spdData, passcode) {
|
|
129
|
+
return new Promise(async (res,rej)=>{
|
|
130
|
+
try{
|
|
123
131
|
if (!spdData || typeof spdData !== 'string' || !spdData.trim() || !passcode || typeof passcode !== 'string' || !passcode.trim()) {
|
|
124
|
-
|
|
132
|
+
rej(new Error('Invalid SPD path or passcode.'));
|
|
125
133
|
}
|
|
126
134
|
|
|
127
135
|
await sodium.ready;
|
|
128
136
|
const spdDataBuffer = Buffer.from(spdData, 'base64');
|
|
129
137
|
const spdData2 = zlib.inflateSync(spdDataBuffer).toString('utf8');
|
|
130
138
|
const { data, salt } = JSON.parse(spdData2);
|
|
131
|
-
|
|
132
139
|
const { pqcKey } = await new SPD().convertPasscodeToPQCKeySalted(passcode, new Uint8Array(salt));
|
|
133
140
|
const pbk = pqcKey.publicKey;
|
|
134
141
|
const spd = new SPD();
|
|
@@ -146,10 +153,14 @@ this.salt = salt;
|
|
|
146
153
|
spd.data.forEach(dat => {
|
|
147
154
|
const calculatedHash = crypto.createHash('sha256').update(Buffer.from(dat.data)).digest('hex');
|
|
148
155
|
if (calculatedHash !== dat.hash) {
|
|
149
|
-
|
|
156
|
+
rej(new Error(`Data integrity check failed for ${dat.dataName}`));
|
|
150
157
|
}
|
|
151
158
|
});
|
|
152
|
-
|
|
159
|
+
res(spd);
|
|
160
|
+
}catch{
|
|
161
|
+
rej()
|
|
162
|
+
}
|
|
163
|
+
})
|
|
153
164
|
}
|
|
154
165
|
|
|
155
166
|
async convertPasscodeToPQCKeySalted(passcode, salt) {
|
|
@@ -291,6 +302,6 @@ this.salt = salt;
|
|
|
291
302
|
}
|
|
292
303
|
}
|
|
293
304
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
305
|
+
module.exports = {
|
|
306
|
+
SPD,
|
|
307
|
+
};
|