@freesignal/protocol 0.1.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.
@@ -0,0 +1,172 @@
1
+ /**
2
+ * FreeSignal Protocol
3
+ *
4
+ * Copyright (C) 2025 Christian Braghette
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with this program. If not, see <https://www.gnu.org/licenses/>
18
+ */
19
+ import { Encodable } from "./data";
20
+ /**
21
+ * Creates a new Double Ratchet session.
22
+ *
23
+ * @param opts.remoteKey The public key of the remote party.
24
+ * @param opts.preSharedKey An optional pre-shared key to initialize the session.
25
+ *
26
+ * @returns A new Double Ratchet session.
27
+ */
28
+ export declare function createSession(opts?: {
29
+ remoteKey?: Uint8Array;
30
+ preSharedKey?: Uint8Array;
31
+ }): Session;
32
+ /**
33
+ * Represents a secure Double Ratchet session.
34
+ * Used for forward-secure encryption and decryption of messages.
35
+ */
36
+ export declare class Session {
37
+ private static readonly skipLimit;
38
+ static readonly version = 1;
39
+ static readonly rootKeyLength: number;
40
+ private keyPair;
41
+ private _remoteKey?;
42
+ private rootKey?;
43
+ private sendingChain?;
44
+ private sendingCount;
45
+ private previousCount;
46
+ private receivingChain?;
47
+ private receivingCount;
48
+ private previousKeys;
49
+ constructor(opts?: {
50
+ remoteKey?: Uint8Array;
51
+ preSharedKey?: Uint8Array;
52
+ });
53
+ /**
54
+ * Whether both the sending and receiving chains are initialized.
55
+ */
56
+ get handshaked(): boolean;
57
+ /**
58
+ * The public key of this session.
59
+ */
60
+ get publicKey(): Uint8Array;
61
+ /**
62
+ * The last known remote public key.
63
+ */
64
+ get remoteKey(): Uint8Array | undefined;
65
+ /**
66
+ * Set remote public key.
67
+ */
68
+ setRemoteKey(key: Uint8Array): this;
69
+ private dhRatchet;
70
+ private getSendingKey;
71
+ private getReceivingKey;
72
+ /**
73
+ * Encrypts a message payload using the current sending chain.
74
+ *
75
+ * @param message - The message as a Uint8Array.
76
+ * @returns An EncryptedPayload or undefined if encryption fails.
77
+ */
78
+ encrypt(message: Uint8Array): EncryptedPayload | undefined;
79
+ /**
80
+ * Decrypts an encrypted message.
81
+ *
82
+ * @param payload - The received encrypted message.
83
+ * @returns The decrypted message as a Uint8Array, or undefined if decryption fails.
84
+ */
85
+ decrypt(payload: Uint8Array | EncryptedPayload): Uint8Array | undefined;
86
+ /**
87
+ * Export the state of the session;
88
+ */
89
+ export(): string;
90
+ /**
91
+ * Import a state.
92
+ *
93
+ * @param json string returned by `export()` method.
94
+ * @returns session with the state parsed.
95
+ */
96
+ static import(json: string): Session;
97
+ /**
98
+ * The fixed key length (in bytes) used throughout the Double Ratchet session.
99
+ * Typically 32 bytes (256 bits) for symmetric keys.
100
+ */
101
+ static readonly keyLength = 32;
102
+ private static symmetricRatchet;
103
+ }
104
+ /**
105
+ * Interface representing an encrypted payload.
106
+ * Provides metadata and de/serialization methods.
107
+ */
108
+ export interface EncryptedPayload extends Encodable {
109
+ /**
110
+ * The length of the payload.
111
+ */
112
+ readonly length: number;
113
+ /**
114
+ * Version of the payload.
115
+ */
116
+ readonly version: number;
117
+ /**
118
+ * The current message count of the sending chain.
119
+ */
120
+ readonly count: number;
121
+ /**
122
+ * The count of the previous sending chain.
123
+ */
124
+ readonly previous: number;
125
+ /**
126
+ * The sender's public key used for this message.
127
+ */
128
+ readonly publicKey: Uint8Array;
129
+ /**
130
+ * The nonce used during encryption.
131
+ */
132
+ readonly nonce: Uint8Array;
133
+ /**
134
+ * The encrypted message content.
135
+ */
136
+ readonly ciphertext: Uint8Array;
137
+ /**
138
+ * The payload signature.
139
+ */
140
+ /**
141
+ * Set the signature of the payload.
142
+ *
143
+ * @param signature signature
144
+ */
145
+ /**
146
+ * Return the payload without the signature.
147
+ */
148
+ /**
149
+ * Serializes the payload into a Uint8Array for transport.
150
+ */
151
+ encode(): Uint8Array;
152
+ /**
153
+ * Decodes the payload into a readable object format.
154
+ */
155
+ /**
156
+ * Returns the payload as a UTF-8 string.
157
+ */
158
+ toString(): string;
159
+ /**
160
+ * Returns the decoded object as a JSON string.
161
+ */
162
+ toJSON(): string;
163
+ }
164
+ export declare class EncryptedPayload {
165
+ /**
166
+ * Static factory method that constructs an `EncryptedPayload` from a raw Uint8Array.
167
+ *
168
+ * @param array - A previously serialized encrypted payload.
169
+ * @returns An instance of `EncryptedPayload`.
170
+ */
171
+ static from(array: Uint8Array | EncryptedPayload): EncryptedPayload;
172
+ }
@@ -0,0 +1,338 @@
1
+ "use strict";
2
+ /**
3
+ * FreeSignal Protocol
4
+ *
5
+ * Copyright (C) 2025 Christian Braghette
6
+ *
7
+ * This program is free software: you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation, either version 3 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * This program is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU General Public License
18
+ * along with this program. If not, see <https://www.gnu.org/licenses/>
19
+ */
20
+ var __importDefault = (this && this.__importDefault) || function (mod) {
21
+ return (mod && mod.__esModule) ? mod : { "default": mod };
22
+ };
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.EncryptedPayload = exports.Session = void 0;
25
+ exports.createSession = createSession;
26
+ const crypto_1 = __importDefault(require("./crypto"));
27
+ const utils_1 = require("./utils");
28
+ /**
29
+ * Creates a new Double Ratchet session.
30
+ *
31
+ * @param opts.remoteKey The public key of the remote party.
32
+ * @param opts.preSharedKey An optional pre-shared key to initialize the session.
33
+ *
34
+ * @returns A new Double Ratchet session.
35
+ */
36
+ function createSession(opts) {
37
+ return new Session(opts);
38
+ }
39
+ /**
40
+ * Represents a secure Double Ratchet session.
41
+ * Used for forward-secure encryption and decryption of messages.
42
+ */
43
+ class Session {
44
+ constructor(opts = {}) {
45
+ this.keyPair = crypto_1.default.ECDH.keyPair();
46
+ this.sendingCount = 0;
47
+ this.previousCount = 0;
48
+ this.receivingCount = 0;
49
+ this.previousKeys = new KeyMap();
50
+ if (opts.preSharedKey)
51
+ this.rootKey = opts.preSharedKey;
52
+ if (opts.remoteKey) {
53
+ this._remoteKey = opts.remoteKey;
54
+ this.sendingChain = this.dhRatchet();
55
+ }
56
+ }
57
+ /**
58
+ * Whether both the sending and receiving chains are initialized.
59
+ */
60
+ get handshaked() { return this.sendingChain && this.receivingChain ? true : false; }
61
+ /**
62
+ * The public key of this session.
63
+ */
64
+ get publicKey() { return this.keyPair.publicKey; }
65
+ /**
66
+ * The last known remote public key.
67
+ */
68
+ get remoteKey() { return this._remoteKey; }
69
+ /**
70
+ * Set remote public key.
71
+ */
72
+ setRemoteKey(key) {
73
+ this._remoteKey = key;
74
+ this.receivingChain = this.dhRatchet();
75
+ if (this.receivingCount > (EncryptedPayloadConstructor.maxCount - Session.skipLimit * 2))
76
+ this.receivingCount = 0;
77
+ this.previousCount = this.sendingCount;
78
+ this.keyPair = crypto_1.default.ECDH.keyPair();
79
+ this.sendingChain = this.dhRatchet();
80
+ if (this.sendingCount > (EncryptedPayloadConstructor.maxCount - Session.skipLimit * 2))
81
+ this.sendingCount = 0;
82
+ return this;
83
+ }
84
+ dhRatchet(info) {
85
+ if (!this._remoteKey)
86
+ throw new Error();
87
+ const sharedKey = crypto_1.default.scalarMult(this.keyPair.secretKey, this._remoteKey);
88
+ if (!this.rootKey)
89
+ this.rootKey = crypto_1.default.hash(sharedKey);
90
+ const hashkey = crypto_1.default.hkdf(sharedKey, this.rootKey, info, Session.keyLength * 2);
91
+ this.rootKey = hashkey.slice(0, Session.keyLength);
92
+ return hashkey.slice(Session.keyLength);
93
+ }
94
+ getSendingKey() {
95
+ if (!this.sendingChain)
96
+ throw new Error;
97
+ const out = Session.symmetricRatchet(this.sendingChain);
98
+ this.sendingChain = out[0];
99
+ this.sendingCount++;
100
+ return out[1];
101
+ }
102
+ getReceivingKey() {
103
+ if (!this.receivingChain)
104
+ throw new Error();
105
+ const out = Session.symmetricRatchet(this.receivingChain);
106
+ this.receivingChain = out[0];
107
+ this.receivingCount++;
108
+ return out[1];
109
+ }
110
+ /**
111
+ * Encrypts a message payload using the current sending chain.
112
+ *
113
+ * @param message - The message as a Uint8Array.
114
+ * @returns An EncryptedPayload or undefined if encryption fails.
115
+ */
116
+ encrypt(message) {
117
+ try {
118
+ const key = this.getSendingKey();
119
+ if (this.sendingCount >= EncryptedPayloadConstructor.maxCount || this.previousCount >= EncryptedPayloadConstructor.maxCount)
120
+ throw new Error();
121
+ const nonce = crypto_1.default.randomBytes(EncryptedPayloadConstructor.nonceLength);
122
+ const ciphertext = crypto_1.default.box.encrypt(message, nonce, key);
123
+ return new EncryptedPayloadConstructor(this.sendingCount, this.previousCount, this.keyPair.publicKey, nonce, ciphertext);
124
+ }
125
+ catch (error) {
126
+ return undefined;
127
+ }
128
+ }
129
+ /**
130
+ * Decrypts an encrypted message.
131
+ *
132
+ * @param payload - The received encrypted message.
133
+ * @returns The decrypted message as a Uint8Array, or undefined if decryption fails.
134
+ */
135
+ decrypt(payload) {
136
+ var _a;
137
+ try {
138
+ const encrypted = EncryptedPayload.from(payload);
139
+ const publicKey = encrypted.publicKey;
140
+ if (!(0, utils_1.verifyUint8Array)(publicKey, this._remoteKey)) {
141
+ while (this.receivingCount < encrypted.previous)
142
+ this.previousKeys.set(this.receivingCount, this.getReceivingKey());
143
+ this.setRemoteKey(publicKey);
144
+ }
145
+ let key;
146
+ const count = encrypted.count;
147
+ if (this.receivingCount < count) {
148
+ let i = 0;
149
+ while (this.receivingCount < count - 1 && i < Session.skipLimit) {
150
+ this.previousKeys.set(this.receivingCount, this.getReceivingKey());
151
+ }
152
+ key = this.getReceivingKey();
153
+ }
154
+ else {
155
+ key = this.previousKeys.get(count);
156
+ }
157
+ if (!key)
158
+ return undefined;
159
+ return (_a = crypto_1.default.box.decrypt(encrypted.ciphertext, encrypted.nonce, key)) !== null && _a !== void 0 ? _a : undefined;
160
+ }
161
+ catch (error) {
162
+ return undefined;
163
+ }
164
+ }
165
+ /**
166
+ * Export the state of the session;
167
+ */
168
+ export() {
169
+ return JSON.stringify({
170
+ remoteKey: (0, utils_1.encodeBase64)(this._remoteKey),
171
+ rootKey: (0, utils_1.encodeBase64)(this.rootKey),
172
+ sendingChain: (0, utils_1.encodeBase64)(this.sendingChain),
173
+ receivingChain: (0, utils_1.encodeBase64)(this.receivingChain),
174
+ sendingCount: this.sendingCount,
175
+ receivingCount: this.receivingCount,
176
+ //previousCount: this.previousCount
177
+ });
178
+ }
179
+ /**
180
+ * Import a state.
181
+ *
182
+ * @param json string returned by `export()` method.
183
+ * @returns session with the state parsed.
184
+ */
185
+ static import(json) {
186
+ const data = JSON.parse(json);
187
+ const session = new Session({ remoteKey: (0, utils_1.decodeBase64)(data.remoteKey), preSharedKey: (0, utils_1.decodeBase64)(data.rootKey) });
188
+ session.sendingChain = (0, utils_1.decodeBase64)(data.sendingChain);
189
+ session.receivingChain = (0, utils_1.decodeBase64)(data.receivingChain);
190
+ session.sendingCount = data.sendingCount;
191
+ session.receivingCount = data.receivingCount;
192
+ return session;
193
+ }
194
+ static symmetricRatchet(chain, salt, info) {
195
+ const hash = crypto_1.default.hkdf(chain, salt !== null && salt !== void 0 ? salt : new Uint8Array(), info, Session.keyLength * 2);
196
+ return [new Uint8Array(hash.buffer, 0, Session.keyLength), new Uint8Array(hash.buffer, Session.keyLength)];
197
+ }
198
+ }
199
+ exports.Session = Session;
200
+ Session.skipLimit = 1000;
201
+ Session.version = 1;
202
+ Session.rootKeyLength = crypto_1.default.box.keyLength;
203
+ /**
204
+ * The fixed key length (in bytes) used throughout the Double Ratchet session.
205
+ * Typically 32 bytes (256 bits) for symmetric keys.
206
+ */
207
+ Session.keyLength = 32;
208
+ class EncryptedPayload {
209
+ /**
210
+ * Static factory method that constructs an `EncryptedPayload` from a raw Uint8Array.
211
+ *
212
+ * @param array - A previously serialized encrypted payload.
213
+ * @returns An instance of `EncryptedPayload`.
214
+ */
215
+ static from(array) {
216
+ return new EncryptedPayloadConstructor(array);
217
+ }
218
+ }
219
+ exports.EncryptedPayload = EncryptedPayload;
220
+ class EncryptedPayloadConstructor {
221
+ constructor(...arrays) {
222
+ var _a;
223
+ arrays = arrays.filter(value => value !== undefined);
224
+ if (arrays[0] instanceof EncryptedPayloadConstructor) {
225
+ this.raw = arrays[0].raw;
226
+ return this;
227
+ }
228
+ if (typeof arrays[0] === 'number')
229
+ arrays[0] = (0, utils_1.numberToUint8Array)(arrays[0], EncryptedPayloadConstructor.countLength);
230
+ if (typeof arrays[1] === 'number')
231
+ arrays[1] = (0, utils_1.numberToUint8Array)(arrays[1], EncryptedPayloadConstructor.countLength);
232
+ if (arrays.length > 1) {
233
+ arrays.unshift((_a = (typeof arrays[5] === 'number' ? (0, utils_1.numberToUint8Array)(arrays[5]) : arrays[5])) !== null && _a !== void 0 ? _a : (0, utils_1.numberToUint8Array)(Session.version));
234
+ }
235
+ this.raw = (0, utils_1.concatUint8Array)(...arrays);
236
+ }
237
+ get length() { return this.raw.length; }
238
+ get version() { return (0, utils_1.numberFromUint8Array)(new Uint8Array(this.raw.buffer, ...Offsets.version.get)); }
239
+ get count() { return (0, utils_1.numberFromUint8Array)(new Uint8Array(this.raw.buffer, ...Offsets.count.get)); }
240
+ get previous() { return (0, utils_1.numberFromUint8Array)(new Uint8Array(this.raw.buffer, ...Offsets.previous.get)); }
241
+ get publicKey() { return new Uint8Array(this.raw.buffer, ...Offsets.publicKey.get); }
242
+ get nonce() { return new Uint8Array(this.raw.buffer, ...Offsets.nonce.get); }
243
+ get ciphertext() { return new Uint8Array(this.raw.buffer, Offsets.ciphertext.start); }
244
+ /*public get signature() { return this.signed ? new Uint8Array(this.raw.buffer, this.raw.length - EncryptedPayloadConstructor.signatureLength) : undefined }
245
+
246
+ public setSignature(signature: Uint8Array): this {
247
+ this.raw = concatUint8Array(this.raw, signature);
248
+ this.signed = true;
249
+ return this;
250
+ }
251
+
252
+ public encodeUnsigned(): Uint8Array {
253
+ return !this.signed ? this.raw : new Uint8Array(this.raw.buffer, 0, this.raw.length - EncryptedPayloadConstructor.signatureLength);
254
+ }
255
+
256
+ public encode(fixedLength?: number): Uint8Array {
257
+ if (fixedLength) {
258
+ var padStart = Math.floor(Math.random() * fixedLength - 2 - this.raw.length);
259
+ var padEnd = Math.floor(padStart + 2 + this.raw.length);
260
+ } else {
261
+ padStart = Math.floor(Math.random() * (EncryptedPayloadConstructor.maxPadLength - EncryptedPayloadConstructor.minPadLength) + 1) + EncryptedPayloadConstructor.minPadLength;
262
+ padEnd = Math.floor(Math.random() * (EncryptedPayloadConstructor.maxPadLength - EncryptedPayloadConstructor.minPadLength) + 1) + EncryptedPayloadConstructor.minPadLength;
263
+ }
264
+ return concatUint8Array(
265
+ randomBytes(padStart - 1).map((value) => value !== 255 ? value : (value - 1)),
266
+ new Uint8Array(1).fill(255), this.raw, new Uint8Array(1).fill(255),
267
+ randomBytes(padEnd).map((value) => value !== 255 ? value : (value - 1)),
268
+ );
269
+ }*/
270
+ encode() {
271
+ return this.raw;
272
+ }
273
+ decode() {
274
+ return {
275
+ version: this.version,
276
+ count: this.count,
277
+ previous: this.previous,
278
+ publicKey: (0, utils_1.encodeBase64)(this.publicKey),
279
+ nonce: (0, utils_1.encodeBase64)(this.nonce),
280
+ ciphertext: (0, utils_1.encodeUTF8)(this.ciphertext),
281
+ //signature: encodeBase64(this.signature)
282
+ };
283
+ }
284
+ toString() {
285
+ return (0, utils_1.encodeUTF8)(this.raw);
286
+ }
287
+ toJSON() {
288
+ return JSON.stringify(this.decode());
289
+ }
290
+ }
291
+ //public static readonly signatureLength = crypto.EdDSA.signatureLength;
292
+ EncryptedPayloadConstructor.secretKeyLength = crypto_1.default.ECDH.secretKeyLength;
293
+ EncryptedPayloadConstructor.publicKeyLength = crypto_1.default.ECDH.publicKeyLength;
294
+ EncryptedPayloadConstructor.keyLength = crypto_1.default.box.keyLength;
295
+ EncryptedPayloadConstructor.nonceLength = crypto_1.default.box.nonceLength;
296
+ //public static readonly minPadLength = 6;
297
+ //public static readonly maxPadLength = 14;
298
+ EncryptedPayloadConstructor.maxCount = 65536; //32768;
299
+ EncryptedPayloadConstructor.countLength = 2;
300
+ class Offsets {
301
+ static set(start, length) {
302
+ class Offset {
303
+ constructor(start, length) {
304
+ this.start = start;
305
+ this.length = length;
306
+ if (typeof length === 'number')
307
+ this.end = start + length;
308
+ }
309
+ get get() {
310
+ return [this.start, this.length];
311
+ }
312
+ }
313
+ return new Offset(start, length);
314
+ }
315
+ }
316
+ Offsets.checksum = Offsets.set(0, 0);
317
+ Offsets.version = Offsets.set(Offsets.checksum.end, 1);
318
+ Offsets.count = Offsets.set(Offsets.version.end, EncryptedPayloadConstructor.countLength);
319
+ Offsets.previous = Offsets.set(Offsets.count.end, EncryptedPayloadConstructor.countLength);
320
+ Offsets.publicKey = Offsets.set(Offsets.previous.end, EncryptedPayloadConstructor.publicKeyLength);
321
+ Offsets.nonce = Offsets.set(Offsets.publicKey.end, EncryptedPayloadConstructor.nonceLength);
322
+ Offsets.ciphertext = Offsets.set(Offsets.nonce.end, undefined);
323
+ class KeyMap {
324
+ constructor(iterable) {
325
+ return new KeyMapConstructor(iterable);
326
+ }
327
+ }
328
+ class KeyMapConstructor extends Map {
329
+ constructor(iterable) {
330
+ super(iterable);
331
+ }
332
+ get(key) {
333
+ const out = super.get(key);
334
+ if (out && !super.delete(key))
335
+ throw new Error();
336
+ return out;
337
+ }
338
+ }
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@freesignal/protocol",
3
+ "version": "0.1.0",
4
+ "description": "Signal Protocol implementation in javascript",
5
+ "license": "GPL-3.0-or-later",
6
+ "author": "Christian Braghette",
7
+ "type": "commonjs",
8
+ "main": "index.js",
9
+ "scripts": {
10
+ "test": "tsc && node ./build/test.js",
11
+ "build": "tsc && node ./build/build.js"
12
+ },
13
+ "dependencies": {
14
+ "base64-js": "^1.5.1",
15
+ "js-sha3": "^0.9.3",
16
+ "tweetnacl": "^1.0.3",
17
+ "uuid": "^11.1.0"
18
+ },
19
+ "devDependencies": {
20
+ "@types/node": "^24.2.1"
21
+ }
22
+ }
package/test.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const x3dh_1 = require("./x3dh");
7
+ const crypto_1 = __importDefault(require("./crypto"));
8
+ const utils_1 = require("./utils");
9
+ const bob = new x3dh_1.X3DH(crypto_1.default.EdDSA.keyPair());
10
+ const alice = new x3dh_1.X3DH(crypto_1.default.EdDSA.keyPair());
11
+ const bobmessage = bob.generateSyn();
12
+ const { rootKey, ackMessage: aliceack } = alice.digestSyn(bobmessage);
13
+ if ((0, utils_1.verifyUint8Array)(rootKey, bob.digestAck(aliceack))) {
14
+ console.log("Session established successfully between Alice and Bob.");
15
+ }
16
+ else
17
+ console.log("Error");
package/utils.d.ts ADDED
@@ -0,0 +1,78 @@
1
+ /**
2
+ * FreeSignal Protocol
3
+ *
4
+ * Copyright (C) 2025 Christian Braghette
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with this program. If not, see <https://www.gnu.org/licenses/>
18
+ */
19
+ /**
20
+ * Decodes a Uint8Array into a UTF-8 string.
21
+ *
22
+ * @param array - The input byte array.
23
+ * @returns The UTF-8 encoded string.
24
+ */
25
+ export declare function encodeUTF8(array?: Uint8Array): string;
26
+ /**
27
+ * Encodes a UTF-8 string into a Uint8Array.
28
+ *
29
+ * @param string - The input string.
30
+ * @returns The resulting Uint8Array.
31
+ */
32
+ export declare function decodeUTF8(string?: string): Uint8Array;
33
+ /**
34
+ * Encodes a Uint8Array into a Base64 string.
35
+ *
36
+ * @param array - The input byte array.
37
+ * @returns The Base64 encoded string.
38
+ */
39
+ export declare function encodeBase64(array?: Uint8Array): string;
40
+ /**
41
+ * Decodes a Base64 string into a Uint8Array.
42
+ *
43
+ * @param string - The Base64 string.
44
+ * @returns The decoded Uint8Array.
45
+ */
46
+ export declare function decodeBase64(string?: string): Uint8Array;
47
+ export declare function encodeHex(array?: Uint8Array): string;
48
+ export declare function decodeHex(string?: string): Uint8Array;
49
+ /**
50
+ * Converts a Uint8Array into a number.
51
+ *
52
+ * @param array - The input byte array.
53
+ * @returns The resulting number.
54
+ */
55
+ export declare function numberFromUint8Array(array?: Uint8Array, endian?: 'big' | 'little'): number;
56
+ /**
57
+ * Converts a number into a Uint8Array of specified length.
58
+ *
59
+ * @param number - The number to convert.
60
+ * @param length - The desired output length.
61
+ * @returns A Uint8Array representing the number.
62
+ */
63
+ export declare function numberToUint8Array(number?: number, length?: number, endian?: 'big' | 'little'): Uint8Array;
64
+ /**
65
+ * Compare Uint8Arrays.
66
+ *
67
+ * @param a - First Uint8Array to compare to.
68
+ * @param b - Arrays to compare to the first one.
69
+ * @returns A boolean value.
70
+ */
71
+ export declare function verifyUint8Array(a?: Uint8Array, ...b: (Uint8Array | undefined)[]): boolean;
72
+ /**
73
+ * Concat Uint8Arrays.
74
+ *
75
+ * @param arrays - Uint8Array to concat.
76
+ * @returns A Uint8Array
77
+ */
78
+ export declare function concatUint8Array(...arrays: Uint8Array[]): Uint8Array;