@armatofik/tgsnake-core 1.0.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 (171) hide show
  1. package/COPYING +674 -0
  2. package/COPYING.lesser +165 -0
  3. package/README.md +88 -0
  4. package/lib/package.json +46 -0
  5. package/lib/src/Logger.d.ts +3 -0
  6. package/lib/src/Logger.js +9 -0
  7. package/lib/src/Queue.d.ts +19 -0
  8. package/lib/src/Queue.js +52 -0
  9. package/lib/src/Timeout.d.ts +9 -0
  10. package/lib/src/Timeout.js +44 -0
  11. package/lib/src/Version.node.d.ts +4 -0
  12. package/lib/src/Version.node.js +26 -0
  13. package/lib/src/client/Auth.d.ts +40 -0
  14. package/lib/src/client/Auth.js +317 -0
  15. package/lib/src/client/Client.d.ts +83 -0
  16. package/lib/src/client/Client.js +461 -0
  17. package/lib/src/client/Session.d.ts +9 -0
  18. package/lib/src/client/Session.js +132 -0
  19. package/lib/src/client/index.d.ts +3 -0
  20. package/lib/src/client/index.js +39 -0
  21. package/lib/src/connection/TCP/TCPAbridged.d.ts +9 -0
  22. package/lib/src/connection/TCP/TCPAbridged.js +47 -0
  23. package/lib/src/connection/TCP/TCPAbridgedO.d.ts +12 -0
  24. package/lib/src/connection/TCP/TCPAbridgedO.js +140 -0
  25. package/lib/src/connection/TCP/TCPFull.d.ts +10 -0
  26. package/lib/src/connection/TCP/TCPFull.js +43 -0
  27. package/lib/src/connection/TCP/TCPIntermediate.d.ts +9 -0
  28. package/lib/src/connection/TCP/TCPIntermediate.js +31 -0
  29. package/lib/src/connection/TCP/TCPIntermediateO.d.ts +12 -0
  30. package/lib/src/connection/TCP/TCPIntermediateO.js +123 -0
  31. package/lib/src/connection/TCP/TCPPaddedIntermediate.d.ts +9 -0
  32. package/lib/src/connection/TCP/TCPPaddedIntermediate.js +38 -0
  33. package/lib/src/connection/TCP/index.d.ts +7 -0
  34. package/lib/src/connection/TCP/index.js +17 -0
  35. package/lib/src/connection/TCP/tcp.d.ts +17 -0
  36. package/lib/src/connection/TCP/tcp.js +98 -0
  37. package/lib/src/connection/WebSocket.d.ts +21 -0
  38. package/lib/src/connection/WebSocket.js +279 -0
  39. package/lib/src/connection/connection.d.ts +53 -0
  40. package/lib/src/connection/connection.js +189 -0
  41. package/lib/src/connection/index.d.ts +4 -0
  42. package/lib/src/connection/index.js +41 -0
  43. package/lib/src/crypto/Aes.d.ts +10 -0
  44. package/lib/src/crypto/Aes.js +135 -0
  45. package/lib/src/crypto/Mtproto.d.ts +5 -0
  46. package/lib/src/crypto/Mtproto.js +123 -0
  47. package/lib/src/crypto/Password.d.ts +5 -0
  48. package/lib/src/crypto/Password.js +92 -0
  49. package/lib/src/crypto/Prime.d.ts +4 -0
  50. package/lib/src/crypto/Prime.js +64 -0
  51. package/lib/src/crypto/RSA.d.ts +7 -0
  52. package/lib/src/crypto/RSA.js +99 -0
  53. package/lib/src/crypto/SecretChat.d.ts +5 -0
  54. package/lib/src/crypto/SecretChat.js +136 -0
  55. package/lib/src/crypto/index.d.ts +6 -0
  56. package/lib/src/crypto/index.js +42 -0
  57. package/lib/src/errors/Base.d.ts +9 -0
  58. package/lib/src/errors/Base.js +50 -0
  59. package/lib/src/errors/Client.d.ts +21 -0
  60. package/lib/src/errors/Client.js +29 -0
  61. package/lib/src/errors/File.d.ts +13 -0
  62. package/lib/src/errors/File.js +30 -0
  63. package/lib/src/errors/RpcError.d.ts +22 -0
  64. package/lib/src/errors/RpcError.js +120 -0
  65. package/lib/src/errors/SecretChat.d.ts +13 -0
  66. package/lib/src/errors/SecretChat.js +30 -0
  67. package/lib/src/errors/WebSocket.d.ts +13 -0
  68. package/lib/src/errors/WebSocket.js +30 -0
  69. package/lib/src/errors/exceptions/All.d.ts +774 -0
  70. package/lib/src/errors/exceptions/All.js +778 -0
  71. package/lib/src/errors/exceptions/BadRequest400.d.ts +2345 -0
  72. package/lib/src/errors/exceptions/BadRequest400.js +2945 -0
  73. package/lib/src/errors/exceptions/Flood420.d.ts +37 -0
  74. package/lib/src/errors/exceptions/Flood420.js +49 -0
  75. package/lib/src/errors/exceptions/Forbidden403.d.ts +205 -0
  76. package/lib/src/errors/exceptions/Forbidden403.js +260 -0
  77. package/lib/src/errors/exceptions/InternalServerError500.d.ts +221 -0
  78. package/lib/src/errors/exceptions/InternalServerError500.js +280 -0
  79. package/lib/src/errors/exceptions/NotAcceptable406.d.ts +117 -0
  80. package/lib/src/errors/exceptions/NotAcceptable406.js +149 -0
  81. package/lib/src/errors/exceptions/NotFound404.d.ts +9 -0
  82. package/lib/src/errors/exceptions/NotFound404.js +14 -0
  83. package/lib/src/errors/exceptions/SeeOther303.d.ts +25 -0
  84. package/lib/src/errors/exceptions/SeeOther303.js +34 -0
  85. package/lib/src/errors/exceptions/ServiceUnavailable503.d.ts +17 -0
  86. package/lib/src/errors/exceptions/ServiceUnavailable503.js +24 -0
  87. package/lib/src/errors/exceptions/Unauthorized401.d.ts +41 -0
  88. package/lib/src/errors/exceptions/Unauthorized401.js +54 -0
  89. package/lib/src/errors/exceptions/index.d.ts +10 -0
  90. package/lib/src/errors/exceptions/index.js +48 -0
  91. package/lib/src/errors/index.d.ts +31 -0
  92. package/lib/src/errors/index.js +111 -0
  93. package/lib/src/file/Download.d.ts +11 -0
  94. package/lib/src/file/Download.js +118 -0
  95. package/lib/src/file/File.d.ts +20 -0
  96. package/lib/src/file/File.js +82 -0
  97. package/lib/src/file/Upload.d.ts +19 -0
  98. package/lib/src/file/Upload.js +250 -0
  99. package/lib/src/file/index.d.ts +3 -0
  100. package/lib/src/file/index.js +11 -0
  101. package/lib/src/helpers.d.ts +34 -0
  102. package/lib/src/helpers.js +358 -0
  103. package/lib/src/index.d.ts +16 -0
  104. package/lib/src/index.js +56 -0
  105. package/lib/src/platform.node.d.ts +36 -0
  106. package/lib/src/platform.node.js +109 -0
  107. package/lib/src/raw/All.d.ts +2400 -0
  108. package/lib/src/raw/All.js +2403 -0
  109. package/lib/src/raw/Raw.d.ts +29195 -0
  110. package/lib/src/raw/Raw.js +97277 -0
  111. package/lib/src/raw/core/BytesIO.d.ts +33 -0
  112. package/lib/src/raw/core/BytesIO.js +145 -0
  113. package/lib/src/raw/core/GzipPacked.d.ts +10 -0
  114. package/lib/src/raw/core/GzipPacked.js +60 -0
  115. package/lib/src/raw/core/Message.d.ts +13 -0
  116. package/lib/src/raw/core/Message.js +81 -0
  117. package/lib/src/raw/core/MsgContainer.d.ts +11 -0
  118. package/lib/src/raw/core/MsgContainer.js +69 -0
  119. package/lib/src/raw/core/TLObject.d.ts +19 -0
  120. package/lib/src/raw/core/TLObject.js +108 -0
  121. package/lib/src/raw/core/UpdateSecretChat.d.ts +40 -0
  122. package/lib/src/raw/core/UpdateSecretChat.js +88 -0
  123. package/lib/src/raw/core/index.d.ts +8 -0
  124. package/lib/src/raw/core/index.js +52 -0
  125. package/lib/src/raw/core/primitive/Bool.d.ts +20 -0
  126. package/lib/src/raw/core/primitive/Bool.js +41 -0
  127. package/lib/src/raw/core/primitive/Bytes.d.ts +7 -0
  128. package/lib/src/raw/core/primitive/Bytes.js +41 -0
  129. package/lib/src/raw/core/primitive/Double.d.ts +7 -0
  130. package/lib/src/raw/core/primitive/Double.js +24 -0
  131. package/lib/src/raw/core/primitive/Float.d.ts +7 -0
  132. package/lib/src/raw/core/primitive/Float.js +24 -0
  133. package/lib/src/raw/core/primitive/Int.d.ts +23 -0
  134. package/lib/src/raw/core/primitive/Int.js +122 -0
  135. package/lib/src/raw/core/primitive/String.d.ts +7 -0
  136. package/lib/src/raw/core/primitive/String.js +15 -0
  137. package/lib/src/raw/core/primitive/Vector.d.ts +9 -0
  138. package/lib/src/raw/core/primitive/Vector.js +50 -0
  139. package/lib/src/raw/core/primitive/index.d.ts +7 -0
  140. package/lib/src/raw/core/primitive/index.js +22 -0
  141. package/lib/src/raw/index.d.ts +3 -0
  142. package/lib/src/raw/index.js +17 -0
  143. package/lib/src/session/Auth.d.ts +15 -0
  144. package/lib/src/session/Auth.js +254 -0
  145. package/lib/src/session/Session.d.ts +58 -0
  146. package/lib/src/session/Session.js +571 -0
  147. package/lib/src/session/index.d.ts +7 -0
  148. package/lib/src/session/index.js +49 -0
  149. package/lib/src/session/internals/DataCenter.d.ts +42 -0
  150. package/lib/src/session/internals/DataCenter.js +80 -0
  151. package/lib/src/session/internals/MsgFactory.d.ts +3 -0
  152. package/lib/src/session/internals/MsgFactory.js +23 -0
  153. package/lib/src/session/internals/MsgId.d.ts +9 -0
  154. package/lib/src/session/internals/MsgId.js +24 -0
  155. package/lib/src/session/internals/SeqNo.d.ts +6 -0
  156. package/lib/src/session/internals/SeqNo.js +18 -0
  157. package/lib/src/session/secretChats/SecretChat.d.ts +29 -0
  158. package/lib/src/session/secretChats/SecretChat.js +527 -0
  159. package/lib/src/session/secretChats/index.d.ts +1 -0
  160. package/lib/src/session/secretChats/index.js +5 -0
  161. package/lib/src/storage/Abstract.d.ts +68 -0
  162. package/lib/src/storage/Abstract.js +6 -0
  163. package/lib/src/storage/SecretChat.d.ts +42 -0
  164. package/lib/src/storage/SecretChat.js +99 -0
  165. package/lib/src/storage/Session.d.ts +63 -0
  166. package/lib/src/storage/Session.js +245 -0
  167. package/lib/src/storage/StringSession.d.ts +4 -0
  168. package/lib/src/storage/StringSession.js +77 -0
  169. package/lib/src/storage/index.d.ts +4 -0
  170. package/lib/src/storage/index.js +12 -0
  171. package/package.json +46 -0
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ige256Encrypt = ige256Encrypt;
4
+ exports.ige256Decrypt = ige256Decrypt;
5
+ exports.ctr256Cipher = ctr256Cipher;
6
+ exports.xor = xor;
7
+ exports.AES = AES;
8
+ const platform_node_js_1 = require("../platform.node.js");
9
+ const Logger_js_1 = require("../Logger.js");
10
+ const helpers_js_1 = require("../helpers.js");
11
+ function ige256Encrypt(data, key, iv) {
12
+ Logger_js_1.Logger.debug(`[4] Encrypting ${platform_node_js_1.Buffer.byteLength(data)} bytes data with AES-256-IGE`);
13
+ const pad = (0, helpers_js_1.mod)(platform_node_js_1.Buffer.byteLength(data), 16);
14
+ if (pad) {
15
+ data = platform_node_js_1.Buffer.concat([
16
+ data,
17
+ platform_node_js_1.crypto.randomBytes(16 - pad),
18
+ ]);
19
+ }
20
+ return ige(data, key, iv, true);
21
+ }
22
+ function ige256Decrypt(data, key, iv) {
23
+ Logger_js_1.Logger.debug(`[5] Decrypting ${platform_node_js_1.Buffer.byteLength(data)} bytes data with AES-256-IGE`);
24
+ return ige(data, key, iv, false);
25
+ }
26
+ function ctr256Cipher(key, iv) {
27
+ if (platform_node_js_1.where === 'Browser') {
28
+ const cipher = new platform_node_js_1.aesjs.ModeOfOperation.ctr(key, new platform_node_js_1.aesjs.Counter(Uint8Array.from(iv)));
29
+ return (data) => {
30
+ Logger_js_1.Logger.debug(`[140] Cryptograph ${platform_node_js_1.Buffer.byteLength(data)} bytes data with AES-256-CTR`);
31
+ return platform_node_js_1.Buffer.from(cipher.encrypt(data));
32
+ };
33
+ }
34
+ try {
35
+ const cipher = platform_node_js_1.crypto.createCipheriv('AES-256-CTR', key, iv);
36
+ return (data) => {
37
+ Logger_js_1.Logger.debug(`[140] Cryptograph ${platform_node_js_1.Buffer.byteLength(data)} bytes data with AES-256-CTR`);
38
+ return platform_node_js_1.Buffer.from(cipher.update(data));
39
+ };
40
+ }
41
+ catch (_error) {
42
+ const cipher = ctr(key, iv);
43
+ return (data) => {
44
+ Logger_js_1.Logger.debug(`[140] Cryptograph ${platform_node_js_1.Buffer.byteLength(data)} bytes data with AES-256-CTR`);
45
+ return platform_node_js_1.Buffer.from(cipher.update(data));
46
+ };
47
+ }
48
+ }
49
+ function xor(a, b) {
50
+ return (0, helpers_js_1.bigintToBuffer)(BigInt((0, helpers_js_1.bufferToBigint)(a, false) ^ (0, helpers_js_1.bufferToBigint)(b, false)), platform_node_js_1.Buffer.byteLength(a), false);
51
+ }
52
+ function AES(key) {
53
+ const iv = platform_node_js_1.Buffer.alloc(0);
54
+ if (platform_node_js_1.where === 'Browser' || platform_node_js_1.where === 'Deno') {
55
+ const cipher = new platform_node_js_1.aesjs.ModeOfOperation.ecb(key);
56
+ return {
57
+ encrypt(data) {
58
+ return platform_node_js_1.Buffer.from(cipher.encrypt(data));
59
+ },
60
+ decrypt(data) {
61
+ return platform_node_js_1.Buffer.from(cipher.decrypt(data));
62
+ },
63
+ };
64
+ }
65
+ else {
66
+ const cipher = platform_node_js_1.crypto.createCipheriv('aes-256-ecb', key, iv);
67
+ const decipher = platform_node_js_1.crypto.createDecipheriv('aes-256-ecb', key, iv);
68
+ cipher.setAutoPadding(false);
69
+ decipher.setAutoPadding(false);
70
+ return {
71
+ encrypt(data) {
72
+ return platform_node_js_1.Buffer.from(cipher.update(data));
73
+ },
74
+ decrypt(data) {
75
+ return platform_node_js_1.Buffer.from(decipher.update(data));
76
+ },
77
+ };
78
+ }
79
+ }
80
+ function ige(data, key, iv, encrypt) {
81
+ const cipher = AES(key);
82
+ let iv1 = iv.subarray(0, 16);
83
+ let iv2 = iv.subarray(16, 32);
84
+ const temp = [];
85
+ for (const i of (0, helpers_js_1.range)(0, platform_node_js_1.Buffer.byteLength(data), 16)) {
86
+ temp.push(data.subarray(i, i + 16));
87
+ }
88
+ if (encrypt) {
89
+ for (let i = 0; i < temp.length; i++) {
90
+ const chunk = temp[i];
91
+ iv1 = temp[i] = xor(cipher.encrypt(xor(chunk, iv1)), iv2);
92
+ iv2 = chunk;
93
+ }
94
+ }
95
+ else {
96
+ for (let i = 0; i < temp.length; i++) {
97
+ const chunk = temp[i];
98
+ iv2 = temp[i] = xor(cipher.decrypt(xor(chunk, iv2)), iv1);
99
+ iv1 = chunk;
100
+ }
101
+ }
102
+ return platform_node_js_1.Buffer.concat(temp);
103
+ }
104
+ function ctr(key, iv, state = platform_node_js_1.Buffer.alloc(1)) {
105
+ const cipher = AES(platform_node_js_1.Buffer.from(key));
106
+ const _iv = platform_node_js_1.Buffer.from(iv);
107
+ let chunk = platform_node_js_1.Buffer.from(cipher.encrypt(iv));
108
+ return {
109
+ update: (data) => {
110
+ const out = platform_node_js_1.Buffer.from(data);
111
+ for (const i of (0, helpers_js_1.range)(0, platform_node_js_1.Buffer.byteLength(data), 16)) {
112
+ for (const j of (0, helpers_js_1.range)(0, Math.min(platform_node_js_1.Buffer.byteLength(data) - i, 16))) {
113
+ out[i + j] ^= chunk[state[0]];
114
+ state[0] += 1;
115
+ if (state[0] >= 16) {
116
+ state[0] = 0;
117
+ }
118
+ if (state[0] === 0) {
119
+ for (const k of (0, helpers_js_1.range)(15, -1, -1)) {
120
+ if (_iv[k] === 255) {
121
+ _iv[k] = 0;
122
+ }
123
+ else {
124
+ _iv[k] += 1;
125
+ break;
126
+ }
127
+ }
128
+ chunk = cipher.encrypt(_iv);
129
+ }
130
+ }
131
+ }
132
+ return out;
133
+ },
134
+ };
135
+ }
@@ -0,0 +1,5 @@
1
+ import { Buffer } from '../platform.node.js';
2
+ import { Message, BytesIO } from '../raw/index.js';
3
+ export declare function kdf(authKey: Buffer, msgKey: Buffer, outgoing: boolean): Array<Buffer>;
4
+ export declare function pack(message: Message, salt: bigint, sessionId: Buffer, authKey: Buffer, authKeyId: Buffer): Buffer;
5
+ export declare function unpack(b: BytesIO, sessionId: Buffer, authKey: Buffer, authKeyId: Buffer, storedMsgId: Array<bigint>): Promise<Message>;
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.kdf = kdf;
4
+ exports.pack = pack;
5
+ exports.unpack = unpack;
6
+ const platform_node_js_1 = require("../platform.node.js");
7
+ const index_js_1 = require("../errors/index.js");
8
+ const index_js_2 = require("../raw/index.js");
9
+ const MsgId_js_1 = require("../session/internals/MsgId.js");
10
+ const helpers_js_1 = require("../helpers.js");
11
+ const Aes_js_1 = require("./Aes.js");
12
+ const Logger_js_1 = require("../Logger.js");
13
+ const STORED_MSG_IDS_MAX_SIZE = 1000 * 2;
14
+ function sha256(data) {
15
+ const hash = platform_node_js_1.crypto.createHash('sha256');
16
+ hash.update(data);
17
+ return hash.digest();
18
+ }
19
+ function toBytes(value) {
20
+ const bytesArray = [];
21
+ for (let i = 0; i < 8; i++) {
22
+ let shift = value >> BigInt(8 * i);
23
+ shift &= BigInt(255);
24
+ bytesArray[i] = Number(String(shift));
25
+ }
26
+ return platform_node_js_1.Buffer.from(bytesArray);
27
+ }
28
+ function kdf(authKey, msgKey, outgoing) {
29
+ const x = outgoing ? 0 : 8;
30
+ const sha256A = sha256(platform_node_js_1.Buffer.concat([
31
+ msgKey,
32
+ authKey.subarray(x, x + 36),
33
+ ]));
34
+ const sha256B = sha256(platform_node_js_1.Buffer.concat([
35
+ authKey.subarray(x + 40, x + 76),
36
+ msgKey,
37
+ ]));
38
+ const aesKey = platform_node_js_1.Buffer.concat([
39
+ sha256A.subarray(0, 8),
40
+ sha256B.subarray(8, 24),
41
+ sha256A.subarray(24, 32),
42
+ ]);
43
+ const aesIv = platform_node_js_1.Buffer.concat([
44
+ sha256B.subarray(0, 8),
45
+ sha256A.subarray(8, 24),
46
+ sha256B.subarray(24, 32),
47
+ ]);
48
+ return [aesKey, aesIv];
49
+ }
50
+ function pack(message, salt, sessionId, authKey, authKeyId) {
51
+ const data = platform_node_js_1.Buffer.concat([
52
+ platform_node_js_1.Buffer.concat([
53
+ toBytes(salt),
54
+ sessionId,
55
+ ]),
56
+ message.write(),
57
+ ]);
58
+ const padding = platform_node_js_1.Buffer.from(platform_node_js_1.crypto.randomBytes((0, helpers_js_1.mod)(-(platform_node_js_1.Buffer.byteLength(data) + 12), 16) + 12));
59
+ const msgKeyLarge = sha256(platform_node_js_1.Buffer.concat([
60
+ authKey.subarray(88, 88 + 32),
61
+ data,
62
+ padding,
63
+ ]));
64
+ const msgKey = msgKeyLarge.subarray(8, 24);
65
+ const [aesKey, aesIv] = kdf(authKey, msgKey, true);
66
+ return platform_node_js_1.Buffer.concat([
67
+ authKeyId,
68
+ msgKey,
69
+ (0, Aes_js_1.ige256Encrypt)(platform_node_js_1.Buffer.concat([data, padding]), aesKey, aesIv),
70
+ ]);
71
+ }
72
+ async function unpack(b, sessionId, authKey, authKeyId, storedMsgId) {
73
+ index_js_1.SecurityCheckMismatch.check(b.read(8).equals(authKeyId), 'Provided auth key id is not equal with expected one.');
74
+ const msgKey = b.read(16);
75
+ const [aesKey, aesIv] = kdf(authKey, msgKey, false);
76
+ const encrypted = b.read();
77
+ const decrypted = (0, Aes_js_1.ige256Decrypt)(encrypted, aesKey, aesIv);
78
+ const hash = sha256(platform_node_js_1.Buffer.concat([
79
+ authKey.subarray(96, 96 + 32),
80
+ decrypted,
81
+ ]));
82
+ index_js_1.SecurityCheckMismatch.check(msgKey.equals(hash.subarray(8, 24)), 'Provided msg key is not equal with expected one');
83
+ const data = new index_js_2.BytesIO(decrypted);
84
+ data.read(8);
85
+ index_js_1.SecurityCheckMismatch.check(platform_node_js_1.Buffer.from(data.read(8)).equals(sessionId), 'Provided session id is not equal with expected one.');
86
+ const message = await index_js_2.Message.read(new index_js_2.BytesIO(data.buffer.slice(16))).catch((error) => {
87
+ Logger_js_1.Logger.error(error);
88
+ });
89
+ data.seek(32);
90
+ const payload = data.read();
91
+ const padding = payload.subarray(message.length);
92
+ index_js_1.SecurityCheckMismatch.check(platform_node_js_1.Buffer.byteLength(padding) >= 12 && platform_node_js_1.Buffer.byteLength(padding) <= 1024, 'Payload padding is lower than 12 or bigger than 1024');
93
+ index_js_1.SecurityCheckMismatch.check((0, helpers_js_1.mod)(platform_node_js_1.Buffer.byteLength(padding), 4) === 0, 'Mod of padding length with 4 is equal with zero');
94
+ index_js_1.SecurityCheckMismatch.check((0, helpers_js_1.bigIntMod)(message.msgId, BigInt(2)) !== BigInt(0), 'Mod of msgId with 2 is not equal with zero');
95
+ if (storedMsgId.length > STORED_MSG_IDS_MAX_SIZE) {
96
+ storedMsgId.splice(0, Math.floor(STORED_MSG_IDS_MAX_SIZE / 2));
97
+ }
98
+ if (storedMsgId.length) {
99
+ if (message.msgId < storedMsgId[0]) {
100
+ throw new index_js_1.SecurityCheckMismatch('Msg id is lower than all of the stored values');
101
+ }
102
+ if (storedMsgId.includes(message.msgId)) {
103
+ throw new index_js_1.SecurityCheckMismatch('Msg id is equal to any of the stored values');
104
+ }
105
+ const msgId = new MsgId_js_1.MsgId();
106
+ const timeDiff = BigInt(message.msgId - msgId.getMsgId()) / BigInt(2 ** 32);
107
+ if (timeDiff > BigInt(30)) {
108
+ throw new index_js_1.SecurityCheckMismatch('Msg id belongs over 30 seconds in the future');
109
+ }
110
+ if (timeDiff < BigInt(-300)) {
111
+ throw new index_js_1.SecurityCheckMismatch('Msg id belongs over 300 seconds in the past');
112
+ }
113
+ }
114
+ storedMsgId.push(message.msgId);
115
+ storedMsgId.sort((a, b) => {
116
+ if (a > b)
117
+ return 1;
118
+ if (a < b)
119
+ return -1;
120
+ return 0;
121
+ });
122
+ return message;
123
+ }
@@ -0,0 +1,5 @@
1
+ import { Raw } from '../raw/index.js';
2
+ import { Buffer } from '../platform.node.js';
3
+ export declare function xor(a: Buffer, b: Buffer): Buffer<ArrayBufferLike>;
4
+ export declare function computePasswordHash(algo: Raw.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow, password: string): Buffer;
5
+ export declare function computePasswordCheck(r: Raw.account.Password, password: string): Raw.InputCheckPasswordSRP;
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.xor = xor;
4
+ exports.computePasswordHash = computePasswordHash;
5
+ exports.computePasswordCheck = computePasswordCheck;
6
+ const index_js_1 = require("../raw/index.js");
7
+ const platform_node_js_1 = require("../platform.node.js");
8
+ const helpers_js_1 = require("../helpers.js");
9
+ function sha256(data) {
10
+ return platform_node_js_1.crypto.createHash('sha256').update(data).digest();
11
+ }
12
+ function xor(a, b) {
13
+ const length = Math.min(platform_node_js_1.Buffer.byteLength(a), platform_node_js_1.Buffer.byteLength(b));
14
+ for (let i = 0; i < length; i++) {
15
+ a[i] =
16
+ a[i] ^ b[i];
17
+ }
18
+ return a;
19
+ }
20
+ function computePasswordHash(algo, password) {
21
+ const hash1 = sha256(platform_node_js_1.Buffer.concat([
22
+ algo.salt1,
23
+ platform_node_js_1.Buffer.from(password, 'utf8'),
24
+ algo.salt1,
25
+ ]));
26
+ const hash2 = sha256(platform_node_js_1.Buffer.concat([
27
+ algo.salt2,
28
+ hash1,
29
+ algo.salt2,
30
+ ]));
31
+ const hash3 = platform_node_js_1.crypto.pbkdf2Sync(hash2, algo.salt1, 100000, 64, 'sha512');
32
+ return sha256(platform_node_js_1.Buffer.concat([
33
+ algo.salt2,
34
+ hash3,
35
+ algo.salt2,
36
+ ]));
37
+ }
38
+ function computePasswordCheck(r, password) {
39
+ const algo = r.currentAlgo;
40
+ const pBytes = algo.p;
41
+ const p = btoi(pBytes);
42
+ const g = algo.g;
43
+ const gBytes = itob(BigInt(g));
44
+ const BBytes = r.srpB;
45
+ const B = btoi(BBytes);
46
+ const srpId = r.srpId;
47
+ const xBytes = computePasswordHash(algo, password);
48
+ const x = btoi(xBytes);
49
+ const gX = (0, helpers_js_1.bigIntPow)(BigInt(g), x, p);
50
+ const kBytes = sha256(platform_node_js_1.Buffer.concat([pBytes, gBytes]));
51
+ const k = btoi(kBytes);
52
+ const kGX = (0, helpers_js_1.bigIntMod)(k * gX, p);
53
+ let aBytes;
54
+ let a;
55
+ let A;
56
+ let ABytes;
57
+ let u;
58
+ while (true) {
59
+ aBytes = platform_node_js_1.crypto.randomBytes(256);
60
+ a = btoi(aBytes);
61
+ A = (0, helpers_js_1.bigIntPow)(BigInt(g), a, p);
62
+ ABytes = itob(A);
63
+ u = btoi(sha256(platform_node_js_1.Buffer.concat([ABytes, BBytes])));
64
+ if (u > BigInt(0))
65
+ break;
66
+ }
67
+ const gB = (0, helpers_js_1.bigIntMod)(B - kGX, p);
68
+ const uX = u * x;
69
+ const aUX = a + uX;
70
+ const S = (0, helpers_js_1.bigIntPow)(gB, aUX, p);
71
+ const SBytes = itob(S);
72
+ const KBytes = sha256(SBytes);
73
+ const M1Bytes = sha256(platform_node_js_1.Buffer.concat([
74
+ xor(sha256(pBytes), sha256(gBytes)),
75
+ sha256(algo.salt1),
76
+ sha256(algo.salt2),
77
+ ABytes,
78
+ BBytes,
79
+ KBytes,
80
+ ]));
81
+ return new index_js_1.Raw.InputCheckPasswordSRP({
82
+ srpId: srpId,
83
+ a: ABytes,
84
+ m1: M1Bytes,
85
+ });
86
+ }
87
+ function btoi(b) {
88
+ return (0, helpers_js_1.bufferToBigint)(b, false);
89
+ }
90
+ function itob(i) {
91
+ return (0, helpers_js_1.bigintToBuffer)(i, 256, false);
92
+ }
@@ -0,0 +1,4 @@
1
+ declare const CURRENT_DH_PRIME: bigint;
2
+ export declare function gcd(a: bigint, b: bigint): bigint;
3
+ export declare function decompose(pq: bigint): bigint;
4
+ export { CURRENT_DH_PRIME };
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CURRENT_DH_PRIME = void 0;
4
+ exports.gcd = gcd;
5
+ exports.decompose = decompose;
6
+ const helpers_js_1 = require("../helpers.js");
7
+ const CURRENT_DH_PRIME = BigInt('0x' +
8
+ 'C71CAEB9C6B1C9048E6C522F70F13F73980D40238E3E21C14934D037563D930F' +
9
+ '48198A0AA7C14058229493D22530F4DBFA336F6E0AC925139543AED44CCE7C37' +
10
+ '20FD51F69458705AC68CD4FE6B6B13ABDC9746512969328454F18FAF8C595F64' +
11
+ '2477FE96BB2A941D5BCD1D4AC8CC49880708FA9B378E3C4F3A9060BEE67CF9A4' +
12
+ 'A4A695811051907E162753B56B0F6B410DBA74D8A84B2A14B3144E0EF1284754' +
13
+ 'FD17ED950D5965B4B9DD46582DB1178D169C6BC465B0D6FF9CA3928FEF5B9AE4' +
14
+ 'E418FC15E83EBEA0F87FA9FF5EED70050DED2849F47BF959D956850CE929851F' +
15
+ '0D8115F635B105EE2E4E15D04B2454BF6F4FADF034B10403119CD8E3B92FCC5B');
16
+ exports.CURRENT_DH_PRIME = CURRENT_DH_PRIME;
17
+ function gcd(a, b) {
18
+ while (b) {
19
+ const c = (0, helpers_js_1.bigIntMod)(a, b);
20
+ a = b;
21
+ b = c;
22
+ }
23
+ return a;
24
+ }
25
+ function decompose(pq) {
26
+ if (pq == BigInt(1))
27
+ return pq;
28
+ if ((0, helpers_js_1.bigIntMod)(pq, BigInt(2)) === BigInt(0))
29
+ return BigInt(2);
30
+ let y = (0, helpers_js_1.randBigint)(BigInt(1), pq - BigInt(1));
31
+ const c = (0, helpers_js_1.randBigint)(BigInt(1), pq - BigInt(1));
32
+ const m = (0, helpers_js_1.randBigint)(BigInt(1), pq - BigInt(1));
33
+ let g = BigInt(1);
34
+ let r = BigInt(1);
35
+ let q = BigInt(1);
36
+ let x = BigInt(0);
37
+ let ys = BigInt(0);
38
+ while (g === BigInt(1)) {
39
+ x = y;
40
+ for (let i = 0; BigInt(i) < r; i++) {
41
+ y = (0, helpers_js_1.bigIntMod)((0, helpers_js_1.bigIntPow)(y, BigInt(2), pq) + c, pq);
42
+ }
43
+ let k = BigInt(0);
44
+ while (k < r && g === BigInt(1)) {
45
+ ys = y;
46
+ for (let i = 0; BigInt(i) < helpers_js_1.bigMath.min(m, r - k); i++) {
47
+ y = (0, helpers_js_1.bigIntMod)((0, helpers_js_1.bigIntPow)(y, BigInt(2), pq) + c, pq);
48
+ q = q * (0, helpers_js_1.bigIntMod)(helpers_js_1.bigMath.abs(BigInt(x - y)), pq);
49
+ }
50
+ g = gcd(q, pq);
51
+ k += m;
52
+ }
53
+ r *= BigInt(2);
54
+ if (g === pq) {
55
+ while (true) {
56
+ ys = (0, helpers_js_1.bigIntMod)((0, helpers_js_1.bigIntPow)(ys, BigInt(2), pq) + c, pq);
57
+ g = gcd(helpers_js_1.bigMath.abs(BigInt(x - ys)), pq);
58
+ if (g > BigInt(1))
59
+ break;
60
+ }
61
+ }
62
+ }
63
+ return g;
64
+ }
@@ -0,0 +1,7 @@
1
+ import { Buffer } from '../platform.node.js';
2
+ declare const PublicKey: Map<bigint, {
3
+ m: bigint;
4
+ e: bigint;
5
+ }>;
6
+ export declare function encrypt(data: Buffer, fingerprint: bigint): Buffer<ArrayBuffer>;
7
+ export { PublicKey };
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PublicKey = void 0;
4
+ exports.encrypt = encrypt;
5
+ const helpers_js_1 = require("../helpers.js");
6
+ const platform_node_js_1 = require("../platform.node.js");
7
+ const PublicKey = new Map();
8
+ exports.PublicKey = PublicKey;
9
+ PublicKey.set(BigInt('-4344800451088585951'), {
10
+ m: BigInt('0x' +
11
+ 'C150023E2F70DB7985DED064759CFECF0AF328E69A41DAF4D6F01B538135A6F9' +
12
+ '1F8F8B2A0EC9BA9720CE352EFCF6C5680FFC424BD634864902DE0B4BD6D49F4E' +
13
+ '580230E3AE97D95C8B19442B3C0A10D8F5633FECEDD6926A7F6DAB0DDB7D457F' +
14
+ '9EA81B8465FCD6FFFEED114011DF91C059CAEDAF97625F6C96ECC74725556934' +
15
+ 'EF781D866B34F011FCE4D835A090196E9A5F0E4449AF7EB697DDB9076494CA5F' +
16
+ '81104A305B6DD27665722C46B60E5DF680FB16B210607EF217652E60236C255F' +
17
+ '6A28315F4083A96791D7214BF64C1DF4FD0DB1944FB26A2A57031B32EEE64AD1' +
18
+ '5A8BA68885CDE74A5BFC920F6ABF59BA5C75506373E7130F9042DA922179251F'),
19
+ e: BigInt('0x010001'),
20
+ });
21
+ PublicKey.set(BigInt('847625836280919973'), {
22
+ m: BigInt('0x' +
23
+ 'AEEC36C8FFC109CB099624685B97815415657BD76D8C9C3E398103D7AD16C9BB' +
24
+ 'A6F525ED0412D7AE2C2DE2B44E77D72CBF4B7438709A4E646A05C43427C7F184' +
25
+ 'DEBF72947519680E651500890C6832796DD11F772C25FF8F576755AFE055B0A3' +
26
+ '752C696EB7D8DA0D8BE1FAF38C9BDD97CE0A77D3916230C4032167100EDD0F9E' +
27
+ '7A3A9B602D04367B689536AF0D64B613CCBA7962939D3B57682BEB6DAE5B6081' +
28
+ '30B2E52ACA78BA023CF6CE806B1DC49C72CF928A7199D22E3D7AC84E47BC9427' +
29
+ 'D0236945D10DBD15177BAB413FBF0EDFDA09F014C7A7DA088DDE9759702CA760' +
30
+ 'AF2B8E4E97CC055C617BD74C3D97008635B98DC4D621B4891DA9FB0473047927'),
31
+ e: BigInt('0x010001'),
32
+ });
33
+ PublicKey.set(BigInt('1562291298945373506'), {
34
+ m: BigInt('0x' +
35
+ 'BDF2C77D81F6AFD47BD30F29AC76E55ADFE70E487E5E48297E5A9055C9C07D2B' +
36
+ '93B4ED3994D3ECA5098BF18D978D54F8B7C713EB10247607E69AF9EF44F38E28' +
37
+ 'F8B439F257A11572945CC0406FE3F37BB92B79112DB69EEDF2DC71584A661638' +
38
+ 'EA5BECB9E23585074B80D57D9F5710DD30D2DA940E0ADA2F1B878397DC1A72B5' +
39
+ 'CE2531B6F7DD158E09C828D03450CA0FF8A174DEACEBCAA22DDE84EF66AD370F' +
40
+ '259D18AF806638012DA0CA4A70BAA83D9C158F3552BC9158E69BF332A45809E1' +
41
+ 'C36905A5CAA12348DD57941A482131BE7B2355A5F4635374F3BD3DDF5FF925BF' +
42
+ '4809EE27C1E67D9120C5FE08A9DE458B1B4A3C5D0A428437F2BECA81F4E2D5FF'),
43
+ e: BigInt('0x010001'),
44
+ });
45
+ PublicKey.set(BigInt('-5859577972006586033'), {
46
+ m: BigInt('0x' +
47
+ 'B3F762B739BE98F343EB1921CF0148CFA27FF7AF02B6471213FED9DAA0098976' +
48
+ 'E667750324F1ABCEA4C31E43B7D11F1579133F2B3D9FE27474E462058884E5E1' +
49
+ 'B123BE9CBBC6A443B2925C08520E7325E6F1A6D50E117EB61EA49D2534C8BB4D' +
50
+ '2AE4153FABE832B9EDF4C5755FDD8B19940B81D1D96CF433D19E6A22968A85DC' +
51
+ '80F0312F596BD2530C1CFB28B5FE019AC9BC25CD9C2A5D8A0F3A1C0C79BCCA52' +
52
+ '4D315B5E21B5C26B46BABE3D75D06D1CD33329EC782A0F22891ED1DB42A1D6C0' +
53
+ 'DEA431428BC4D7AABDCF3E0EB6FDA4E23EB7733E7727E9A1915580796C55188D' +
54
+ '2596D2665AD1182BA7ABF15AAA5A8B779EA996317A20AE044B820BFF35B6E8A1'),
55
+ e: BigInt('0x010001'),
56
+ });
57
+ PublicKey.set(BigInt('6491968696586960280'), {
58
+ m: BigInt('0x' +
59
+ 'BE6A71558EE577FF03023CFA17AAB4E6C86383CFF8A7AD38EDB9FAFE6F323F2D' +
60
+ '5106CBC8CAFB83B869CFFD1CCF121CD743D509E589E68765C96601E813DC5B9D' +
61
+ 'FC4BE415C7A6526132D0035CA33D6D6075D4F535122A1CDFE017041F1088D141' +
62
+ '9F65C8E5490EE613E16DBF662698C0F54870F0475FA893FC41EB55B08FF1AC21' +
63
+ '1BC045DED31BE27D12C96D8D3CFC6A7AE8AA50BF2EE0F30ED507CC2581E3DEC5' +
64
+ '6DE94F5DC0A7ABEE0BE990B893F2887BD2C6310A1E0A9E3E38BD34FDED254150' +
65
+ '8DC102A9C9B4C95EFFD9DD2DFE96C29BE647D6C69D66CA500843CFAED6E44019' +
66
+ '6F1DBE0E2E22163C61CA48C79116FA77216726749A976A1C4B0944B5121E8C01'),
67
+ e: BigInt('0x010001'),
68
+ });
69
+ PublicKey.set(BigInt('6427105915145367799'), {
70
+ m: BigInt('0x' +
71
+ 'F8B7F73EF804D72C5B25408C6840245744324935699DA0E389E76707945BB4D5' +
72
+ 'A309EA9255A9181DBAAA18C208BF958219D15DAEA39F30D70D4ACB4FB5253A47' +
73
+ 'D526470EADAAE388CA4A52B943A37BD1FEE175482AABA3C8BD8849D2BEE1938C' +
74
+ '978842324A9ABB0E1B3F549BAF4DEF65141B53AA84034E15E23F3BF410320558' +
75
+ '6BDD61BDF998BEB795DF1924E0484C4F60497CAD934760D579441F81BABA151F' +
76
+ '61CB4CEA53FE62557E2918A608DF585E6575ECD5E16A3D2D21F471919214869E' +
77
+ '265F1DD00F048B2E41F60B413BC98BF977D044A38E9ABEDAE01338468D9D7B9A' +
78
+ 'EBDA2DA877B8585DDDC33BD1514A5E32D7303C026E3C45F77DE561C5DCDFCE99'),
79
+ e: BigInt('0x010001'),
80
+ });
81
+ PublicKey.set(BigInt('2685959930972952888'), {
82
+ m: BigInt('0x' +
83
+ 'CEE1D50BBB04E742A1A3FC83559B569E5980E417FF68CF0A658DD6CD2D7AC3AC' +
84
+ '35B01AA2A63F2880C186ED42DB181B5898A11A23B20824EE963369B531A5D59E' +
85
+ 'CA92F1DECF6860198B2F2B48DDD2ED2D9AF30A7845765E86CD09017BD9788CF8' +
86
+ 'E6207208C05FC9C6C92A64B079891EB11508EE150EF1E4219A6FD4614129258E' +
87
+ 'D53ADD087A68AE5114A9AA5450D8595CC876A161435CBDB2026F8FBF00FEDCA0' +
88
+ 'A067E9C079172CCCECC09C2B16C428EC776373149DB66AAB9A4DEBF7916B391E' +
89
+ '832AE5A7892E27DE0AB1B4451C55F90F1F2ECE3ACEF708BC2C5EE022066EE434' +
90
+ '4C7268D724AABAAC667667D727AC3F2956ED4BDAF7089DDE0AEB18A6652DA16F'),
91
+ e: BigInt('0x010001'),
92
+ });
93
+ function encrypt(data, fingerprint) {
94
+ const key = PublicKey.get(fingerprint);
95
+ if (key == undefined) {
96
+ throw new Error(`unknown fingerprint ${fingerprint}n`);
97
+ }
98
+ return platform_node_js_1.Buffer.from((0, helpers_js_1.bigintToBuffer)((0, helpers_js_1.bigIntPow)((0, helpers_js_1.bufferToBigint)(data, false), key.e, key.m), 256, false));
99
+ }
@@ -0,0 +1,5 @@
1
+ import { Buffer } from '../platform.node.js';
2
+ import { Raw } from '../raw/index.js';
3
+ export declare function kdf(sharedKey: Buffer, msgKey: Buffer, isAdmin: boolean, v1?: boolean): Array<Buffer>;
4
+ export declare function pack(message: Raw.TypeDecryptedMessage, sharedKey: Buffer, inSeqNo: number, outSeqNo: number, isAdmin: boolean, layer: number, mtproto?: number): Buffer;
5
+ export declare function unpack(message: Raw.TypeEncryptedMessage, sharedKey: Buffer, isAdmin: boolean, mtproto?: number): Promise<Raw.TypeDecryptedMessage>;