spd-lib 1.2.4 → 1.2.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.
- package/package.json +2 -4
- package/ai.py +0 -66
- package/fatman.spd +0 -0
- package/high-zip/index.js +0 -28
- package/high-zip/node_modules/.package-lock.json +0 -7
- package/high-zip/package-lock.json +0 -13
- package/high-zip/package.json +0 -13
- package/high-zip/tool.js +0 -78
- package/img.py +0 -44
- package/obfuscate.js +0 -131
- package/package/dist/index.js +0 -1
- package/package/package-lock.json +0 -1332
- package/package/package.json +0 -34
- package/readme.md +0 -137
- package/readme.md.gz +0 -0
- package/readme.md.gz.lo +0 -137
- package/src/index.js +0 -972
- package/t.py +0 -47
- package/test.js +0 -10
package/package.json
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "spd-lib",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.6",
|
|
4
4
|
"description": "SPD or Secure Packaged Data is a compress PQC protected file format to store sensitive data localy",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
-
"prepublishOnly": "node obfuscate.js",
|
|
9
|
-
"package": "cp package-lock.json ./package/package-lock.json ; cp package.json ./package/package.json ; cp -R node_modules ./package/node_modules ; mkdir -p ./package/dist/ ; cp ./dist/index.js ./package/dist/index.js"
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
10
8
|
},
|
|
11
9
|
"keywords": [
|
|
12
10
|
"security",
|
package/ai.py
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
from panda3d.core import Point3
|
|
2
|
-
from panda3d.gui import OnscreenText
|
|
3
|
-
from panda3d import core, render
|
|
4
|
-
from panda3d.core import CollisionNode, CollisionSphere, CollisionHandlerQueue, CollisionRay, CollisionTraverser
|
|
5
|
-
from panda3d.core import NodePath
|
|
6
|
-
import random
|
|
7
|
-
|
|
8
|
-
from panda3d import ShowBase
|
|
9
|
-
|
|
10
|
-
class MazeGame(ShowBase):
|
|
11
|
-
def __init__(self):
|
|
12
|
-
super().__init__()
|
|
13
|
-
|
|
14
|
-
self.maze_size = 10
|
|
15
|
-
self.create_maze()
|
|
16
|
-
|
|
17
|
-
# Set up the agent (player)
|
|
18
|
-
self.agent = self.loader.loadModel("models/box")
|
|
19
|
-
self.agent.setScale(0.5, 0.5, 0.5)
|
|
20
|
-
self.agent.setPos(1, 1, 0.25)
|
|
21
|
-
self.agent.reparentTo(self.render)
|
|
22
|
-
|
|
23
|
-
# Set up goal
|
|
24
|
-
self.goal = self.loader.loadModel("models/box")
|
|
25
|
-
self.goal.setScale(0.5, 0.5, 0.5)
|
|
26
|
-
self.goal.setPos(8, 8, 0.25)
|
|
27
|
-
self.goal.reparentTo(self.render)
|
|
28
|
-
|
|
29
|
-
# Camera settings
|
|
30
|
-
self.camera.setPos(5, -15, 10)
|
|
31
|
-
self.camera.lookAt(self.agent)
|
|
32
|
-
|
|
33
|
-
# Collision setup
|
|
34
|
-
self.collision_traverser = CollisionTraverser()
|
|
35
|
-
self.collision_handler = CollisionHandlerQueue()
|
|
36
|
-
|
|
37
|
-
# Set up agent's collision detection
|
|
38
|
-
self.collision_ray = CollisionRay()
|
|
39
|
-
self.collision_ray.setOrigin(0, 0, 1)
|
|
40
|
-
self.collision_ray.setDirection(0, 1, 0)
|
|
41
|
-
self.agent_collider = CollisionNode("agent_ray")
|
|
42
|
-
self.agent_collider.addSolid(self.collision_ray)
|
|
43
|
-
self.agent_collider.setFromCollideMask(core.CollisionNode.getDefaultCollideMask())
|
|
44
|
-
self.agent_collider.setIntoCollideMask(core.CollisionNode.getDefaultCollideMask())
|
|
45
|
-
self.agent_collider_path = self.agent.attachNewNode(self.agent_collider)
|
|
46
|
-
|
|
47
|
-
# Add collision traverser
|
|
48
|
-
self.collision_traverser.addCollider(self.agent_collider_path, self.collision_handler)
|
|
49
|
-
|
|
50
|
-
def create_maze(self):
|
|
51
|
-
"""Creates a simple maze of walls."""
|
|
52
|
-
for i in range(self.maze_size):
|
|
53
|
-
for j in range(self.maze_size):
|
|
54
|
-
if random.random() < 0.2:
|
|
55
|
-
wall = self.loader.loadModel("models/box")
|
|
56
|
-
wall.setScale(1, 1, 2)
|
|
57
|
-
wall.setPos(i, j, 1)
|
|
58
|
-
wall.reparentTo(self.render)
|
|
59
|
-
|
|
60
|
-
def check_collisions(self):
|
|
61
|
-
"""Checks for collisions between the agent and walls."""
|
|
62
|
-
self.collision_traverser.traverse(self.render)
|
|
63
|
-
return len(self.collision_handler.getEntries()) > 0
|
|
64
|
-
|
|
65
|
-
app = MazeGame()
|
|
66
|
-
app.run()
|
package/fatman.spd
DELETED
|
Binary file
|
package/high-zip/index.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
const tools = require("./tool")
|
|
2
|
-
const fs = require("fs")
|
|
3
|
-
async function compressFile(file_path="",dist_path="")
|
|
4
|
-
{
|
|
5
|
-
const read = fs.createReadStream(file_path)
|
|
6
|
-
const write = fs.createWriteStream(dist_path)
|
|
7
|
-
await tools.compress(read,write)
|
|
8
|
-
}
|
|
9
|
-
async function decompressFile(file_path="",dist_path="")
|
|
10
|
-
{
|
|
11
|
-
const read = fs.createReadStream(file_path)
|
|
12
|
-
const write = fs.createWriteStream(dist_path)
|
|
13
|
-
await tools.decompress(read,write)
|
|
14
|
-
}
|
|
15
|
-
async function compressBuffer(buf=new Buffer())
|
|
16
|
-
{
|
|
17
|
-
return await tools.compressBuffer(buf)
|
|
18
|
-
}
|
|
19
|
-
async function decompressBuffer(buf=new Buffer())
|
|
20
|
-
{
|
|
21
|
-
return await tools.decompressBuffer(buf)
|
|
22
|
-
}
|
|
23
|
-
module.exports = {
|
|
24
|
-
compressFile,
|
|
25
|
-
decompressFile,
|
|
26
|
-
compressBuffer,
|
|
27
|
-
decompressBuffer
|
|
28
|
-
}
|
package/high-zip/package.json
DELETED
package/high-zip/tool.js
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const zlib = require('zlib');
|
|
3
|
-
const { pipeline } = require('stream');
|
|
4
|
-
const { promisify } = require('util');
|
|
5
|
-
|
|
6
|
-
const streamPipeline = promisify(pipeline);
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Compress using Brotli at level 11 (generic mode).
|
|
10
|
-
* Supports file streams.
|
|
11
|
-
* @param {ReadableStream} inputStream
|
|
12
|
-
* @param {WritableStream} outputStream
|
|
13
|
-
* @returns {Promise<void>}
|
|
14
|
-
*/
|
|
15
|
-
async function compress(inputStream, outputStream) {
|
|
16
|
-
const brotli = zlib.createBrotliCompress({
|
|
17
|
-
params: {
|
|
18
|
-
[zlib.constants.BROTLI_PARAM_QUALITY]: 11,
|
|
19
|
-
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_GENERIC
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
await streamPipeline(inputStream, brotli, outputStream);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Decompress Brotli stream.
|
|
27
|
-
* @param {ReadableStream} inputStream
|
|
28
|
-
* @param {WritableStream} outputStream
|
|
29
|
-
* @returns {Promise<void>}
|
|
30
|
-
*/
|
|
31
|
-
async function decompress(inputStream, outputStream) {
|
|
32
|
-
const brotli = zlib.createBrotliDecompress();
|
|
33
|
-
await streamPipeline(inputStream, brotli, outputStream);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Compress a Buffer using Brotli level 11.
|
|
38
|
-
* @param {Buffer} buffer
|
|
39
|
-
* @returns {Promise<Buffer>}
|
|
40
|
-
*/
|
|
41
|
-
function compressBuffer(buffer) {
|
|
42
|
-
return new Promise((resolve, reject) => {
|
|
43
|
-
zlib.brotliCompress(
|
|
44
|
-
buffer,
|
|
45
|
-
{
|
|
46
|
-
params: {
|
|
47
|
-
[zlib.constants.BROTLI_PARAM_QUALITY]: 11,
|
|
48
|
-
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_GENERIC
|
|
49
|
-
}
|
|
50
|
-
},
|
|
51
|
-
(err, result) => {
|
|
52
|
-
if (err) return reject(err);
|
|
53
|
-
resolve(result);
|
|
54
|
-
}
|
|
55
|
-
);
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Decompress a Brotli-compressed Buffer.
|
|
61
|
-
* @param {Buffer} buffer
|
|
62
|
-
* @returns {Promise<Buffer>}
|
|
63
|
-
*/
|
|
64
|
-
function decompressBuffer(buffer) {
|
|
65
|
-
return new Promise((resolve, reject) => {
|
|
66
|
-
zlib.brotliDecompress(buffer, (err, result) => {
|
|
67
|
-
if (err) return reject(err);
|
|
68
|
-
resolve(result);
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
module.exports = {
|
|
74
|
-
compress,
|
|
75
|
-
decompress,
|
|
76
|
-
compressBuffer,
|
|
77
|
-
decompressBuffer
|
|
78
|
-
};
|
package/img.py
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
from PIL import Image
|
|
2
|
-
def smoothstep(edge0, edge1, x):
|
|
3
|
-
# Scale, bias and saturate x to 0..1 range
|
|
4
|
-
t = max(0.0, min(1.0, (x - edge0) / (edge1 - edge0)))
|
|
5
|
-
# Evaluate polynomial
|
|
6
|
-
return t * t * (3 - 2 * t)
|
|
7
|
-
def invert_image_colors(image_path, output_path,output2_path):
|
|
8
|
-
# Open the image
|
|
9
|
-
img = Image.open(image_path).convert("RGB")
|
|
10
|
-
width, height = img.size
|
|
11
|
-
|
|
12
|
-
# Create a new image for the output
|
|
13
|
-
inverted_img = Image.new("RGB", (width, height))
|
|
14
|
-
i = 1
|
|
15
|
-
br=1
|
|
16
|
-
filter = [
|
|
17
|
-
1,1,1,
|
|
18
|
-
0,0,0,
|
|
19
|
-
0,0,0
|
|
20
|
-
]
|
|
21
|
-
# Loop through every pixel
|
|
22
|
-
for x in range(width):
|
|
23
|
-
for y in range(height):
|
|
24
|
-
r, g, b = img.getpixel((x, y))
|
|
25
|
-
# Subtract each color component from 255
|
|
26
|
-
|
|
27
|
-
m = ((255 - (r)*i) + (255 - (g)*i) + (255 -( b)*i))/3
|
|
28
|
-
m2 = (r+g+b)/3
|
|
29
|
-
co = smoothstep(0,255,m+m2)*255
|
|
30
|
-
|
|
31
|
-
r2 = smoothstep(0,255,r)
|
|
32
|
-
b2 = smoothstep(0,255,b)
|
|
33
|
-
g2 = smoothstep(0,255,g)
|
|
34
|
-
p_pixel = (round(co*r2),round(co*g2),round(co*b2))
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
inverted_img.putpixel((x, y), p_pixel)
|
|
38
|
-
|
|
39
|
-
# Save the result
|
|
40
|
-
inverted_img.save(output_path)
|
|
41
|
-
print(f"Inverted image saved to {output_path}")
|
|
42
|
-
|
|
43
|
-
# Example usage
|
|
44
|
-
invert_image_colors("new.jpeg", "output.jpg","output2.jpg")
|
package/obfuscate.js
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const terser = require('terser');
|
|
4
|
-
|
|
5
|
-
const inputDir = path.join(__dirname, 'src'); // Your source code directory
|
|
6
|
-
const outputDir = path.join(__dirname, 'dist'); // The directory to output obfuscated code
|
|
7
|
-
|
|
8
|
-
// Create output directory if it doesn't exist
|
|
9
|
-
if (!fs.existsSync(outputDir)) {
|
|
10
|
-
fs.mkdirSync(outputDir);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// Read the source code from 'src' directory
|
|
14
|
-
fs.readdirSync(inputDir).forEach(async (file) => {
|
|
15
|
-
const filePath = path.join(inputDir, file);
|
|
16
|
-
const outputFilePath = path.join(outputDir, file);
|
|
17
|
-
|
|
18
|
-
// Read the file content
|
|
19
|
-
const fileContent = fs.readFileSync(filePath, 'utf8');
|
|
20
|
-
console.log(fileContent)
|
|
21
|
-
|
|
22
|
-
if (!fileContent) {
|
|
23
|
-
console.error(`Error: File ${file} is empty or couldn't be read.`);
|
|
24
|
-
return; // Skip this file if content is empty or not readable
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Log file being processed
|
|
28
|
-
console.log(`Processing file: ${file}`);
|
|
29
|
-
|
|
30
|
-
// Terser options for obfuscation
|
|
31
|
-
const terserOptions = {
|
|
32
|
-
mangle: {
|
|
33
|
-
// Ensure that class and function names are not mangled
|
|
34
|
-
reserved: ['SPD', 'setPassKey', 'addData', 'saveToFile', 'loadFromFile', 'extractData', 'saveData', 'loadFromString','setHash']
|
|
35
|
-
},
|
|
36
|
-
compress: {
|
|
37
|
-
// Enable compression to remove unnecessary code
|
|
38
|
-
drop_console: true // Optional: Remove console.log and similar calls
|
|
39
|
-
},
|
|
40
|
-
output: {
|
|
41
|
-
beautify: false,
|
|
42
|
-
comments: false
|
|
43
|
-
},
|
|
44
|
-
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
try {
|
|
48
|
-
// Obfuscate the content of the file with Terser
|
|
49
|
-
const result = await terser.minify(fileContent, {"compress":{
|
|
50
|
-
"booleans":true,
|
|
51
|
-
"arguments":true,
|
|
52
|
-
"arrows":true,
|
|
53
|
-
"collapse_vars":true,
|
|
54
|
-
"computed_props":true,
|
|
55
|
-
"booleans_as_integers":true,
|
|
56
|
-
"dead_code":true,
|
|
57
|
-
"drop_console":true,
|
|
58
|
-
"drop_debugger":true,
|
|
59
|
-
"if_return":true,
|
|
60
|
-
"join_vars":true,
|
|
61
|
-
"comparisons":true,
|
|
62
|
-
"conditionals":true,
|
|
63
|
-
"evaluate":true,
|
|
64
|
-
"expression":true,
|
|
65
|
-
"global_defs":true,
|
|
66
|
-
"defaults":true,
|
|
67
|
-
"hoist_funs":true,
|
|
68
|
-
"hoist_props":true,
|
|
69
|
-
"hoist_vars":true,
|
|
70
|
-
"directives":true,
|
|
71
|
-
"inline":true,
|
|
72
|
-
"module":true,
|
|
73
|
-
"loops":true,
|
|
74
|
-
"passes":true,
|
|
75
|
-
"typeofs":true,
|
|
76
|
-
"side_effects":true,
|
|
77
|
-
"switches":true,
|
|
78
|
-
"properties":true,
|
|
79
|
-
"reduce_funcs":true,
|
|
80
|
-
"reduce_vars":true,
|
|
81
|
-
"top_retain":false,
|
|
82
|
-
"toplevel":true,
|
|
83
|
-
"sequences":true,
|
|
84
|
-
"ie8":true,
|
|
85
|
-
"negate_iife":true,
|
|
86
|
-
},"mangle":{
|
|
87
|
-
"keep_classnames":true,
|
|
88
|
-
"keep_fnames":false,
|
|
89
|
-
"properties":false,
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
},"parse":{
|
|
93
|
-
"bare_returns":false,
|
|
94
|
-
"shebang":true
|
|
95
|
-
},
|
|
96
|
-
"format":{
|
|
97
|
-
"ascii_only":false,
|
|
98
|
-
"preserve_annotations":false,
|
|
99
|
-
"comments":false,
|
|
100
|
-
"keep_numbers":false,
|
|
101
|
-
"keep_quoted_props":false,
|
|
102
|
-
"wrap_func_args":true,
|
|
103
|
-
"shorthand":true,
|
|
104
|
-
"beautify":false
|
|
105
|
-
|
|
106
|
-
}});
|
|
107
|
-
console.log(result)
|
|
108
|
-
|
|
109
|
-
// Check if minification was successful and has a code
|
|
110
|
-
if (result.error) {
|
|
111
|
-
console.error(`Error minifying file ${file}:`, result.error);
|
|
112
|
-
return; // Skip writing this file if there was an error
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (!result.code) {
|
|
116
|
-
console.error(`No obfuscated code generated for ${file}.`);
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const obfuscatedCode = result.code;
|
|
121
|
-
|
|
122
|
-
// Write the obfuscated code to the 'dist' directory
|
|
123
|
-
fs.writeFileSync(outputFilePath, obfuscatedCode, 'utf8');
|
|
124
|
-
console.log(`Successfully obfuscated ${file}`);
|
|
125
|
-
|
|
126
|
-
} catch (err) {
|
|
127
|
-
console.error(`Error processing file ${file}:`, err);
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
console.log('Terser obfuscation complete.');
|
package/package/dist/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
const fs=require("fs"),zlib=require("zlib"),sodium=require("libsodium-wrappers"),crypto=require("crypto"),argon2=require("argon2");class SPD{constructor(){this.data=[],this.keyPair,this.userKey,this.salt,this.hash="sha3-512",this.init()}async init(){await sodium.ready,this.keyPair=sodium.crypto_kx_keypair()}async setPassKey(a){await sodium.ready,this.init();const{pqcKey:t,salt:r}=await(new SPD).convertPasscodeToPQCKey(a),e=t.publicKey;this.userKey=e,this.salt=r}setHash(a){this.hash=a}async migrateFromFile(a,t){const r=await SPD.loadFromFile(a,t);r.extractData_OLD=async()=>new Promise((async(a,t)=>{try{await sodium.ready;let r={};this.data.forEach((async a=>{try{const t=sodium.crypto_secretbox_open_easy(a.data,a.nonce,this.userKey),e=zlib.inflateSync(t,{level:9}).toString("utf8");r[a.dataName]=await this.CSTI(e,a.dataType)}catch{t()}})),a(r)}catch{t()}}));const e=await r.extractData_OLD(),i=new SPD;await i.setPassKey(t);for(let a in e)await i.addData(a,e[a]);i.saveToFile(a)}async migrateFromString(a,t){const r=await SPD.loadFromString(a,t);r.extractData_OLD=async()=>new Promise((async(a,t)=>{try{await sodium.ready;let r={};this.data.forEach((async a=>{try{const t=sodium.crypto_secretbox_open_easy(a.data,a.nonce,this.userKey),e=zlib.inflateSync(t,{level:9}).toString("utf8");r[a.dataName]=await this.CSTI(e,a.dataType)}catch{t()}})),a(r)}catch{t()}}));const e=await r.extractData_OLD(),i=new SPD;await i.setPassKey(t);for(let a in e)await i.addData(a,e[a]);return i.saveData()}async addData(a,t){const r=await this.CITS(t);await sodium.ready;const e=Buffer.from(r[0]),i=zlib.deflateSync(e,{level:9}),s=sodium.randombytes_buf(sodium.crypto_box_NONCEBYTES),n=sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(i,null,null,s,this.userKey),o=crypto.createHash(this.hash).update(n).digest("hex");this.data.push({dataName:a,nonce:Array.from(s),data:Array.from(n),hash:o,dataType:r[1]})}saveToFile(a){if(!(a&&"string"==typeof a&&a.trim()&&this.salt&&this.salt instanceof Uint8Array&&16===this.salt.length))throw new Error("Invalid output path or salt.");const t=JSON.stringify({data:this.data,salt:Array.from(this.salt)}),r=zlib.deflateSync(t,{level:9});fs.writeFileSync(a,r,{mode:384})}static async loadFromFile(a,t,r="sha3-512"){return new Promise((async(e,i)=>{try{a&&"string"==typeof a&&a.trim()&&t&&"string"==typeof t&&t.trim()||i(new Error("Invalid SPD path or passcode.")),await sodium.ready;const s=fs.readFileSync(a),n=zlib.inflateSync(s,{level:9}).toString("utf8"),{data:o,salt:y}=JSON.parse(n),{pqcKey:c}=await(new SPD).convertPasscodeToPQCKeySalted(t,new Uint8Array(y)),d=c.publicKey,l=new SPD;l.userKey=d,l.keyPair={publicKey:d.publicKey},l.data=o.map((a=>({dataName:a.dataName,nonce:Buffer.from(a.nonce),data:Buffer.from(a.data),hash:a.hash,dataType:a.dataType}))),l.data.forEach((a=>{crypto.createHash(r).update(Buffer.from(a.data)).digest("hex")!==a.hash&&i(new Error(`Data integrity check failed for ${a.dataName}`))})),e(l)}catch{i()}}))}async extractData(){return new Promise((async(a,t)=>{try{await sodium.ready;let r={};this.data.forEach((async a=>{try{const t=sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(null,a.data,null,a.nonce,this.userKey),e=zlib.inflateSync(t,{level:9}).toString("utf8");r[a.dataName]=await this.CSTI(e,a.dataType)}catch{t()}})),a(r)}catch{t()}}))}static async derivePBK(a,t){if(!(a&&"string"==typeof a&&a.trim()&&t&&t instanceof Uint8Array&&16===t.length))throw new Error("Invalid passcode or salt.");return new Promise((async(r,e)=>{argon2.hash(a,{salt:Buffer.from(t),type:argon2.argon2id}).then((a=>{r({pbk:a,salt:t})})).catch((a=>{e(a)}))}))}saveData(){const a=JSON.stringify({data:this.data,salt:Array.from(this.salt)});return zlib.deflateSync(a,{level:9})}static async loadFromString(a,t,r="sha3-512"){return new Promise((async(e,i)=>{try{a&&"string"==typeof a&&a.trim()&&t&&"string"==typeof t&&t.trim()||i(new Error("Invalid SPD path or passcode.")),await sodium.ready;const s=Buffer.from(a,"base64"),n=zlib.inflateSync(s,{level:9}).toString("utf8"),{data:o,salt:y}=JSON.parse(n),{pqcKey:c}=await(new SPD).convertPasscodeToPQCKeySalted(t,new Uint8Array(y)),d=c.publicKey,l=new SPD;l.userKey=d,l.keyPair={publicKey:d.publicKey},l.data=o.map((a=>({dataName:a.dataName,nonce:Buffer.from(a.nonce),data:Buffer.from(a.data),hash:a.hash,dataType:a.dataType}))),l.data.forEach((a=>{crypto.createHash(r).update(Buffer.from(a.data)).digest("hex")!==a.hash&&i(new Error(`Data integrity check failed for ${a.dataName}`))})),e(l)}catch{i()}}))}async convertPasscodeToPQCKeySalted(a,t){if(!a||"string"!=typeof a||!a.trim()||a.length<8||!t||!(t instanceof Uint8Array)||16!==t.length)throw new Error("Invalid passcode or salt.");const{pbk:r}=await SPD.derivePBK(a,t);await sodium.ready;return{pqcKey:{publicKey:sodium.crypto_kx_seed_keypair(r.slice(0,sodium.crypto_kx_SEEDBYTES)).publicKey},salt:t}}async convertPasscodeToPQCKey(a){if(!a||"string"!=typeof a||!a.trim()||a.length<8)throw new Error("Invalid passcode.");const{pbk:t,salt:r}=await SPD.derivePBK(a,crypto.getRandomValues(new Uint8Array(16)));await sodium.ready;return{pqcKey:{publicKey:sodium.crypto_kx_seed_keypair(t.slice(0,sodium.crypto_kx_SEEDBYTES)).publicKey},salt:r}}async TDT(a){return{"[object Array]":"Array","[object Uint8Array]":"Uint8Array","[object Uint16Array]":"Uint16Array","[object Uint32Array]":"Uint32Array","[object BigInt64Array]":"BigInt64Array","[object BigUint64Array]":"BigUint64Array","[object Float32Array]":"Float32Array","[object Float64Array]":"Float64Array","[object Map]":"Map","[object Set]":"Set","[object Date]":"Date","[object RegExp]":"RegExp","[object Error]":"Error"}[Object.prototype.toString.call(a)]}async isNumArr(a){if("Uint8Array"===a||"Uint16Array"===a||"Uint32Array"===a||"BigInt64Array"===a||"BigUint64Array"===a||"Float32Array"===a||"Float64Array"===a)return 1}async isSWM(a){if("Map"===a||"Set"===a||"WeakMap"===a||"WeakSet"===a)return 1}async isDRE(a){if("Date"===a||"RegExp"===a||"Error"===a)return 1}async CITS(a){const t=typeof a;if("string"===t||"number"===t||"boolean"===t)return[a.toString(),t];if("object"==typeof a){const t=await this.TDT(a);return"Array"===t?[JSON.stringify(a),"Array"]:await this.isNumArr(t)?[JSON.stringify(Array.from(a)),t]:await this.isSWM(t)?[JSON.stringify([...a]),t]:await this.isDRE(t)?[a.toString(),t]:[JSON.stringify(a),typeof a]}}async CSTI(a,t){return"string"===t?a:"number"===t?parseFloat(a):"boolean"===t?"true"===a&&"false"!==a:"object"===t||"Array"===t?JSON.parse(a):"Uint8Array"===t?new Uint8Array(JSON.parse(a)):"Uint16Array"===t?new Uint16Array(JSON.parse(a)):"Uint32Array"===t?new Uint32Array(JSON.parse(a)):"BigInt64Array"===t?new BigInt64Array(JSON.parse(a)):"BigUint64Array"===t?new BigUint64Array(JSON.parse(a)):"Float32Array"===t?new Float32Array(JSON.parse(a)):"Float64Array"===t?new Float64Array(JSON.parse(a)):"Map"===t?new Map(JSON.parse(a)):"Set"===t?new Set(JSON.parse(a)):"WeakMap"===t?new WeakMap(JSON.parse(a)):"WeakSet"===t?new WeakSet(JSON.parse(a)):"Date"===t?new Date(a):"RegExp"===t?new RegExp(a):"Error"===t?new Error(a):void 0}}module.exports={SPD};
|