@secrecy/lib 1.62.0-feat-node-sharing.4 → 1.62.0-feat-node-sharing.5
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
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@secrecy/lib",
|
|
3
3
|
"author": "Anonymize <anonymize@gmail.com>",
|
|
4
4
|
"description": "Anonymize Secrecy Library",
|
|
5
|
-
"version": "1.62.0-feat-node-sharing.
|
|
5
|
+
"version": "1.62.0-feat-node-sharing.5",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "https://github.com/anonymize-org/lib.git"
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
function* chunks(arr, n) {
|
|
3
|
-
for (let i = 0; i < arr.length; i += n) {
|
|
4
|
-
yield arr.slice(i, i + n);
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
function assert(c, message) {
|
|
8
|
-
if (!c) {
|
|
9
|
-
throw new Error(message);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
function encrypt(key) {
|
|
13
|
-
let destroyed = false;
|
|
14
|
-
const { state, header } = sodium.crypto_secretstream_xchacha20poly1305_init_push(key);
|
|
15
|
-
const encrypt = (tag, plaintext) => {
|
|
16
|
-
assert(destroyed === false, 'state already destroyed');
|
|
17
|
-
return sodium.crypto_secretstream_xchacha20poly1305_push(state, plaintext, null, tag);
|
|
18
|
-
};
|
|
19
|
-
function destroy() {
|
|
20
|
-
assert(destroyed === false, 'state already destroyed');
|
|
21
|
-
destroyed = true;
|
|
22
|
-
}
|
|
23
|
-
return {
|
|
24
|
-
encrypt,
|
|
25
|
-
destroy,
|
|
26
|
-
header,
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
function decrypt(header, key) {
|
|
30
|
-
assert(header.byteLength >=
|
|
31
|
-
sodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES, 'header must be at least HEADERBYTES (' +
|
|
32
|
-
sodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES +
|
|
33
|
-
') long');
|
|
34
|
-
assert(key.byteLength >= sodium.crypto_secretstream_xchacha20poly1305_KEYBYTES, 'key must be at least KEYBYTES (' +
|
|
35
|
-
sodium.crypto_secretstream_xchacha20poly1305_KEYBYTES +
|
|
36
|
-
') long');
|
|
37
|
-
let destroyed = false;
|
|
38
|
-
const state = sodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key);
|
|
39
|
-
const decrypt = (ciphertext) => {
|
|
40
|
-
assert(destroyed === false, 'state already destroyed');
|
|
41
|
-
return sodium.crypto_secretstream_xchacha20poly1305_pull(state, ciphertext);
|
|
42
|
-
};
|
|
43
|
-
function destroy() {
|
|
44
|
-
assert(destroyed === false, 'state already destroyed');
|
|
45
|
-
destroyed = true;
|
|
46
|
-
}
|
|
47
|
-
return {
|
|
48
|
-
decrypt,
|
|
49
|
-
destroy,
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
const CHUNK_SIZE = 8192;
|
|
53
|
-
export async function encryptSecretstream(key, data, progress) {
|
|
54
|
-
const { encrypt: crypt, destroy, header } = encrypt(key);
|
|
55
|
-
const cryptedChunk = CHUNK_SIZE + sodium.crypto_secretstream_xchacha20poly1305_ABYTES;
|
|
56
|
-
const max = Math.ceil(data.byteLength / CHUNK_SIZE) * cryptedChunk + header.byteLength;
|
|
57
|
-
progress?.({
|
|
58
|
-
percent: 0,
|
|
59
|
-
total: max,
|
|
60
|
-
current: 0,
|
|
61
|
-
});
|
|
62
|
-
const final = new Uint8Array(max);
|
|
63
|
-
const sparkEncrypted = new SparkMD5.ArrayBuffer();
|
|
64
|
-
const spark = new SparkMD5.ArrayBuffer();
|
|
65
|
-
final.set(header);
|
|
66
|
-
sparkEncrypted.append(header);
|
|
67
|
-
let total = header.byteLength;
|
|
68
|
-
progress?.({
|
|
69
|
-
percent: total / max,
|
|
70
|
-
total: max,
|
|
71
|
-
current: total,
|
|
72
|
-
});
|
|
73
|
-
let lastPercent = total / max;
|
|
74
|
-
for (const chunk of chunks(data, CHUNK_SIZE)) {
|
|
75
|
-
spark.append(chunk);
|
|
76
|
-
const tag = chunk.length < CHUNK_SIZE
|
|
77
|
-
? sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL
|
|
78
|
-
: sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE;
|
|
79
|
-
const crypted = crypt(tag, chunk);
|
|
80
|
-
sparkEncrypted.append(crypted);
|
|
81
|
-
final.set(crypted, total);
|
|
82
|
-
total += crypted.byteLength;
|
|
83
|
-
const percent = total / max;
|
|
84
|
-
if (percent > lastPercent + 0.01) {
|
|
85
|
-
progress?.({
|
|
86
|
-
percent,
|
|
87
|
-
total: max,
|
|
88
|
-
current: total,
|
|
89
|
-
});
|
|
90
|
-
lastPercent = percent;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
destroy();
|
|
94
|
-
progress?.({
|
|
95
|
-
percent: 1,
|
|
96
|
-
total,
|
|
97
|
-
current: total,
|
|
98
|
-
});
|
|
99
|
-
return {
|
|
100
|
-
data: final.slice(0, total),
|
|
101
|
-
md5Encrypted: sparkEncrypted.end(),
|
|
102
|
-
md5: spark.end(),
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
export async function decryptSecretstream(key, data, progress) {
|
|
106
|
-
const header = data.slice(0, sodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES);
|
|
107
|
-
data = data.slice(sodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES);
|
|
108
|
-
const { decrypt: decryptt, destroy } = decrypt(header, key);
|
|
109
|
-
const chunkSize = CHUNK_SIZE + sodium.crypto_secretstream_xchacha20poly1305_ABYTES;
|
|
110
|
-
const max = Math.ceil(data.byteLength / chunkSize) * CHUNK_SIZE;
|
|
111
|
-
progress?.({
|
|
112
|
-
percent: 0,
|
|
113
|
-
total: max,
|
|
114
|
-
current: 0,
|
|
115
|
-
});
|
|
116
|
-
const final = new Uint8Array(max);
|
|
117
|
-
let total = 0;
|
|
118
|
-
let lastPercent = total / max;
|
|
119
|
-
for (const chunk of chunks(data, chunkSize)) {
|
|
120
|
-
const tmp = decryptt(chunk);
|
|
121
|
-
final.set(tmp.message, total);
|
|
122
|
-
total += tmp.message.byteLength;
|
|
123
|
-
const percent = total / max;
|
|
124
|
-
if (percent > lastPercent + 0.01) {
|
|
125
|
-
progress?.({
|
|
126
|
-
percent,
|
|
127
|
-
total: max,
|
|
128
|
-
current: total,
|
|
129
|
-
});
|
|
130
|
-
lastPercent = percent;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
destroy();
|
|
134
|
-
progress?.({
|
|
135
|
-
percent: 1,
|
|
136
|
-
total,
|
|
137
|
-
current: total,
|
|
138
|
-
});
|
|
139
|
-
return final.slice(0, total);
|
|
140
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export declare function encryptSecretstream(key: any, data: any, progress: any): Promise<{
|
|
2
|
-
data: Uint8Array<ArrayBuffer>;
|
|
3
|
-
md5Encrypted: any;
|
|
4
|
-
md5: any;
|
|
5
|
-
}>;
|
|
6
|
-
export declare function decryptSecretstream(key: any, data: any, progress: any): Promise<Uint8Array<ArrayBuffer>>;
|