@mtcute/test 0.17.0 → 0.18.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.
- package/client.js +223 -0
- package/crypto.d.ts +0 -1
- package/crypto.js +191 -0
- package/index.d.ts +0 -1
- package/index.js +9 -843
- package/package.json +19 -17
- package/platform.web.js +7 -0
- package/schema.js +29 -0
- package/storage/auth-keys.js +91 -0
- package/storage/key-value.js +37 -0
- package/storage/peers.js +83 -0
- package/storage/ref-messages.js +67 -0
- package/storage.d.ts +1 -1
- package/storage.js +45 -0
- package/stub.js +60 -0
- package/transport.d.ts +12 -22
- package/transport.js +28 -0
- package/utils.js +15 -0
- package/index.d.cts +0 -8
- package/platform.test.d.ts +0 -1
- package/transport.test.d.ts +0 -1
package/client.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { tl } from "@mtcute/core";
|
|
2
|
+
import { BaseTelegramClient } from "@mtcute/core/client.js";
|
|
3
|
+
import { defaultPlatform, defaultCryptoProvider } from "./platform.web.js";
|
|
4
|
+
import { StubMemoryTelegramStorage } from "./storage.js";
|
|
5
|
+
import { StubTelegramTransport } from "./transport.js";
|
|
6
|
+
import { markedIdToPeer } from "./utils.js";
|
|
7
|
+
class StubTelegramClient extends BaseTelegramClient {
|
|
8
|
+
constructor(params) {
|
|
9
|
+
const storage = new StubMemoryTelegramStorage({
|
|
10
|
+
hasKeys: true,
|
|
11
|
+
hasTempKeys: true
|
|
12
|
+
});
|
|
13
|
+
super({
|
|
14
|
+
apiId: 0,
|
|
15
|
+
apiHash: "",
|
|
16
|
+
logLevel: 9,
|
|
17
|
+
storage,
|
|
18
|
+
disableUpdates: true,
|
|
19
|
+
transport: new StubTelegramTransport({
|
|
20
|
+
onMessage: (data, dcId) => {
|
|
21
|
+
if (!this._onRawMessage) {
|
|
22
|
+
if (this._responders.size) {
|
|
23
|
+
this.onError.emit(new Error("Unexpected outgoing message"));
|
|
24
|
+
}
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const key = storage.authKeys.get(dcId);
|
|
28
|
+
if (key) {
|
|
29
|
+
return this._onRawMessage(storage.decryptOutgoingMessage(this.crypto, data, dcId));
|
|
30
|
+
}
|
|
31
|
+
this._onRawMessage?.(data);
|
|
32
|
+
}
|
|
33
|
+
}),
|
|
34
|
+
crypto: defaultCryptoProvider,
|
|
35
|
+
platform: defaultPlatform,
|
|
36
|
+
...params
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Create a fake client that may not actually be used for API calls
|
|
41
|
+
*
|
|
42
|
+
* Useful for testing "offline" methods
|
|
43
|
+
*/
|
|
44
|
+
static offline() {
|
|
45
|
+
const client = new StubTelegramClient();
|
|
46
|
+
client.call = (obj) => {
|
|
47
|
+
throw new Error(`Expected offline client to not make any API calls (method called: ${obj._})`);
|
|
48
|
+
};
|
|
49
|
+
return client;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Create a fake "full" client (i.e. TelegramClient)
|
|
53
|
+
*
|
|
54
|
+
* Basically a proxy that returns an empty function for every unknown method
|
|
55
|
+
*/
|
|
56
|
+
static full() {
|
|
57
|
+
const client = new StubTelegramClient();
|
|
58
|
+
return new Proxy(client, {
|
|
59
|
+
get(target, prop) {
|
|
60
|
+
if (typeof prop === "string" && !(prop in target)) {
|
|
61
|
+
return () => {
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
return target[prop];
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
// some fake peers handling //
|
|
69
|
+
_knownChats = /* @__PURE__ */ new Map();
|
|
70
|
+
_knownUsers = /* @__PURE__ */ new Map();
|
|
71
|
+
_selfId = 0;
|
|
72
|
+
async registerPeers(...peers) {
|
|
73
|
+
for (const peer of peers) {
|
|
74
|
+
if (tl.isAnyUser(peer)) {
|
|
75
|
+
this._knownUsers.set(peer.id, peer);
|
|
76
|
+
} else {
|
|
77
|
+
this._knownChats.set(peer.id, peer);
|
|
78
|
+
}
|
|
79
|
+
await this.storage.peers.updatePeersFrom(peer);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
getPeers(ids) {
|
|
83
|
+
const users = [];
|
|
84
|
+
const chats = [];
|
|
85
|
+
for (let id of ids) {
|
|
86
|
+
if (!id) continue;
|
|
87
|
+
if (typeof id === "number") {
|
|
88
|
+
id = markedIdToPeer(id);
|
|
89
|
+
}
|
|
90
|
+
switch (id._) {
|
|
91
|
+
case "peerUser": {
|
|
92
|
+
const user = this._knownUsers.get(id.userId);
|
|
93
|
+
if (!user) throw new Error(`Unknown user with ID ${id.userId}`);
|
|
94
|
+
users.push(user);
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
case "peerChat": {
|
|
98
|
+
const chat = this._knownChats.get(id.chatId);
|
|
99
|
+
if (!chat) throw new Error(`Unknown chat with ID ${id.chatId}`);
|
|
100
|
+
chats.push(chat);
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
case "peerChannel": {
|
|
104
|
+
const chat = this._knownChats.get(id.channelId);
|
|
105
|
+
if (!chat) throw new Error(`Unknown channel with ID ${id.channelId}`);
|
|
106
|
+
chats.push(chat);
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return { users, chats };
|
|
112
|
+
}
|
|
113
|
+
// method calls intercepting //
|
|
114
|
+
_onRawMessage;
|
|
115
|
+
onRawMessage(fn) {
|
|
116
|
+
this._onRawMessage = fn;
|
|
117
|
+
}
|
|
118
|
+
_responders = /* @__PURE__ */ new Map();
|
|
119
|
+
addResponder(responders) {
|
|
120
|
+
if (Array.isArray(responders)) {
|
|
121
|
+
for (const responder2 of responders) {
|
|
122
|
+
this.addResponder(responder2);
|
|
123
|
+
}
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (typeof responders === "function") {
|
|
127
|
+
responders = responders(this);
|
|
128
|
+
}
|
|
129
|
+
const [method, responder] = responders;
|
|
130
|
+
this.respondWith(method, responder);
|
|
131
|
+
}
|
|
132
|
+
respondWith(method, response) {
|
|
133
|
+
this._responders.set(method, response);
|
|
134
|
+
return response;
|
|
135
|
+
}
|
|
136
|
+
async call(message, params) {
|
|
137
|
+
if (this._responders.has(message._)) {
|
|
138
|
+
return Promise.resolve(this._responders.get(message._)(message));
|
|
139
|
+
}
|
|
140
|
+
return super.call(message, params);
|
|
141
|
+
}
|
|
142
|
+
// some fake updates mechanism //
|
|
143
|
+
_fakeMessageBoxes = /* @__PURE__ */ new Map();
|
|
144
|
+
_lastQts = 0;
|
|
145
|
+
_lastDate = Math.floor(Date.now() / 1e3);
|
|
146
|
+
_lastSeq = 0;
|
|
147
|
+
getMessageBox(chatId = 0) {
|
|
148
|
+
const state = this._fakeMessageBoxes.get(chatId);
|
|
149
|
+
if (!state) {
|
|
150
|
+
const newState = { pts: 0, lastMessageId: 0 };
|
|
151
|
+
this._fakeMessageBoxes.set(chatId, newState);
|
|
152
|
+
return newState;
|
|
153
|
+
}
|
|
154
|
+
return state;
|
|
155
|
+
}
|
|
156
|
+
getNextMessageId(chatId = 0) {
|
|
157
|
+
const state = this.getMessageBox(chatId);
|
|
158
|
+
const nextId = state.lastMessageId + 1;
|
|
159
|
+
state.lastMessageId = nextId;
|
|
160
|
+
return nextId;
|
|
161
|
+
}
|
|
162
|
+
getNextPts(chatId = 0, count = 1) {
|
|
163
|
+
const state = this.getMessageBox(chatId);
|
|
164
|
+
const nextPts = state.pts + count;
|
|
165
|
+
state.pts = nextPts;
|
|
166
|
+
return nextPts;
|
|
167
|
+
}
|
|
168
|
+
getNextQts() {
|
|
169
|
+
return this._lastQts++;
|
|
170
|
+
}
|
|
171
|
+
getNextDate() {
|
|
172
|
+
return this._lastDate = Math.floor(Date.now() / 1e3);
|
|
173
|
+
}
|
|
174
|
+
createStubUpdates(params) {
|
|
175
|
+
const { peers, updates, seq = 0, seqCount = 1 } = params;
|
|
176
|
+
const { users, chats } = this.getPeers(peers ?? []);
|
|
177
|
+
const seqStart = seq - seqCount + 1;
|
|
178
|
+
if (seq !== 0) {
|
|
179
|
+
this._lastSeq = seq + seqCount;
|
|
180
|
+
}
|
|
181
|
+
if (seqStart !== seq) {
|
|
182
|
+
return {
|
|
183
|
+
_: "updatesCombined",
|
|
184
|
+
updates,
|
|
185
|
+
users,
|
|
186
|
+
chats,
|
|
187
|
+
date: this.getNextDate(),
|
|
188
|
+
seq,
|
|
189
|
+
seqStart
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
_: "updates",
|
|
194
|
+
updates,
|
|
195
|
+
users,
|
|
196
|
+
chats,
|
|
197
|
+
date: this.getNextDate(),
|
|
198
|
+
seq
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
// helpers //
|
|
202
|
+
async with(fn) {
|
|
203
|
+
await this.connect();
|
|
204
|
+
let error;
|
|
205
|
+
const handler = (err) => {
|
|
206
|
+
error = err;
|
|
207
|
+
};
|
|
208
|
+
this.onError.add(handler);
|
|
209
|
+
try {
|
|
210
|
+
await fn();
|
|
211
|
+
} catch (e) {
|
|
212
|
+
error = e;
|
|
213
|
+
}
|
|
214
|
+
await this.close();
|
|
215
|
+
this.onError.remove(handler);
|
|
216
|
+
if (error) {
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
export {
|
|
222
|
+
StubTelegramClient
|
|
223
|
+
};
|
package/crypto.d.ts
CHANGED
|
@@ -3,4 +3,3 @@ export declare function withFakeRandom(provider: ICryptoProvider, source?: strin
|
|
|
3
3
|
export declare function useFakeMathRandom(source?: string): void;
|
|
4
4
|
export declare function defaultTestCryptoProvider(source?: string): Promise<ICryptoProvider>;
|
|
5
5
|
export declare function testCryptoProvider(c: ICryptoProvider): void;
|
|
6
|
-
export declare function u8HexDecode(hex: string): Uint8Array;
|
package/crypto.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { inflateSync, gzipSync } from "node:zlib";
|
|
2
|
+
import { hex, typed, utf8 } from "@fuman/utils";
|
|
3
|
+
import { beforeEach, vi, afterEach, beforeAll, it, expect, describe } from "vitest";
|
|
4
|
+
import { defaultCryptoProvider } from "./platform.web.js";
|
|
5
|
+
const DEFAULT_ENTROPY = `
|
|
6
|
+
29afd26df40fb8ed10b6b4ad6d56ef5df9453f88e6ee6adb6e0544ba635dc6a8a990c9b8b980c343936b33fa7f97bae025102532233abb269c489920ef99021b
|
|
7
|
+
259ce3a2c964c5c8972b4a84ff96f3375a94b535a9468f2896e2080ac7a32ed58e910474a4b02415e07671cbb5bdd58a5dd26fd137c4c98b8c346571fae6ead3
|
|
8
|
+
9dfd612bd6b480b6723433f5218e9d6e271591153fb3ffefc089f7e848d3f4633459fff66b33cf939e5655813149fa34be8625f9bc4814d1ee6cf40e4d0de229
|
|
9
|
+
1aa22e68c8ad8cc698103734f9aaf79f2bdc052a787a7a9b3629d1ed38750f88cb0481c0ba30a9c611672f9a4d1dc02637abb4e98913ee810a3b152d3d75f25d
|
|
10
|
+
7efdc263c08833569968b1771ebbe843d187e2c917d9ad8e8865e44b69f7b74d72ab86a4ef1891dce196ee11a7c9d7d8074fc0450e745bd3a827d77bb0820b90
|
|
11
|
+
3055dc15f0abd897ea740a99606b64d28968d770b5e43492ddbf07a7c75104d3e522be9b72050c0fdae8412cdf49014be21105b87a06cb7202dd580387adc007
|
|
12
|
+
6280d98b015a1a413819d817f007939d1490467a1ef85a345584c7e594bb729c12a1233f806e515e7088360219dfa109264310ba84777b93eb1ad3c40727a25a
|
|
13
|
+
a5d9cdd6748c6ab2ca0bd4daa2ba8225bce2b066a163bcacf05609fc84055bb86a4742c28addd7d7ab8d87b64cfde0b3f4b3bc8e05f3d0a1a2fadb294860e099
|
|
14
|
+
a10b3721b0d5b28918b8fb49a18a82a0fde6680a64ed915637805e35ffe8b2c1d4177ec10d10eaaf24425e0351b6a89e794944e1aa82eb5c0210a37da66cccac
|
|
15
|
+
895398cf915a8aa141f611521fc258514a99c02721113942c66f2c9a8f9601ff0044a953d17a47b07ad1b5f8725cc020a1a5239be65db0a43d42c206903740f0
|
|
16
|
+
27c3f749ecfff2e646570118cd54db2fec392b44d8eb8377309f3e4d164dbc0530914b117b9d278b06db8359d97442d4dcbcaff93cd9a08a6b06a5ba8725d0d7
|
|
17
|
+
06b313a5d792be254d33e087b7a4fafcdf819941b9bec4c6057d4c050bd01eb243efd4e6b707281b127820a2b734c6d8f6b2131bf0b5b215c7a798ff3fe90ceb
|
|
18
|
+
da91539fcc7b03d2b8b1381bd6023fff20278344ad944d364ba684842db3901c346335f0d455eda414f99c1e794a86aa3a90bcc6e085eecb0b4bf61198d16ed3
|
|
19
|
+
89cfa495f977a37a51502b2f60649f2efd7d89c757b6366776ba4c0612017bf1fbfc682dd62e9960d39cbea854d2dcc708b1db5d268192954d13ee72c0bb1bd8
|
|
20
|
+
558a3cf3b02b1cd795b40f7a57780391bb8724883d3f7764846c3823e165b3f8c025f59d896905f9a955478586ce57f820d958a01aa59a4cace7ecdf125df334
|
|
21
|
+
fa3de8e50aac96c1275591a1221c32a60a1513370a33a228e00894341b10cf44a6ae6ac250d17a364e956ab1a17b068df3fb2d5b5a672d8a409eeb8b6ca1ade6
|
|
22
|
+
`.replace(/\s/g, "");
|
|
23
|
+
function withFakeRandom(provider, source = DEFAULT_ENTROPY) {
|
|
24
|
+
const sourceBytes = hex.decode(source);
|
|
25
|
+
let offset = 0;
|
|
26
|
+
function getRandomValues(buf) {
|
|
27
|
+
if (offset + buf.length > sourceBytes.length) {
|
|
28
|
+
throw new Error("not enough entropy");
|
|
29
|
+
}
|
|
30
|
+
buf.set(sourceBytes.subarray(offset, offset + buf.length));
|
|
31
|
+
offset += buf.length;
|
|
32
|
+
}
|
|
33
|
+
return new Proxy(provider, {
|
|
34
|
+
get(target, prop, receiver) {
|
|
35
|
+
if (prop === "randomFill") return getRandomValues;
|
|
36
|
+
return Reflect.get(target, prop, receiver);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
function useFakeMathRandom(source = DEFAULT_ENTROPY) {
|
|
41
|
+
const sourceBytes = hex.decode(source);
|
|
42
|
+
const dv = typed.toDataView(sourceBytes);
|
|
43
|
+
let spy;
|
|
44
|
+
beforeEach(() => {
|
|
45
|
+
let offset = 0;
|
|
46
|
+
spy = vi.spyOn(globalThis.Math, "random").mockImplementation(() => {
|
|
47
|
+
const ret = dv.getUint32(offset, true) / 4294967295;
|
|
48
|
+
offset += 4;
|
|
49
|
+
return ret;
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
afterEach(() => {
|
|
53
|
+
spy.mockRestore();
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
async function defaultTestCryptoProvider(source = DEFAULT_ENTROPY) {
|
|
57
|
+
const prov = withFakeRandom(defaultCryptoProvider, source);
|
|
58
|
+
await prov.initialize?.();
|
|
59
|
+
return prov;
|
|
60
|
+
}
|
|
61
|
+
function testCryptoProvider(c) {
|
|
62
|
+
beforeAll(() => c.initialize?.());
|
|
63
|
+
function gzipSyncWrap(data) {
|
|
64
|
+
return gzipSync(data);
|
|
65
|
+
}
|
|
66
|
+
function inflateSyncWrap(data) {
|
|
67
|
+
return inflateSync(data);
|
|
68
|
+
}
|
|
69
|
+
it("should calculate sha1", () => {
|
|
70
|
+
expect(hex.encode(c.sha1(utf8.encoder.encode("")))).to.eq("da39a3ee5e6b4b0d3255bfef95601890afd80709");
|
|
71
|
+
expect(hex.encode(c.sha1(utf8.encoder.encode("hello")))).to.eq("aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d");
|
|
72
|
+
expect(hex.encode(c.sha1(hex.decode("aebb1f")))).to.eq("62849d15c5dea495916c5eea8dba5f9551288850");
|
|
73
|
+
});
|
|
74
|
+
it("should calculate sha256", () => {
|
|
75
|
+
expect(hex.encode(c.sha256(utf8.encoder.encode("")))).to.eq(
|
|
76
|
+
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
|
77
|
+
);
|
|
78
|
+
expect(hex.encode(c.sha256(utf8.encoder.encode("hello")))).to.eq(
|
|
79
|
+
"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
|
|
80
|
+
);
|
|
81
|
+
expect(hex.encode(c.sha256(hex.decode("aebb1f")))).to.eq(
|
|
82
|
+
"2d29658aba48f2b286fe8bbddb931b7ad297e5adb5b9a6fc3aab67ef7fbf4e80"
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
it("should calculate hmac-sha256", async () => {
|
|
86
|
+
const key = hex.decode("aaeeff");
|
|
87
|
+
expect(hex.encode(await c.hmacSha256(utf8.encoder.encode(""), key))).to.eq(
|
|
88
|
+
"642711307c9e4437df09d6ebaa6bdc1b3a810c7f15c50fd1d0f8d7d5490f44dd"
|
|
89
|
+
);
|
|
90
|
+
expect(hex.encode(await c.hmacSha256(utf8.encoder.encode("hello"), key))).to.eq(
|
|
91
|
+
"39b00bab151f9868e6501655c580b5542954711181243474d46b894703b1c1c2"
|
|
92
|
+
);
|
|
93
|
+
expect(hex.encode(await c.hmacSha256(hex.decode("aebb1f"), key))).to.eq(
|
|
94
|
+
"a3a7273871808711cab17aba14f58e96f63f3ccfc5097d206f0f00ead2c3dd35"
|
|
95
|
+
);
|
|
96
|
+
});
|
|
97
|
+
it("should derive pbkdf2 key", async () => {
|
|
98
|
+
expect(hex.encode(await c.pbkdf2(utf8.encoder.encode("pbkdf2 test"), utf8.encoder.encode("some salt"), 10))).to.eq(
|
|
99
|
+
"e43276cfa27f135f261cec8ddcf593fd74ec251038e459c165461f2308f3a7235e0744ee1aed9710b00db28d1a2112e20fea3601c60e770ac57ffe6b33ca8be1"
|
|
100
|
+
);
|
|
101
|
+
});
|
|
102
|
+
it("should encrypt and decrypt aes-ctr", () => {
|
|
103
|
+
let aes = c.createAesCtr(
|
|
104
|
+
hex.decode("d450aae0bf0060a4af1044886b42a13f7c506b35255d134a7e87ab3f23a9493b"),
|
|
105
|
+
hex.decode("0182de2bd789c295c3c6c875c5e9e190"),
|
|
106
|
+
true
|
|
107
|
+
);
|
|
108
|
+
const data = hex.decode("7baae571e4c2f4cfadb1931d5923aca7");
|
|
109
|
+
expect(hex.encode(aes.process(data))).eq("df5647dbb70bc393f2fb05b72f42286f");
|
|
110
|
+
expect(hex.encode(aes.process(data))).eq("3917147082672516b3177150129bc579");
|
|
111
|
+
expect(hex.encode(aes.process(data))).eq("2a7a9089270a5de45d5e3dd399cac725");
|
|
112
|
+
expect(hex.encode(aes.process(data))).eq("56d085217771398ac13583de4d677dd8");
|
|
113
|
+
expect(hex.encode(aes.process(data))).eq("cc639b488126cf36e79c4515e8012b92");
|
|
114
|
+
expect(hex.encode(aes.process(data))).eq("01384d100646cd562cc5586ec3f8f8c4");
|
|
115
|
+
aes.close?.();
|
|
116
|
+
aes = c.createAesCtr(
|
|
117
|
+
hex.decode("d450aae0bf0060a4af1044886b42a13f7c506b35255d134a7e87ab3f23a9493b"),
|
|
118
|
+
hex.decode("0182de2bd789c295c3c6c875c5e9e190"),
|
|
119
|
+
false
|
|
120
|
+
);
|
|
121
|
+
expect(hex.encode(aes.process(hex.decode("df5647dbb70bc393f2fb05b72f42286f")))).eq(hex.encode(data));
|
|
122
|
+
expect(hex.encode(aes.process(hex.decode("3917147082672516b3177150129bc579")))).eq(hex.encode(data));
|
|
123
|
+
expect(hex.encode(aes.process(hex.decode("2a7a9089270a5de45d5e3dd399cac725")))).eq(hex.encode(data));
|
|
124
|
+
expect(hex.encode(aes.process(hex.decode("56d085217771398ac13583de4d677dd8")))).eq(hex.encode(data));
|
|
125
|
+
expect(hex.encode(aes.process(hex.decode("cc639b488126cf36e79c4515e8012b92")))).eq(hex.encode(data));
|
|
126
|
+
expect(hex.encode(aes.process(hex.decode("01384d100646cd562cc5586ec3f8f8c4")))).eq(hex.encode(data));
|
|
127
|
+
aes.close?.();
|
|
128
|
+
});
|
|
129
|
+
it("should encrypt and decrypt aes-ige", () => {
|
|
130
|
+
const aes = c.createAesIge(
|
|
131
|
+
hex.decode("5468697320697320616E20696D706C655468697320697320616E20696D706C65"),
|
|
132
|
+
hex.decode("6D656E746174696F6E206F6620494745206D6F646520666F72204F70656E5353")
|
|
133
|
+
);
|
|
134
|
+
expect(
|
|
135
|
+
hex.encode(aes.encrypt(hex.decode("99706487a1cde613bc6de0b6f24b1c7aa448c8b9c3403e3467a8cad89340f53b")))
|
|
136
|
+
).to.eq("792ea8ae577b1a66cb3bd92679b8030ca54ee631976bd3a04547fdcb4639fa69");
|
|
137
|
+
expect(
|
|
138
|
+
hex.encode(aes.decrypt(hex.decode("792ea8ae577b1a66cb3bd92679b8030ca54ee631976bd3a04547fdcb4639fa69")))
|
|
139
|
+
).to.eq("99706487a1cde613bc6de0b6f24b1c7aa448c8b9c3403e3467a8cad89340f53b");
|
|
140
|
+
});
|
|
141
|
+
it(
|
|
142
|
+
"should decompose PQ to prime factors P and Q",
|
|
143
|
+
async () => {
|
|
144
|
+
const testFactorization = async (pq, p_, q) => {
|
|
145
|
+
const [p1, q1] = await c.factorizePQ(hex.decode(pq));
|
|
146
|
+
expect(hex.encode(p1)).eq(p_.toLowerCase());
|
|
147
|
+
expect(hex.encode(q1)).eq(q.toLowerCase());
|
|
148
|
+
};
|
|
149
|
+
await testFactorization("17ED48941A08F981", "494C553B", "53911073");
|
|
150
|
+
await testFactorization("14fcab4dfc861f45", "494c5c99", "494c778d");
|
|
151
|
+
},
|
|
152
|
+
// since PQ factorization relies on RNG, it may take a while (or may not!)
|
|
153
|
+
{ timeout: 1e4 }
|
|
154
|
+
);
|
|
155
|
+
it("should correctly gzip", () => {
|
|
156
|
+
const data = new Uint8Array(1e3).fill(66);
|
|
157
|
+
const compressed = c.gzip(data, 100);
|
|
158
|
+
expect(compressed).not.toBeNull();
|
|
159
|
+
const decompressed = inflateSyncWrap(compressed);
|
|
160
|
+
expect(compressed.length).toBeLessThan(data.length);
|
|
161
|
+
expect(hex.encode(decompressed)).toEqual(hex.encode(data));
|
|
162
|
+
});
|
|
163
|
+
it("should correctly gunzip", () => {
|
|
164
|
+
const data = new Uint8Array(1e3).fill(66);
|
|
165
|
+
const compressed = gzipSyncWrap(data);
|
|
166
|
+
const decompressed = c.gunzip(compressed);
|
|
167
|
+
expect(hex.encode(decompressed)).toEqual(hex.encode(data));
|
|
168
|
+
});
|
|
169
|
+
describe("randomBytes", () => {
|
|
170
|
+
it("should return exactly N bytes", () => {
|
|
171
|
+
expect(c.randomBytes(0).length).eq(0);
|
|
172
|
+
expect(c.randomBytes(5).length).eq(5);
|
|
173
|
+
expect(c.randomBytes(10).length).eq(10);
|
|
174
|
+
expect(c.randomBytes(256).length).eq(256);
|
|
175
|
+
});
|
|
176
|
+
it("should not be deterministic", () => {
|
|
177
|
+
expect([...c.randomBytes(8)]).not.eql([...c.randomBytes(8)]);
|
|
178
|
+
});
|
|
179
|
+
it("should use randomFill", () => {
|
|
180
|
+
const spy = vi.spyOn(c, "randomFill");
|
|
181
|
+
c.randomBytes(8);
|
|
182
|
+
expect(spy).toHaveBeenCalled();
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
export {
|
|
187
|
+
defaultTestCryptoProvider,
|
|
188
|
+
testCryptoProvider,
|
|
189
|
+
useFakeMathRandom,
|
|
190
|
+
withFakeRandom
|
|
191
|
+
};
|