@mtkruto/node 0.0.999 → 0.1.100
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/esm/0_deps.d.ts +1 -1
- package/esm/0_deps.js +1 -1
- package/esm/3_errors.d.ts +1531 -0
- package/esm/3_errors.js +1531 -0
- package/esm/4_constants.d.ts +1 -1
- package/esm/4_constants.js +1 -1
- package/esm/4_errors.d.ts +25 -0
- package/esm/4_errors.js +64 -0
- package/esm/client/2_client_plain.js +1 -1
- package/esm/client/4_client.d.ts +2 -1
- package/esm/client/4_client.js +24 -41
- package/esm/deps/deno.land/x/{tgcrypto@0.1.3 → tgcrypto@0.3.2}/mod.d.ts +2 -6
- package/esm/deps/deno.land/x/tgcrypto@0.3.2/mod.js +158 -0
- package/esm/deps/deno.land/x/tgcrypto@0.3.2/tgcrypto.d.ts +2 -0
- package/esm/deps/deno.land/x/tgcrypto@0.3.2/tgcrypto.js +308 -0
- package/esm/mod.d.ts +3 -2
- package/esm/mod.js +3 -2
- package/esm/tl/2_types.d.ts +1 -1
- package/esm/tl/2_types.js +1 -2
- package/esm/tl/3_functions.d.ts +1 -1
- package/esm/tl/3_functions.js +1 -2
- package/esm/transport/0_obfuscation.js +1 -1
- package/esm/transport/0_transport.js +2 -2
- package/esm/types/0__file_id.js +0 -1
- package/esm/types/1_bot_command_scope.d.ts +1 -1
- package/esm/types/1_bot_command_scope.js +1 -1
- package/esm/types/1_chat.d.ts +1 -1
- package/esm/types/1_chat.js +1 -1
- package/esm/types/1_sticker.d.ts +1 -1
- package/esm/types/1_sticker.js +1 -1
- package/esm/types/1_user.d.ts +1 -1
- package/esm/types/1_user.js +1 -1
- package/esm/types/2_inline_keyboard_button.d.ts +1 -1
- package/esm/types/3_message.d.ts +12 -12
- package/esm/types/3_message.js +12 -12
- package/esm/types/5_inline_query_result.d.ts +1 -1
- package/esm/types/5_inline_query_result.js +1 -1
- package/esm/utilities/0_crypto.d.ts +1 -2
- package/esm/utilities/0_crypto.js +5 -12
- package/package.json +1 -1
- package/script/0_deps.d.ts +1 -1
- package/script/0_deps.js +3 -4
- package/script/3_errors.d.ts +1531 -0
- package/script/3_errors.js +2053 -0
- package/script/4_constants.d.ts +1 -1
- package/script/4_constants.js +1 -1
- package/script/4_errors.d.ts +25 -0
- package/script/4_errors.js +88 -0
- package/script/client/2_client_plain.js +1 -1
- package/script/client/4_client.d.ts +2 -1
- package/script/client/4_client.js +24 -41
- package/script/deps/deno.land/x/{tgcrypto@0.1.3 → tgcrypto@0.3.2}/mod.d.ts +2 -6
- package/script/deps/deno.land/x/tgcrypto@0.3.2/mod.js +171 -0
- package/script/deps/deno.land/x/tgcrypto@0.3.2/tgcrypto.d.ts +2 -0
- package/script/deps/deno.land/x/tgcrypto@0.3.2/tgcrypto.js +310 -0
- package/script/mod.d.ts +3 -2
- package/script/mod.js +16 -3
- package/script/tl/2_types.d.ts +1 -1
- package/script/tl/2_types.js +0 -1
- package/script/tl/3_functions.d.ts +1 -1
- package/script/tl/3_functions.js +0 -1
- package/script/transport/0_obfuscation.js +1 -1
- package/script/transport/0_transport.js +2 -2
- package/script/types/0__file_id.js +0 -1
- package/script/types/1_bot_command_scope.d.ts +1 -1
- package/script/types/1_bot_command_scope.js +1 -1
- package/script/types/1_chat.d.ts +1 -1
- package/script/types/1_chat.js +1 -1
- package/script/types/1_sticker.d.ts +1 -1
- package/script/types/1_sticker.js +1 -1
- package/script/types/1_user.d.ts +1 -1
- package/script/types/1_user.js +1 -1
- package/script/types/2_inline_keyboard_button.d.ts +1 -1
- package/script/types/3_message.d.ts +12 -12
- package/script/types/3_message.js +12 -12
- package/script/types/5_inline_query_result.d.ts +1 -1
- package/script/types/5_inline_query_result.js +1 -1
- package/script/utilities/0_crypto.d.ts +1 -2
- package/script/utilities/0_crypto.js +4 -11
- package/esm/deps/deno.land/x/tgcrypto@0.1.3/mod.js +0 -68
- package/esm/deps/deno.land/x/tgcrypto@0.1.3/tgcrypto.d.ts +0 -2
- package/esm/deps/deno.land/x/tgcrypto@0.1.3/tgcrypto.js +0 -1078
- package/script/deps/deno.land/x/tgcrypto@0.1.3/mod.js +0 -82
- package/script/deps/deno.land/x/tgcrypto@0.1.3/tgcrypto.d.ts +0 -2
- package/script/deps/deno.land/x/tgcrypto@0.1.3/tgcrypto.js +0 -1080
package/esm/4_constants.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export declare const PUBLIC_KEYS: PublicKeys;
|
|
|
5
5
|
export declare const VECTOR_CONSTRUCTOR = 481674261;
|
|
6
6
|
export declare const INITIAL_DC: DC;
|
|
7
7
|
export declare const LAYER = 161;
|
|
8
|
-
export declare const APP_VERSION = "MTKruto 0.
|
|
8
|
+
export declare const APP_VERSION = "MTKruto 0.1.100";
|
|
9
9
|
export declare const DEVICE_MODEL: string;
|
|
10
10
|
export declare const LANG_CODE: string;
|
|
11
11
|
export declare const LANG_PACK = "";
|
package/esm/4_constants.js
CHANGED
|
@@ -54,7 +54,7 @@ export const PUBLIC_KEYS = Object.freeze([
|
|
|
54
54
|
export const VECTOR_CONSTRUCTOR = 0x1CB5C415;
|
|
55
55
|
export const INITIAL_DC = "2-test";
|
|
56
56
|
export const LAYER = 161;
|
|
57
|
-
export const APP_VERSION = "MTKruto 0.
|
|
57
|
+
export const APP_VERSION = "MTKruto 0.1.100";
|
|
58
58
|
// @ts-ignore: lib
|
|
59
59
|
export const DEVICE_MODEL = typeof dntShim.Deno === "undefined" ? typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : process.platform + "-" + process.arch : navigator.userAgent.split(" ")[0] : dntShim.Deno.build.os + "-" + dntShim.Deno.build.arch;
|
|
60
60
|
export const LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { types } from "./2_tl.js";
|
|
2
|
+
export * from "./3_errors.js";
|
|
3
|
+
export declare class FloodWait extends types.RPCError {
|
|
4
|
+
seconds: number;
|
|
5
|
+
constructor(params: {
|
|
6
|
+
errorCode: number;
|
|
7
|
+
errorMessage: string;
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
export declare class Migrate extends types.RPCError {
|
|
11
|
+
dc: number;
|
|
12
|
+
constructor(params: {
|
|
13
|
+
errorCode: number;
|
|
14
|
+
errorMessage: string;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
export declare class UserMigrate extends Migrate {
|
|
18
|
+
}
|
|
19
|
+
export declare class PhoneMigrate extends Migrate {
|
|
20
|
+
}
|
|
21
|
+
export declare class FileMigrate extends Migrate {
|
|
22
|
+
}
|
|
23
|
+
export declare class StatsMigrate extends Migrate {
|
|
24
|
+
}
|
|
25
|
+
export declare function upgradeInstance(error: types.RPCError): types.RPCError;
|
package/esm/4_errors.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { UNREACHABLE } from "./1_utilities.js";
|
|
2
|
+
import { map } from "./3_errors.js";
|
|
3
|
+
import { types } from "./2_tl.js";
|
|
4
|
+
export * from "./3_errors.js";
|
|
5
|
+
export class FloodWait extends types.RPCError {
|
|
6
|
+
constructor(params) {
|
|
7
|
+
super(params);
|
|
8
|
+
Object.defineProperty(this, "seconds", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
configurable: true,
|
|
11
|
+
writable: true,
|
|
12
|
+
value: void 0
|
|
13
|
+
});
|
|
14
|
+
const p = params.errorMessage.split("_");
|
|
15
|
+
this.seconds = Number(p[p.length - 1]);
|
|
16
|
+
if (isNaN(this.seconds)) {
|
|
17
|
+
UNREACHABLE();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export class Migrate extends types.RPCError {
|
|
22
|
+
constructor(params) {
|
|
23
|
+
super(params);
|
|
24
|
+
Object.defineProperty(this, "dc", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
configurable: true,
|
|
27
|
+
writable: true,
|
|
28
|
+
value: void 0
|
|
29
|
+
});
|
|
30
|
+
const p = params.errorMessage.split("_");
|
|
31
|
+
this.dc = Number(p[p.length - 1]);
|
|
32
|
+
if (isNaN(this.dc)) {
|
|
33
|
+
UNREACHABLE();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export class UserMigrate extends Migrate {
|
|
38
|
+
}
|
|
39
|
+
export class PhoneMigrate extends Migrate {
|
|
40
|
+
}
|
|
41
|
+
export class FileMigrate extends Migrate {
|
|
42
|
+
}
|
|
43
|
+
export class StatsMigrate extends Migrate {
|
|
44
|
+
}
|
|
45
|
+
const prefixMap = {
|
|
46
|
+
"FILE_MIGRATE_": FileMigrate,
|
|
47
|
+
"PHONE_MIGRATE_": PhoneMigrate,
|
|
48
|
+
"USER_MIGRATE_": UserMigrate,
|
|
49
|
+
"STATS_MIGRATE_": StatsMigrate,
|
|
50
|
+
"FLOOD_WAIT_": FloodWait,
|
|
51
|
+
};
|
|
52
|
+
export function upgradeInstance(error) {
|
|
53
|
+
for (const [k, v] of Object.entries(prefixMap)) {
|
|
54
|
+
if (error.errorMessage.startsWith(k)) {
|
|
55
|
+
return new v(error);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
for (const [k, v] of Object.entries(map)) {
|
|
59
|
+
if (error.errorMessage == k) {
|
|
60
|
+
return new v(error);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return error;
|
|
64
|
+
}
|
|
@@ -2,8 +2,8 @@ import { assertEquals, assertInstanceOf, debug, factorize, ige256Decrypt, ige256
|
|
|
2
2
|
import { bigIntFromBuffer, bufferFromBigInt, concat, getRandomBigInt, modExp, rsaPad, sha1, UNREACHABLE } from "../1_utilities.js";
|
|
3
3
|
import { functions, serialize, TLReader, types } from "../2_tl.js";
|
|
4
4
|
import { PUBLIC_KEYS } from "../4_constants.js";
|
|
5
|
-
import { ClientAbstract } from "./1_client_abstract.js";
|
|
6
5
|
import { getMessageId, packUnencryptedMessage, unpackUnencryptedMessage } from "./0_message.js";
|
|
6
|
+
import { ClientAbstract } from "./1_client_abstract.js";
|
|
7
7
|
const d = debug("ClientPlain/createAuthKey");
|
|
8
8
|
export class ClientPlain extends ClientAbstract {
|
|
9
9
|
constructor(params) {
|
package/esm/client/4_client.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { functions, ReadObject, types } from "../2_tl.js";
|
|
|
3
3
|
import { Storage } from "../3_storage.js";
|
|
4
4
|
import { DC } from "../3_transport.js";
|
|
5
5
|
import { BotCommand, BotCommandScope, ChatAction, ChatID, InlineQueryResult, InlineQueryResultButton, Message } from "../3_types.js";
|
|
6
|
+
import { Migrate } from "../4_errors.js";
|
|
6
7
|
import { With } from "./0_utilities.js";
|
|
7
8
|
import { ClientAbstract } from "./1_client_abstract.js";
|
|
8
9
|
import { AnswerCallbackQueryParams, AuthorizeUserParams, ClientParams, EditMessageParams, FilterableUpdates, FilterUpdate, ForwardMessagesParams, Handler, ParseMode, SendMessagesParams, SendPollParams, Update } from "./3_types.js";
|
|
@@ -60,7 +61,7 @@ export declare class Client extends ClientAbstract {
|
|
|
60
61
|
private assertUser;
|
|
61
62
|
private assertBot;
|
|
62
63
|
private fetchState;
|
|
63
|
-
[handleMigrationError](err:
|
|
64
|
+
[handleMigrationError](err: Migrate): Promise<void>;
|
|
64
65
|
private connectionInited;
|
|
65
66
|
disconnect(): Promise<void>;
|
|
66
67
|
private initConnection;
|
package/esm/client/4_client.js
CHANGED
|
@@ -4,12 +4,13 @@ import { as, functions, getChannelChatId, Message_, MessageContainer, peerToChat
|
|
|
4
4
|
import { StorageMemory } from "../3_storage.js";
|
|
5
5
|
import { botCommandScopeToTlObject, constructCallbackQuery, constructInlineQuery, constructMessage, constructUser, FileID, FileType, inlineQueryResultToTlObject, messageEntityToTlObject, replyMarkupToTlObject, ThumbnailSource } from "../3_types.js";
|
|
6
6
|
import { ACK_THRESHOLD, APP_VERSION, CHANNEL_DIFFERENCE_LIMIT_BOT, CHANNEL_DIFFERENCE_LIMIT_USER, DEVICE_MODEL, LANG_CODE, LANG_PACK, LAYER, MAX_CHANNEL_ID, MAX_CHAT_ID, STICKER_SET_NAME_TTL, SYSTEM_LANG_CODE, SYSTEM_VERSION, USERNAME_TTL, ZERO_CHANNEL_ID } from "../4_constants.js";
|
|
7
|
-
import {
|
|
7
|
+
import { AuthKeyUnregistered, Migrate, PasswordHashInvalid, PhoneNumberInvalid, SessionPasswordNeeded, upgradeInstance } from "../4_errors.js";
|
|
8
|
+
import { parseHtml } from "./0_html.js";
|
|
8
9
|
import { decryptMessage, encryptMessage, getMessageId } from "./0_message.js";
|
|
9
10
|
import { checkPassword } from "./0_password.js";
|
|
10
|
-
import {
|
|
11
|
-
import { ClientPlain } from "./2_client_plain.js";
|
|
11
|
+
import { isChannelPtsUpdate, isPtsUpdate, resolve } from "./0_utilities.js";
|
|
12
12
|
import { ClientAbstract } from "./1_client_abstract.js";
|
|
13
|
+
import { ClientPlain } from "./2_client_plain.js";
|
|
13
14
|
import { skip } from "./3_types.js";
|
|
14
15
|
const d = debug("Client");
|
|
15
16
|
const dGap = debug("Client/recoverUpdateGap");
|
|
@@ -327,18 +328,12 @@ export class Client extends ClientAbstract {
|
|
|
327
328
|
d("state fetched [%s]", source);
|
|
328
329
|
}
|
|
329
330
|
async [handleMigrationError](err) {
|
|
330
|
-
|
|
331
|
-
if (
|
|
332
|
-
|
|
333
|
-
if (Math.abs(this.dcId) >= 10000) {
|
|
334
|
-
newDc += "-test";
|
|
335
|
-
}
|
|
336
|
-
await this.reconnect(newDc);
|
|
337
|
-
d("migrated to DC%s", newDc);
|
|
338
|
-
}
|
|
339
|
-
else {
|
|
340
|
-
UNREACHABLE();
|
|
331
|
+
let newDc = String(err.dc);
|
|
332
|
+
if (Math.abs(this.dcId) >= 10000) {
|
|
333
|
+
newDc += "-test";
|
|
341
334
|
}
|
|
335
|
+
await this.reconnect(newDc);
|
|
336
|
+
d("migrated to DC%s", newDc);
|
|
342
337
|
}
|
|
343
338
|
disconnect() {
|
|
344
339
|
this.connectionInited = false;
|
|
@@ -411,7 +406,7 @@ export class Client extends ClientAbstract {
|
|
|
411
406
|
return;
|
|
412
407
|
}
|
|
413
408
|
catch (err) {
|
|
414
|
-
if (!(err instanceof
|
|
409
|
+
if (!(err instanceof AuthKeyUnregistered)) {
|
|
415
410
|
throw err;
|
|
416
411
|
}
|
|
417
412
|
}
|
|
@@ -423,16 +418,10 @@ export class Client extends ClientAbstract {
|
|
|
423
418
|
break;
|
|
424
419
|
}
|
|
425
420
|
catch (err) {
|
|
426
|
-
if (err instanceof
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
await this.initConnection();
|
|
431
|
-
continue;
|
|
432
|
-
}
|
|
433
|
-
else {
|
|
434
|
-
throw err;
|
|
435
|
-
}
|
|
421
|
+
if (err instanceof Migrate) {
|
|
422
|
+
await this[handleMigrationError](err);
|
|
423
|
+
await this.initConnection();
|
|
424
|
+
continue;
|
|
436
425
|
}
|
|
437
426
|
else {
|
|
438
427
|
throw err;
|
|
@@ -464,16 +453,10 @@ export class Client extends ClientAbstract {
|
|
|
464
453
|
sentCode = await sendCode();
|
|
465
454
|
}
|
|
466
455
|
catch (err) {
|
|
467
|
-
if (err instanceof
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
await this.initConnection();
|
|
472
|
-
sentCode = await sendCode();
|
|
473
|
-
}
|
|
474
|
-
else {
|
|
475
|
-
throw err;
|
|
476
|
-
}
|
|
456
|
+
if (err instanceof Migrate) {
|
|
457
|
+
await this[handleMigrationError](err);
|
|
458
|
+
await this.initConnection();
|
|
459
|
+
sentCode = await sendCode();
|
|
477
460
|
}
|
|
478
461
|
else {
|
|
479
462
|
throw err;
|
|
@@ -482,7 +465,7 @@ export class Client extends ClientAbstract {
|
|
|
482
465
|
break;
|
|
483
466
|
}
|
|
484
467
|
catch (err) {
|
|
485
|
-
if (err instanceof
|
|
468
|
+
if (err instanceof PhoneNumberInvalid) {
|
|
486
469
|
continue;
|
|
487
470
|
}
|
|
488
471
|
else {
|
|
@@ -516,7 +499,7 @@ export class Client extends ClientAbstract {
|
|
|
516
499
|
}
|
|
517
500
|
}
|
|
518
501
|
}
|
|
519
|
-
if (!(err instanceof
|
|
502
|
+
if (!(err instanceof SessionPasswordNeeded)) {
|
|
520
503
|
throw err;
|
|
521
504
|
}
|
|
522
505
|
while (true) {
|
|
@@ -535,7 +518,7 @@ export class Client extends ClientAbstract {
|
|
|
535
518
|
break;
|
|
536
519
|
}
|
|
537
520
|
catch (err) {
|
|
538
|
-
if (err instanceof
|
|
521
|
+
if (err instanceof PasswordHashInvalid) {
|
|
539
522
|
continue;
|
|
540
523
|
}
|
|
541
524
|
else {
|
|
@@ -557,7 +540,7 @@ export class Client extends ClientAbstract {
|
|
|
557
540
|
return;
|
|
558
541
|
}
|
|
559
542
|
catch (err) {
|
|
560
|
-
if (!(err instanceof
|
|
543
|
+
if (!(err instanceof AuthKeyUnregistered)) {
|
|
561
544
|
throw err;
|
|
562
545
|
}
|
|
563
546
|
}
|
|
@@ -609,7 +592,7 @@ export class Client extends ClientAbstract {
|
|
|
609
592
|
const promise = this.promises.get(messageId);
|
|
610
593
|
if (promise) {
|
|
611
594
|
if (result instanceof types.RPCError) {
|
|
612
|
-
promise.reject(result);
|
|
595
|
+
promise.reject(upgradeInstance(result));
|
|
613
596
|
}
|
|
614
597
|
else {
|
|
615
598
|
promise.resolve(result);
|
|
@@ -703,7 +686,7 @@ export class Client extends ClientAbstract {
|
|
|
703
686
|
});
|
|
704
687
|
}
|
|
705
688
|
catch (err) {
|
|
706
|
-
if (err instanceof
|
|
689
|
+
if (err instanceof AuthKeyUnregistered) {
|
|
707
690
|
await this.propagateAuthorizationState(false);
|
|
708
691
|
}
|
|
709
692
|
throw err;
|
|
@@ -16,18 +16,14 @@ export declare function ige256Encrypt(data: Uint8Array, key: Uint8Array, iv: Uin
|
|
|
16
16
|
*/
|
|
17
17
|
export declare function ige256Decrypt(data: Uint8Array, key: Uint8Array, iv: Uint8Array): Uint8Array;
|
|
18
18
|
/**
|
|
19
|
-
* Performs CTR-256 encryption.
|
|
19
|
+
* Performs CTR-256 encryption/decryption.
|
|
20
20
|
*
|
|
21
21
|
* @param data The data, larger than a byte
|
|
22
22
|
* @param key 32-byte encryption key
|
|
23
23
|
* @param iv 16-byte initialization vector
|
|
24
24
|
* @param state 1-byte state
|
|
25
25
|
*/
|
|
26
|
-
export declare function
|
|
27
|
-
/**
|
|
28
|
-
* Alias of `ctr256Encrypt`
|
|
29
|
-
*/
|
|
30
|
-
export declare function ctr256Decrypt(data: Uint8Array, key: Uint8Array, iv: Uint8Array, state: Uint8Array): [Uint8Array, Uint8Array, Uint8Array];
|
|
26
|
+
export declare function ctr256(data: Uint8Array, key: Uint8Array, iv: Uint8Array, state: Uint8Array): void;
|
|
31
27
|
/**
|
|
32
28
|
* Performs CBC-256 encryption.
|
|
33
29
|
*
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { default as init_ } from "./tgcrypto.js";
|
|
2
|
+
// deno-lint-ignore no-explicit-any
|
|
3
|
+
let module_;
|
|
4
|
+
const promise = init_().then((v) => module_ = v);
|
|
5
|
+
export async function init() {
|
|
6
|
+
await promise;
|
|
7
|
+
}
|
|
8
|
+
function checkIgeParams(data, key, iv) {
|
|
9
|
+
if (data.length == 0) {
|
|
10
|
+
throw new TypeError("data must not be empty");
|
|
11
|
+
}
|
|
12
|
+
else if (data.length % 16 != 0) {
|
|
13
|
+
throw new TypeError("data must consist of a number of bytes that is divisible by 16");
|
|
14
|
+
}
|
|
15
|
+
else if (key.length != 32) {
|
|
16
|
+
throw new TypeError("key must be 32 bytes");
|
|
17
|
+
}
|
|
18
|
+
else if (iv.length != 32) {
|
|
19
|
+
throw new TypeError("iv must be 32 bytes");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Performs IGE-256 encryption.
|
|
24
|
+
*
|
|
25
|
+
* @param data The unencrypted data, larger than a byte, divisible by 16
|
|
26
|
+
* @param key 32-byte encryption key
|
|
27
|
+
* @param iv 32-byte initialization vector
|
|
28
|
+
*/
|
|
29
|
+
export function ige256Encrypt(data, key, iv) {
|
|
30
|
+
checkIgeParams(data, key, iv);
|
|
31
|
+
const out = module_._malloc(1024);
|
|
32
|
+
module_.ccall("ige256_encrypt", "void", ["array", "pointer", "number", "array", "array"], [data, out, data.length, key, iv]);
|
|
33
|
+
try {
|
|
34
|
+
return module_.HEAPU8.slice(out, out + data.length);
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
module_._free(out);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Performs IGE-256 decryption.
|
|
42
|
+
*
|
|
43
|
+
* @param data The encrypted data, larger than a byte, divisible by 16
|
|
44
|
+
* @param key 32-byte encryption key
|
|
45
|
+
* @param iv 32-byte initialization vector
|
|
46
|
+
*/
|
|
47
|
+
export function ige256Decrypt(data, key, iv) {
|
|
48
|
+
checkIgeParams(data, key, iv);
|
|
49
|
+
const out = module_._malloc(1024);
|
|
50
|
+
module_.ccall("ige256_decrypt", "void", ["array", "pointer", "number", "array", "array"], [data, out, data.length, key, iv]);
|
|
51
|
+
try {
|
|
52
|
+
return module_.HEAPU8.slice(out, out + data.length);
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
module_._free(out);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function checkCtrParams(data, key, iv, state) {
|
|
59
|
+
if (data.length == 0) {
|
|
60
|
+
throw new TypeError("data must not be empty");
|
|
61
|
+
}
|
|
62
|
+
else if (key.length != 32) {
|
|
63
|
+
throw new TypeError("key must be 32 bytes");
|
|
64
|
+
}
|
|
65
|
+
else if (iv.length != 16) {
|
|
66
|
+
throw new TypeError("iv must be 16 bytes");
|
|
67
|
+
}
|
|
68
|
+
else if (state.length != 1) {
|
|
69
|
+
throw new TypeError("state must be 1 byte");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Performs CTR-256 encryption/decryption.
|
|
74
|
+
*
|
|
75
|
+
* @param data The data, larger than a byte
|
|
76
|
+
* @param key 32-byte encryption key
|
|
77
|
+
* @param iv 16-byte initialization vector
|
|
78
|
+
* @param state 1-byte state
|
|
79
|
+
*/
|
|
80
|
+
export function ctr256(data, key, iv, state) {
|
|
81
|
+
checkCtrParams(data, key, iv, state);
|
|
82
|
+
const datap = module_._malloc(data.length);
|
|
83
|
+
module_.HEAPU8.set(data, datap);
|
|
84
|
+
const ivp = module_._malloc(iv.length);
|
|
85
|
+
module_.HEAPU8.set(iv, ivp);
|
|
86
|
+
const statep = module_._malloc(state.length);
|
|
87
|
+
module_.HEAPU8.set(state, statep);
|
|
88
|
+
module_.ccall("ctr256", "void", ["pointer", "number", "array", "pointer", "pointer"], [datap, data.length, key, ivp, statep]);
|
|
89
|
+
data.set(module_.HEAPU8.slice(datap, datap + data.length));
|
|
90
|
+
iv.set(module_.HEAPU8.slice(ivp, ivp + iv.length));
|
|
91
|
+
state.set(module_.HEAPU8.slice(statep, statep + state.length));
|
|
92
|
+
module_._free(datap);
|
|
93
|
+
module_._free(ivp);
|
|
94
|
+
module_._free(statep);
|
|
95
|
+
}
|
|
96
|
+
function checkCbcParams(data, key, iv) {
|
|
97
|
+
if (data.length == 0) {
|
|
98
|
+
throw new TypeError("data must not be empty");
|
|
99
|
+
}
|
|
100
|
+
else if (data.length % 16 != 0) {
|
|
101
|
+
throw new TypeError("data must consist of a number of bytes that is divisible by 16");
|
|
102
|
+
}
|
|
103
|
+
else if (key.length != 32) {
|
|
104
|
+
throw new TypeError("key must be 32 bytes");
|
|
105
|
+
}
|
|
106
|
+
else if (iv.length != 16) {
|
|
107
|
+
throw new TypeError("iv must be 16 bytes");
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Performs CBC-256 encryption.
|
|
112
|
+
*
|
|
113
|
+
* @param data The unencrypted data, larger than a byte, divisible by 16
|
|
114
|
+
* @param key 32-byte encryption key
|
|
115
|
+
* @param iv 16-byte initialization vector
|
|
116
|
+
*/
|
|
117
|
+
export function cbc256Encrypt(data, key, iv) {
|
|
118
|
+
checkCbcParams(data, key, iv);
|
|
119
|
+
const datap = module_._malloc(data.length);
|
|
120
|
+
module_.HEAPU8.set(data, datap);
|
|
121
|
+
module_.ccall("cbc256_encrypt", "void", ["pointer", "number", "array", "array"], [datap, data.length, key, iv]);
|
|
122
|
+
try {
|
|
123
|
+
return module_.HEAPU8.slice(datap, datap + data.length);
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
module_._free(datap);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Performs CBC-256 decryption.
|
|
131
|
+
*
|
|
132
|
+
* @param data The encrypted data, larger than a byte, divisible by 16
|
|
133
|
+
* @param key 32-byte encryption key
|
|
134
|
+
* @param iv 16-byte initialization vector
|
|
135
|
+
*/
|
|
136
|
+
export function cbc256Decrypt(data, key, iv) {
|
|
137
|
+
checkCbcParams(data, key, iv);
|
|
138
|
+
const datap = module_._malloc(data.length);
|
|
139
|
+
module_.HEAPU8.set(data, datap);
|
|
140
|
+
module_.ccall("cbc256_decrypt", "void", ["pointer", "number", "array", "array"], [datap, data.length, key, iv]);
|
|
141
|
+
try {
|
|
142
|
+
return module_.HEAPU8.slice(datap, datap + data.length);
|
|
143
|
+
}
|
|
144
|
+
finally {
|
|
145
|
+
module_._free(datap);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
export function factorize(pq) {
|
|
149
|
+
const pqp = module_._malloc(16);
|
|
150
|
+
module_.ccall("factorize", "void", ["number", "pointer"], [pq, pqp]);
|
|
151
|
+
try {
|
|
152
|
+
const pqp_ = module_.HEAP64.slice(pqp / 8, pqp / 8 + 2);
|
|
153
|
+
return [pqp_[0], pqp_[1]];
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
module_._free(pqp);
|
|
157
|
+
}
|
|
158
|
+
}
|