@mtkruto/node 0.18.0 → 0.18.1
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"1_crypto.d.ts","sourceRoot":"","sources":["../../src/utilities/1_crypto.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,KAAK,OAAO,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"1_crypto.d.ts","sourceRoot":"","sources":["../../src/utilities/1_crypto.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,KAAK,OAAO,MAAM,kBAAkB,CAAC;AAO5C,qBAAa,GAAG;;IAMd,IAAI,MAAM,IAAI;QAAE,EAAE,EAAE,UAAU,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAE9C;gBAEW,GAAG,EAAE,OAAO,CAAC,SAAS,EAAE,EAAE,EAAE,UAAU;WAKrC,SAAS,CAAC,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC;IAI7D,IAAI,CAAC,IAAI,EAAE,UAAU;CAkD5B"}
|
|
@@ -9,7 +9,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
10
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
11
|
};
|
|
12
|
-
var _CTR_key, _CTR_iv,
|
|
12
|
+
var _CTR_instances, _CTR_key, _CTR_iv, _CTR_bytesUntilNextBlock, _CTR_promise, _CTR_call, _CTR_encrypt, _CTR_increaseIv;
|
|
13
13
|
/**
|
|
14
14
|
* MTKruto - Cross-runtime JavaScript library for building Telegram clients
|
|
15
15
|
* Copyright (C) 2023-2025 Roj <https://roj.im/>
|
|
@@ -30,16 +30,19 @@ var _CTR_key, _CTR_iv, _CTR_incrementPending;
|
|
|
30
30
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
31
31
|
*/
|
|
32
32
|
import * as dntShim from "../_dnt.shims.js";
|
|
33
|
+
import { concat } from "../0_deps.js";
|
|
33
34
|
import { bigIntFromBuffer } from "./0_bigint.js";
|
|
34
35
|
import { bufferFromBigInt } from "./0_buffer.js";
|
|
35
36
|
export class CTR {
|
|
36
37
|
get _state() {
|
|
37
|
-
return { iv: new Uint8Array(__classPrivateFieldGet(this, _CTR_iv, "f")), state: __classPrivateFieldGet(this,
|
|
38
|
+
return { iv: new Uint8Array(__classPrivateFieldGet(this, _CTR_iv, "f")), state: __classPrivateFieldGet(this, _CTR_bytesUntilNextBlock, "f") };
|
|
38
39
|
}
|
|
39
40
|
constructor(key, iv) {
|
|
41
|
+
_CTR_instances.add(this);
|
|
40
42
|
_CTR_key.set(this, void 0);
|
|
41
43
|
_CTR_iv.set(this, void 0);
|
|
42
|
-
|
|
44
|
+
_CTR_bytesUntilNextBlock.set(this, 0);
|
|
45
|
+
_CTR_promise.set(this, void 0);
|
|
43
46
|
__classPrivateFieldSet(this, _CTR_key, key, "f");
|
|
44
47
|
__classPrivateFieldSet(this, _CTR_iv, iv, "f");
|
|
45
48
|
}
|
|
@@ -47,22 +50,43 @@ export class CTR {
|
|
|
47
50
|
return await dntShim.crypto.subtle.importKey("raw", key, "AES-CTR", false, ["encrypt"]);
|
|
48
51
|
}
|
|
49
52
|
async call(data) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
counter: new Uint8Array(__classPrivateFieldGet(this, _CTR_iv, "f")),
|
|
53
|
-
length: __classPrivateFieldGet(this, _CTR_iv, "f").length * 8,
|
|
54
|
-
}, __classPrivateFieldGet(this, _CTR_key, "f"), data);
|
|
55
|
-
const incrementPending = data.length % __classPrivateFieldGet(this, _CTR_iv, "f").length;
|
|
56
|
-
let incrementBy = (data.length - incrementPending) / __classPrivateFieldGet(this, _CTR_iv, "f").length;
|
|
57
|
-
__classPrivateFieldSet(this, _CTR_incrementPending, __classPrivateFieldGet(this, _CTR_incrementPending, "f") + incrementPending, "f");
|
|
58
|
-
while (__classPrivateFieldGet(this, _CTR_incrementPending, "f") >= __classPrivateFieldGet(this, _CTR_iv, "f").length) {
|
|
59
|
-
__classPrivateFieldSet(this, _CTR_incrementPending, __classPrivateFieldGet(this, _CTR_incrementPending, "f") - __classPrivateFieldGet(this, _CTR_iv, "f").length, "f");
|
|
60
|
-
incrementBy += 1;
|
|
53
|
+
if (__classPrivateFieldGet(this, _CTR_promise, "f")) {
|
|
54
|
+
await Promise.allSettled([__classPrivateFieldGet(this, _CTR_promise, "f")]);
|
|
61
55
|
}
|
|
62
|
-
|
|
63
|
-
__classPrivateFieldSet(this, _CTR_iv, bufferFromBigInt(bigIntFromBuffer(__classPrivateFieldGet(this, _CTR_iv, "f"), false, false) + BigInt(incrementBy), 16, false, false), "f");
|
|
64
|
-
}
|
|
65
|
-
return new Uint8Array(await promise);
|
|
56
|
+
return await (__classPrivateFieldSet(this, _CTR_promise, __classPrivateFieldGet(this, _CTR_instances, "m", _CTR_call).call(this, data), "f"));
|
|
66
57
|
}
|
|
67
58
|
}
|
|
68
|
-
_CTR_key = new WeakMap(), _CTR_iv = new WeakMap(),
|
|
59
|
+
_CTR_key = new WeakMap(), _CTR_iv = new WeakMap(), _CTR_bytesUntilNextBlock = new WeakMap(), _CTR_promise = new WeakMap(), _CTR_instances = new WeakSet(), _CTR_call = async function _CTR_call(data) {
|
|
60
|
+
let header;
|
|
61
|
+
if (__classPrivateFieldGet(this, _CTR_bytesUntilNextBlock, "f")) {
|
|
62
|
+
const headerLength = Math.min(data.length, __classPrivateFieldGet(this, _CTR_iv, "f").length - __classPrivateFieldGet(this, _CTR_bytesUntilNextBlock, "f"));
|
|
63
|
+
const encrypted = await __classPrivateFieldGet(this, _CTR_instances, "m", _CTR_encrypt).call(this, concat([new Uint8Array(__classPrivateFieldGet(this, _CTR_bytesUntilNextBlock, "f")), data.subarray(0, headerLength)]));
|
|
64
|
+
header = encrypted.subarray(__classPrivateFieldGet(this, _CTR_bytesUntilNextBlock, "f"));
|
|
65
|
+
data = data.subarray(headerLength);
|
|
66
|
+
if (encrypted.length === __classPrivateFieldGet(this, _CTR_iv, "f").length) {
|
|
67
|
+
__classPrivateFieldGet(this, _CTR_instances, "m", _CTR_increaseIv).call(this, 1);
|
|
68
|
+
__classPrivateFieldSet(this, _CTR_bytesUntilNextBlock, 0, "f");
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
__classPrivateFieldSet(this, _CTR_bytesUntilNextBlock, __classPrivateFieldGet(this, _CTR_bytesUntilNextBlock, "f") + headerLength, "f");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (!data.length && header) {
|
|
75
|
+
return header;
|
|
76
|
+
}
|
|
77
|
+
const encrypted = await __classPrivateFieldGet(this, _CTR_instances, "m", _CTR_encrypt).call(this, data);
|
|
78
|
+
__classPrivateFieldSet(this, _CTR_bytesUntilNextBlock, encrypted.length % __classPrivateFieldGet(this, _CTR_iv, "f").length, "f");
|
|
79
|
+
__classPrivateFieldGet(this, _CTR_instances, "m", _CTR_increaseIv).call(this, (encrypted.length - __classPrivateFieldGet(this, _CTR_bytesUntilNextBlock, "f")) / __classPrivateFieldGet(this, _CTR_iv, "f").length);
|
|
80
|
+
return header ? concat([header, encrypted]) : encrypted;
|
|
81
|
+
}, _CTR_encrypt = async function _CTR_encrypt(data) {
|
|
82
|
+
return new Uint8Array(await dntShim.crypto.subtle.encrypt({
|
|
83
|
+
name: "AES-CTR",
|
|
84
|
+
counter: new Uint8Array(__classPrivateFieldGet(this, _CTR_iv, "f")),
|
|
85
|
+
length: __classPrivateFieldGet(this, _CTR_iv, "f").length * 8,
|
|
86
|
+
}, __classPrivateFieldGet(this, _CTR_key, "f"), data));
|
|
87
|
+
}, _CTR_increaseIv = function _CTR_increaseIv(amount) {
|
|
88
|
+
if (amount < 1) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
__classPrivateFieldSet(this, _CTR_iv, bufferFromBigInt(bigIntFromBuffer(__classPrivateFieldGet(this, _CTR_iv, "f"), false, false) + BigInt(amount), __classPrivateFieldGet(this, _CTR_iv, "f").length, false, false), "f");
|
|
92
|
+
};
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"1_crypto.d.ts","sourceRoot":"","sources":["../../src/utilities/1_crypto.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,KAAK,OAAO,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"1_crypto.d.ts","sourceRoot":"","sources":["../../src/utilities/1_crypto.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,KAAK,OAAO,MAAM,kBAAkB,CAAC;AAO5C,qBAAa,GAAG;;IAMd,IAAI,MAAM,IAAI;QAAE,EAAE,EAAE,UAAU,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAE9C;gBAEW,GAAG,EAAE,OAAO,CAAC,SAAS,EAAE,EAAE,EAAE,UAAU;WAKrC,SAAS,CAAC,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC;IAI7D,IAAI,CAAC,IAAI,EAAE,UAAU;CAkD5B"}
|
|
@@ -33,7 +33,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
33
33
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
34
34
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
35
35
|
};
|
|
36
|
-
var _CTR_key, _CTR_iv,
|
|
36
|
+
var _CTR_instances, _CTR_key, _CTR_iv, _CTR_bytesUntilNextBlock, _CTR_promise, _CTR_call, _CTR_encrypt, _CTR_increaseIv;
|
|
37
37
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
38
|
exports.CTR = void 0;
|
|
39
39
|
/**
|
|
@@ -56,16 +56,19 @@ exports.CTR = void 0;
|
|
|
56
56
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
57
57
|
*/
|
|
58
58
|
const dntShim = __importStar(require("../_dnt.shims.js"));
|
|
59
|
+
const _0_deps_js_1 = require("../0_deps.js");
|
|
59
60
|
const _0_bigint_js_1 = require("./0_bigint.js");
|
|
60
61
|
const _0_buffer_js_1 = require("./0_buffer.js");
|
|
61
62
|
class CTR {
|
|
62
63
|
get _state() {
|
|
63
|
-
return { iv: new Uint8Array(__classPrivateFieldGet(this, _CTR_iv, "f")), state: __classPrivateFieldGet(this,
|
|
64
|
+
return { iv: new Uint8Array(__classPrivateFieldGet(this, _CTR_iv, "f")), state: __classPrivateFieldGet(this, _CTR_bytesUntilNextBlock, "f") };
|
|
64
65
|
}
|
|
65
66
|
constructor(key, iv) {
|
|
67
|
+
_CTR_instances.add(this);
|
|
66
68
|
_CTR_key.set(this, void 0);
|
|
67
69
|
_CTR_iv.set(this, void 0);
|
|
68
|
-
|
|
70
|
+
_CTR_bytesUntilNextBlock.set(this, 0);
|
|
71
|
+
_CTR_promise.set(this, void 0);
|
|
69
72
|
__classPrivateFieldSet(this, _CTR_key, key, "f");
|
|
70
73
|
__classPrivateFieldSet(this, _CTR_iv, iv, "f");
|
|
71
74
|
}
|
|
@@ -73,23 +76,44 @@ class CTR {
|
|
|
73
76
|
return await dntShim.crypto.subtle.importKey("raw", key, "AES-CTR", false, ["encrypt"]);
|
|
74
77
|
}
|
|
75
78
|
async call(data) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
counter: new Uint8Array(__classPrivateFieldGet(this, _CTR_iv, "f")),
|
|
79
|
-
length: __classPrivateFieldGet(this, _CTR_iv, "f").length * 8,
|
|
80
|
-
}, __classPrivateFieldGet(this, _CTR_key, "f"), data);
|
|
81
|
-
const incrementPending = data.length % __classPrivateFieldGet(this, _CTR_iv, "f").length;
|
|
82
|
-
let incrementBy = (data.length - incrementPending) / __classPrivateFieldGet(this, _CTR_iv, "f").length;
|
|
83
|
-
__classPrivateFieldSet(this, _CTR_incrementPending, __classPrivateFieldGet(this, _CTR_incrementPending, "f") + incrementPending, "f");
|
|
84
|
-
while (__classPrivateFieldGet(this, _CTR_incrementPending, "f") >= __classPrivateFieldGet(this, _CTR_iv, "f").length) {
|
|
85
|
-
__classPrivateFieldSet(this, _CTR_incrementPending, __classPrivateFieldGet(this, _CTR_incrementPending, "f") - __classPrivateFieldGet(this, _CTR_iv, "f").length, "f");
|
|
86
|
-
incrementBy += 1;
|
|
79
|
+
if (__classPrivateFieldGet(this, _CTR_promise, "f")) {
|
|
80
|
+
await Promise.allSettled([__classPrivateFieldGet(this, _CTR_promise, "f")]);
|
|
87
81
|
}
|
|
88
|
-
|
|
89
|
-
__classPrivateFieldSet(this, _CTR_iv, (0, _0_buffer_js_1.bufferFromBigInt)((0, _0_bigint_js_1.bigIntFromBuffer)(__classPrivateFieldGet(this, _CTR_iv, "f"), false, false) + BigInt(incrementBy), 16, false, false), "f");
|
|
90
|
-
}
|
|
91
|
-
return new Uint8Array(await promise);
|
|
82
|
+
return await (__classPrivateFieldSet(this, _CTR_promise, __classPrivateFieldGet(this, _CTR_instances, "m", _CTR_call).call(this, data), "f"));
|
|
92
83
|
}
|
|
93
84
|
}
|
|
94
85
|
exports.CTR = CTR;
|
|
95
|
-
_CTR_key = new WeakMap(), _CTR_iv = new WeakMap(),
|
|
86
|
+
_CTR_key = new WeakMap(), _CTR_iv = new WeakMap(), _CTR_bytesUntilNextBlock = new WeakMap(), _CTR_promise = new WeakMap(), _CTR_instances = new WeakSet(), _CTR_call = async function _CTR_call(data) {
|
|
87
|
+
let header;
|
|
88
|
+
if (__classPrivateFieldGet(this, _CTR_bytesUntilNextBlock, "f")) {
|
|
89
|
+
const headerLength = Math.min(data.length, __classPrivateFieldGet(this, _CTR_iv, "f").length - __classPrivateFieldGet(this, _CTR_bytesUntilNextBlock, "f"));
|
|
90
|
+
const encrypted = await __classPrivateFieldGet(this, _CTR_instances, "m", _CTR_encrypt).call(this, (0, _0_deps_js_1.concat)([new Uint8Array(__classPrivateFieldGet(this, _CTR_bytesUntilNextBlock, "f")), data.subarray(0, headerLength)]));
|
|
91
|
+
header = encrypted.subarray(__classPrivateFieldGet(this, _CTR_bytesUntilNextBlock, "f"));
|
|
92
|
+
data = data.subarray(headerLength);
|
|
93
|
+
if (encrypted.length === __classPrivateFieldGet(this, _CTR_iv, "f").length) {
|
|
94
|
+
__classPrivateFieldGet(this, _CTR_instances, "m", _CTR_increaseIv).call(this, 1);
|
|
95
|
+
__classPrivateFieldSet(this, _CTR_bytesUntilNextBlock, 0, "f");
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
__classPrivateFieldSet(this, _CTR_bytesUntilNextBlock, __classPrivateFieldGet(this, _CTR_bytesUntilNextBlock, "f") + headerLength, "f");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (!data.length && header) {
|
|
102
|
+
return header;
|
|
103
|
+
}
|
|
104
|
+
const encrypted = await __classPrivateFieldGet(this, _CTR_instances, "m", _CTR_encrypt).call(this, data);
|
|
105
|
+
__classPrivateFieldSet(this, _CTR_bytesUntilNextBlock, encrypted.length % __classPrivateFieldGet(this, _CTR_iv, "f").length, "f");
|
|
106
|
+
__classPrivateFieldGet(this, _CTR_instances, "m", _CTR_increaseIv).call(this, (encrypted.length - __classPrivateFieldGet(this, _CTR_bytesUntilNextBlock, "f")) / __classPrivateFieldGet(this, _CTR_iv, "f").length);
|
|
107
|
+
return header ? (0, _0_deps_js_1.concat)([header, encrypted]) : encrypted;
|
|
108
|
+
}, _CTR_encrypt = async function _CTR_encrypt(data) {
|
|
109
|
+
return new Uint8Array(await dntShim.crypto.subtle.encrypt({
|
|
110
|
+
name: "AES-CTR",
|
|
111
|
+
counter: new Uint8Array(__classPrivateFieldGet(this, _CTR_iv, "f")),
|
|
112
|
+
length: __classPrivateFieldGet(this, _CTR_iv, "f").length * 8,
|
|
113
|
+
}, __classPrivateFieldGet(this, _CTR_key, "f"), data));
|
|
114
|
+
}, _CTR_increaseIv = function _CTR_increaseIv(amount) {
|
|
115
|
+
if (amount < 1) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
__classPrivateFieldSet(this, _CTR_iv, (0, _0_buffer_js_1.bufferFromBigInt)((0, _0_bigint_js_1.bigIntFromBuffer)(__classPrivateFieldGet(this, _CTR_iv, "f"), false, false) + BigInt(amount), __classPrivateFieldGet(this, _CTR_iv, "f").length, false, false), "f");
|
|
119
|
+
};
|