@openlv/core 0.0.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +165 -0
- package/README.md +3 -0
- package/dist/encryption/asymmetric.d.ts +22 -0
- package/dist/encryption/asymmetric.js +90 -0
- package/dist/encryption/asymmetric.js.map +1 -0
- package/dist/encryption/asymmetric.test.d.ts +1 -0
- package/dist/encryption/asymmetric.test.js +38 -0
- package/dist/encryption/asymmetric.test.js.map +1 -0
- package/dist/encryption/base64.d.ts +2 -0
- package/dist/encryption/base64.js +16 -0
- package/dist/encryption/base64.js.map +1 -0
- package/dist/encryption/base64.test.d.ts +1 -0
- package/dist/encryption/base64.test.js +21 -0
- package/dist/encryption/base64.test.js.map +1 -0
- package/dist/encryption/handshake.d.ts +10 -0
- package/dist/encryption/handshake.js +37 -0
- package/dist/encryption/handshake.js.map +1 -0
- package/dist/encryption/handshake.test.d.ts +1 -0
- package/dist/encryption/handshake.test.js +16 -0
- package/dist/encryption/handshake.test.js.map +1 -0
- package/dist/encryption/hash.d.ts +13 -0
- package/dist/encryption/hash.js +32 -0
- package/dist/encryption/hash.js.map +1 -0
- package/dist/encryption/index.d.ts +6 -0
- package/dist/encryption/index.js +7 -0
- package/dist/encryption/index.js.map +1 -0
- package/dist/encryption/random.d.ts +8 -0
- package/dist/encryption/random.js +21 -0
- package/dist/encryption/random.js.map +1 -0
- package/dist/errors/base.d.ts +29 -0
- package/dist/errors/base.js +99 -0
- package/dist/errors/base.js.map +1 -0
- package/dist/errors/index.d.ts +4 -0
- package/dist/errors/index.js +5 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/errors/signal.d.ts +4 -0
- package/dist/errors/signal.js +7 -0
- package/dist/errors/signal.js.map +1 -0
- package/dist/icons/index.d.ts +1 -0
- package/dist/icons/index.js +3 -0
- package/dist/icons/index.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/messages/index.d.ts +21 -0
- package/dist/messages/index.js +2 -0
- package/dist/messages/index.js.map +1 -0
- package/dist/session.d.ts +23 -0
- package/dist/session.js +2 -0
- package/dist/session.js.map +1 -0
- package/dist/settings.d.ts +15 -0
- package/dist/settings.js +2 -0
- package/dist/settings.js.map +1 -0
- package/dist/utils/encryption.d.ts +88 -0
- package/dist/utils/encryption.js +245 -0
- package/dist/utils/encryption.js.map +1 -0
- package/dist/utils/url.d.ts +3 -0
- package/dist/utils/url.js +66 -0
- package/dist/utils/url.js.map +1 -0
- package/dist/utils/url.test.d.ts +1 -0
- package/dist/utils/url.test.js +29 -0
- package/dist/utils/url.test.js.map +1 -0
- package/package.json +69 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { SessionLinkParameters } from "../session.js";
|
|
2
|
+
export type EncryptionKey = {
|
|
3
|
+
toString: () => string;
|
|
4
|
+
encrypt: (message: string) => Promise<string>;
|
|
5
|
+
};
|
|
6
|
+
export type DecryptionKey = {
|
|
7
|
+
toString: () => string;
|
|
8
|
+
decrypt: (message: string) => Promise<string>;
|
|
9
|
+
};
|
|
10
|
+
export declare const createEncryptionKey: (key: Uint8Array) => Promise<EncryptionKey>;
|
|
11
|
+
export declare const parseEncryptionKey: (serializedKey: string) => Promise<EncryptionKey>;
|
|
12
|
+
export declare const createDecryptionKey: (key: Uint8Array) => Promise<DecryptionKey>;
|
|
13
|
+
export type EncKeypair = {
|
|
14
|
+
encryptionKey: EncryptionKey;
|
|
15
|
+
decryptionKey: DecryptionKey;
|
|
16
|
+
};
|
|
17
|
+
export declare const generateKeyPair: () => Promise<EncKeypair>;
|
|
18
|
+
export declare const initEncryptionKeys: (initParameters?: SessionLinkParameters) => Promise<{
|
|
19
|
+
encryptionKey: EncryptionKey;
|
|
20
|
+
decryptionKey: DecryptionKey;
|
|
21
|
+
relyingEncryptionKey: EncryptionKey;
|
|
22
|
+
}>;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import nacl from "tweetnacl";
|
|
2
|
+
import { fromBase64, toBase64 } from "./base64.js";
|
|
3
|
+
const PUBLIC_KEY_BYTE_LENGTH = nacl.box.publicKeyLength;
|
|
4
|
+
const NONCE_BYTE_LENGTH = nacl.box.nonceLength;
|
|
5
|
+
const composePayload = (ephemeralPublicKey, nonce, ciphertext) => {
|
|
6
|
+
const payload = new Uint8Array(ephemeralPublicKey.length + nonce.length + ciphertext.length);
|
|
7
|
+
payload.set(ephemeralPublicKey, 0);
|
|
8
|
+
payload.set(nonce, ephemeralPublicKey.length);
|
|
9
|
+
payload.set(ciphertext, ephemeralPublicKey.length + nonce.length);
|
|
10
|
+
return toBase64(payload);
|
|
11
|
+
};
|
|
12
|
+
const decomposePayload = (payload) => {
|
|
13
|
+
const bytes = new Uint8Array(atob(payload)
|
|
14
|
+
.split("")
|
|
15
|
+
.map((char) => char.charCodeAt(0)));
|
|
16
|
+
if (bytes.length <= PUBLIC_KEY_BYTE_LENGTH + NONCE_BYTE_LENGTH) {
|
|
17
|
+
throw new Error("Encrypted payload is malformed.");
|
|
18
|
+
}
|
|
19
|
+
const ephemeralPublicKey = bytes.slice(0, PUBLIC_KEY_BYTE_LENGTH);
|
|
20
|
+
const nonce = bytes.slice(PUBLIC_KEY_BYTE_LENGTH, PUBLIC_KEY_BYTE_LENGTH + NONCE_BYTE_LENGTH);
|
|
21
|
+
const ciphertext = bytes.slice(PUBLIC_KEY_BYTE_LENGTH + NONCE_BYTE_LENGTH);
|
|
22
|
+
return {
|
|
23
|
+
ephemeralPublicKey,
|
|
24
|
+
nonce,
|
|
25
|
+
ciphertext,
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
export const createEncryptionKey = async (key) => {
|
|
29
|
+
const serializedPublicKey = toBase64(key);
|
|
30
|
+
return {
|
|
31
|
+
toString: () => serializedPublicKey,
|
|
32
|
+
encrypt: async (message) => {
|
|
33
|
+
const messageBytes = new TextEncoder().encode(message);
|
|
34
|
+
const ephemeralKeyPair = nacl.box.keyPair();
|
|
35
|
+
const nonce = nacl.randomBytes(NONCE_BYTE_LENGTH);
|
|
36
|
+
const ciphertext = nacl.box(messageBytes, nonce, key, ephemeralKeyPair.secretKey);
|
|
37
|
+
if (!ciphertext) {
|
|
38
|
+
throw new Error("Failed to encrypt message.");
|
|
39
|
+
}
|
|
40
|
+
return composePayload(ephemeralKeyPair.publicKey, nonce, ciphertext);
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
export const parseEncryptionKey = async (serializedKey) => {
|
|
45
|
+
const decodedPublicKey = fromBase64(serializedKey);
|
|
46
|
+
return createEncryptionKey(decodedPublicKey);
|
|
47
|
+
};
|
|
48
|
+
export const createDecryptionKey = async (key) => {
|
|
49
|
+
const serializedSecretKey = toBase64(key);
|
|
50
|
+
return {
|
|
51
|
+
toString: () => serializedSecretKey,
|
|
52
|
+
decrypt: async (message) => {
|
|
53
|
+
if (!message) {
|
|
54
|
+
return "";
|
|
55
|
+
}
|
|
56
|
+
const { ephemeralPublicKey, nonce, ciphertext } = decomposePayload(message);
|
|
57
|
+
const decrypted = nacl.box.open(ciphertext, nonce, ephemeralPublicKey, key);
|
|
58
|
+
if (!decrypted) {
|
|
59
|
+
throw new Error("Failed to decrypt message.");
|
|
60
|
+
}
|
|
61
|
+
return new TextDecoder().decode(decrypted);
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
export const generateKeyPair = async () => {
|
|
66
|
+
const { publicKey, secretKey } = nacl.box.keyPair();
|
|
67
|
+
const encryptionKey = await createEncryptionKey(publicKey);
|
|
68
|
+
const decryptionKey = await createDecryptionKey(secretKey);
|
|
69
|
+
return { encryptionKey, decryptionKey };
|
|
70
|
+
};
|
|
71
|
+
export const initEncryptionKeys = async (initParameters) => {
|
|
72
|
+
const keyPair = nacl.box.keyPair();
|
|
73
|
+
const { encryptionKey, decryptionKey } = await generateKeyPair();
|
|
74
|
+
if (initParameters &&
|
|
75
|
+
"publicKey" in initParameters &&
|
|
76
|
+
initParameters.publicKey) {
|
|
77
|
+
const relyingEncryptionKey = await parseEncryptionKey(initParameters.publicKey);
|
|
78
|
+
return {
|
|
79
|
+
encryptionKey,
|
|
80
|
+
decryptionKey,
|
|
81
|
+
relyingEncryptionKey,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
encryptionKey,
|
|
86
|
+
decryptionKey,
|
|
87
|
+
relyingEncryptionKey: await createEncryptionKey(keyPair.publicKey),
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
//# sourceMappingURL=asymmetric.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"asymmetric.js","sourceRoot":"","sources":["../../src/encryption/asymmetric.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAG7B,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEnD,MAAM,sBAAsB,GAAG,IAAI,CAAC,GAAG,CAAC,eAAe,CAAC;AACxD,MAAM,iBAAiB,GAAG,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC;AAE/C,MAAM,cAAc,GAAG,CACrB,kBAA8B,EAC9B,KAAiB,EACjB,UAAsB,EACd,EAAE;IACV,MAAM,OAAO,GAAG,IAAI,UAAU,CAC5B,kBAAkB,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,GAAG,UAAU,CAAC,MAAM,CAC7D,CAAC;IAEF,OAAO,CAAC,GAAG,CAAC,kBAAkB,EAAE,CAAC,CAAC,CAAC;IACnC,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,kBAAkB,CAAC,MAAM,CAAC,CAAC;IAC9C,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,kBAAkB,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC;IAElE,OAAO,QAAQ,CAAC,OAAO,CAAC,CAAC;AAC3B,CAAC,CAAC;AAEF,MAAM,gBAAgB,GAAG,CACvB,OAAe,EAKf,EAAE;IACF,MAAM,KAAK,GAAG,IAAI,UAAU,CAC1B,IAAI,CAAC,OAAO,CAAC;SACV,KAAK,CAAC,EAAE,CAAC;SACT,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU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AC,SAAS,CAAC,CAAC;IAE3D,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,CAAC;AAC1C,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,kBAAkB,GAAG,KAAK,EACrC,cAAsC,EACtC,EAAE;IACF,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;IACnC,MAAM,EAAE,aAAa,EAAE,aAAa,EAAE,GAAG,MAAM,eAAe,EAAE,CAAC;IAEjE,IACE,cAAc;QACd,WAAW,IAAI,cAAc;QAC7B,cAAc,CAAC,SAAS,EACxB,CAAC;QACD,MAAM,oBAAoB,GAAG,MAAM,kBAAkB,CACnD,cAAc,CAAC,SAAmB,CACnC,CAAC;QAEF,OAAO;YACL,aAAa;YACb,aAAa;YACb,oBAAoB;SACrB,CAAC;IACJ,CAAC;IAED,OAAO;QACL,aAAa;QACb,aAAa;QACb,oBAAoB,EAAE,MAAM,mBAAmB,CAAC,OAAO,CAAC,SAAS,CAAC;KACnE,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { generateKeyPair, parseEncryptionKey } from "./asymmetric.js";
|
|
3
|
+
describe("Asymmetric encryption", () => {
|
|
4
|
+
test("should generate a key pair", async () => {
|
|
5
|
+
const keyPair = await generateKeyPair();
|
|
6
|
+
expect(keyPair).toBeDefined();
|
|
7
|
+
expect(keyPair.encryptionKey).toBeDefined();
|
|
8
|
+
expect(keyPair.decryptionKey).toBeDefined();
|
|
9
|
+
});
|
|
10
|
+
test("should encrypt and decrypt a message", async () => {
|
|
11
|
+
const { encryptionKey, decryptionKey } = await generateKeyPair();
|
|
12
|
+
const message = "test";
|
|
13
|
+
// regular encryption decryption
|
|
14
|
+
const encrypted = await encryptionKey.encrypt(message);
|
|
15
|
+
const decrypted = await decryptionKey.decrypt(encrypted);
|
|
16
|
+
expect(decrypted).toBe(message);
|
|
17
|
+
});
|
|
18
|
+
test("should encrypt and decrypt a message with a parsed key", async () => {
|
|
19
|
+
const { encryptionKey, decryptionKey } = await generateKeyPair();
|
|
20
|
+
const message = "test";
|
|
21
|
+
const key = encryptionKey.toString();
|
|
22
|
+
const parsedKey = await parseEncryptionKey(key);
|
|
23
|
+
const parsedEncrypted = await parsedKey.encrypt(message);
|
|
24
|
+
console.log("parsedEncrypted", parsedEncrypted);
|
|
25
|
+
const decodedParsedEncrypted = await decryptionKey.decrypt(parsedEncrypted);
|
|
26
|
+
console.log("decodedParsedEncrypted", decodedParsedEncrypted);
|
|
27
|
+
expect(decodedParsedEncrypted).toBe(message);
|
|
28
|
+
});
|
|
29
|
+
test("should be able to encrypt large blob", async () => {
|
|
30
|
+
const { encryptionKey, decryptionKey } = await generateKeyPair();
|
|
31
|
+
const message = new Array(1024 * 1024).fill("a").join("");
|
|
32
|
+
console.log("message", message.length);
|
|
33
|
+
const encrypted = await encryptionKey.encrypt(message);
|
|
34
|
+
const decrypted = await decryptionKey.decrypt(encrypted);
|
|
35
|
+
expect(decrypted).toBe(message);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
//# sourceMappingURL=asymmetric.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"asymmetric.test.js","sourceRoot":"","sources":["../../src/encryption/asymmetric.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEhD,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAEtE,QAAQ,CAAC,uBAAuB,EAAE,GAAG,EAAE;IACrC,IAAI,CAAC,4BAA4B,EAAE,KAAK,IAAI,EAAE;QAC5C,MAAM,OAAO,GAAG,MAAM,eAAe,EAAE,CAAC;QAExC,MAAM,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,CAAC;Q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AAC,sCAAsC,EAAE,KAAK,IAAI,EAAE;QACtD,MAAM,EAAE,aAAa,EAAE,aAAa,EAAE,GAAG,MAAM,eAAe,EAAE,CAAC;QACjE,MAAM,OAAO,GAAG,IAAI,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAE1D,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;QAEvC,MAAM,SAAS,GAAG,MAAM,aAAa,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QACvD,MAAM,SAAS,GAAG,MAAM,aAAa,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAEzD,MAAM,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAClC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const toBase64 = (data) => {
|
|
2
|
+
let binary = "";
|
|
3
|
+
for (let index = 0; index < data.length; index += 1) {
|
|
4
|
+
binary += String.fromCharCode(data[index]);
|
|
5
|
+
}
|
|
6
|
+
return btoa(binary);
|
|
7
|
+
};
|
|
8
|
+
export const fromBase64 = (value) => {
|
|
9
|
+
const binary = atob(value);
|
|
10
|
+
const bytes = new Uint8Array(binary.length);
|
|
11
|
+
for (let index = 0; index < binary.length; index += 1) {
|
|
12
|
+
bytes[index] = binary.charCodeAt(index);
|
|
13
|
+
}
|
|
14
|
+
return bytes;
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=base64.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base64.js","sourceRoot":"","sources":["../../src/encryption/base64.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,IAAgB,EAAU,EAAE;IACnD,IAAI,MAAM,GAAG,EAAE,CAAC;IAEhB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,KAAK,IAAI,CAAC,EAAE,CAAC;QACpD,MAAM,IAAI,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;IAC7C,CAAC;IAED,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC;AACtB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,KAAa,EAAc,EAAE;IACtD,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC;IAC3B,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAE5C,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,IAAI,CAAC,EAAE,CAAC;QACtD,KAAK,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;IAC1C,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { fromBase64, toBase64 } from "./base64.js";
|
|
3
|
+
describe("Base64", () => {
|
|
4
|
+
test("should convert Uint8Array to base64", () => {
|
|
5
|
+
const data = new Uint8Array([0x00, 0x01, 0x02, 0x03]);
|
|
6
|
+
const base64 = toBase64(data);
|
|
7
|
+
expect(base64).toBe("AAECAw==");
|
|
8
|
+
});
|
|
9
|
+
test("should convert base64 to Uint8Array", () => {
|
|
10
|
+
const base64 = "AAECAw==";
|
|
11
|
+
const data = fromBase64(base64);
|
|
12
|
+
expect(data).toEqual(new Uint8Array([0x00, 0x01, 0x02, 0x03]));
|
|
13
|
+
});
|
|
14
|
+
test("should be transparent", () => {
|
|
15
|
+
const input = [1, 2, 3, 4];
|
|
16
|
+
const base64 = toBase64(new Uint8Array(input));
|
|
17
|
+
const data = fromBase64(base64);
|
|
18
|
+
expect(data).toEqual(new Uint8Array(input));
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
//# sourceMappingURL=base64.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base64.test.js","sourceRoot":"","sources":["../../src/encryption/base64.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEhD,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEnD,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;IACtB,IAAI,CAAC,qCAAqC,EAAE,GAAG,EAAE;QAC/C,MAAM,IAAI,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;QACtD,MAAM,MAAM,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;QAE9B,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAClC,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,qCAAqC,EAAE,GAAG,EAAE;QAC/C,MAAM,MAAM,GAAG,UAAU,CAAC;QAC1B,MAAM,IAAI,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC;QAEhC,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;IACjE,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,uBAAuB,EAAE,GAAG,EAAE;QACjC,MAAM,KAAK,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;QAC3B,MAAM,MAAM,GAAG,QAAQ,CAAC,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;QAC/C,MAAM,IAAI,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC;QAEhC,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;IAC9C,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type SymmetricKey = {
|
|
2
|
+
toString: () => string;
|
|
3
|
+
encrypt: (message: string) => Promise<string>;
|
|
4
|
+
decrypt: (message: string) => Promise<string>;
|
|
5
|
+
};
|
|
6
|
+
export declare const deriveSymmetricKey: (k: string) => Promise<SymmetricKey>;
|
|
7
|
+
/**
|
|
8
|
+
* Generate a 16-byte random shared key and return as hex
|
|
9
|
+
*/
|
|
10
|
+
export declare const generateHandshakeKey: () => Promise<SymmetricKey>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export const deriveSymmetricKey = async (k) => {
|
|
2
|
+
const baseKey = new TextEncoder().encode(k);
|
|
3
|
+
const sharedSecret = await crypto.subtle.importKey("raw", baseKey, { name: "AES-GCM" }, false, ["encrypt", "decrypt"]);
|
|
4
|
+
return {
|
|
5
|
+
toString: () => k,
|
|
6
|
+
encrypt: async (message) => {
|
|
7
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
8
|
+
const encrypted = await crypto.subtle.encrypt({ name: "AES-GCM", iv }, sharedSecret, new TextEncoder().encode(message));
|
|
9
|
+
const combined = new Uint8Array(iv.length + encrypted.byteLength);
|
|
10
|
+
combined.set(iv);
|
|
11
|
+
combined.set(new Uint8Array(encrypted), iv.length);
|
|
12
|
+
return btoa(String.fromCharCode.apply(null, Array.from(combined)));
|
|
13
|
+
},
|
|
14
|
+
decrypt: async (message) => {
|
|
15
|
+
const combined = new Uint8Array(atob(message)
|
|
16
|
+
.split("")
|
|
17
|
+
.map((char) => char.charCodeAt(0)));
|
|
18
|
+
const iv = combined.slice(0, 12);
|
|
19
|
+
const encrypted = combined.slice(12);
|
|
20
|
+
const decrypted = await crypto.subtle.decrypt({ name: "AES-GCM", iv }, sharedSecret, encrypted);
|
|
21
|
+
const decoder = new TextDecoder();
|
|
22
|
+
return decoder.decode(decrypted);
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Generate a 16-byte random shared key and return as hex
|
|
28
|
+
*/
|
|
29
|
+
export const generateHandshakeKey = async () => {
|
|
30
|
+
const array = new Uint8Array(16);
|
|
31
|
+
crypto.getRandomValues(array);
|
|
32
|
+
const keyHex = Array.from(array)
|
|
33
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
34
|
+
.join("");
|
|
35
|
+
return await deriveSymmetricKey(keyHex);
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=handshake.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handshake.js","sourceRoot":"","sources":["../../src/encryption/handshake.ts"],"names":[],"mappings":"AAOA,MAAM,CAAC,MAAM,kBAAkB,GAAG,KAAK,EAAE,CAAS,EAAyB,EAAE;IAC3E,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IAE5C,MAAM,YAAY,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,SAAS,CAChD,KAAK,EACL,OAAO,EACP,EAAE,IAAI,EAAE,SAAS,EAAE,EACnB,KAAK,EACL,CAAC,SAAS,EAAE,SAAS,CAAC,CACvB,CAAC;IAEF,OAAO;QACL,QAAQ,EAAE,GAAG,EAAE,CAAC,CAAC;QACjB,OAAO,EAAE,KAAK,EAAE,OAAe,EAAE,EAAE;YACjC,MAAM,EAAE,GAAG,MAAM,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC;YACtD,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,OAAO,CAC3C,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G,KAAK,IAA2B,EAAE;IACpE,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;IAEjC,MAAM,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;IAE9B,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;SAC7B,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;SAC3C,IAAI,CAAC,EAAE,CAAC,CAAC;IAEZ,OAAO,MAAM,kBAAkB,CAAC,MAAM,CAAC,CAAC;AAC1C,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { generateHandshakeKey } from "./handshake.js";
|
|
3
|
+
describe("Handshake", async () => {
|
|
4
|
+
const key = await generateHandshakeKey();
|
|
5
|
+
test("should generate a 16-byte random shared key", () => {
|
|
6
|
+
expect(key).toBeDefined();
|
|
7
|
+
expect(key.toString()).toHaveLength(32);
|
|
8
|
+
});
|
|
9
|
+
test("should encrypt and decrypt a message", async () => {
|
|
10
|
+
const message = "test";
|
|
11
|
+
const encrypted = await key.encrypt(message);
|
|
12
|
+
const decrypted = await key.decrypt(encrypted);
|
|
13
|
+
expect(decrypted).toBe(message);
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
//# sourceMappingURL=handshake.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handshake.test.js","sourceRoot":"","sources":["../../src/encryption/handshake.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEhD,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAEtD,QAAQ,CAAC,WAAW,EAAE,KAAK,IAAI,EAAE;IAC/B,MAAM,GAAG,GAAG,MAAM,oBAAoB,EAAE,CAAC;IAEzC,IAAI,CAAC,6CAA6C,EAAE,GAAG,EAAE;QACvD,MAAM,CAAC,GAAG,CAAC,CAAC,WAAW,EAAE,CAAC;QAC1B,MAAM,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC;IAC1C,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,sCAAsC,EAAE,KAAK,IAAI,EAAE;QACtD,MAAM,OAAO,GAAG,MAAM,CAAC;QACvB,MAAM,SAAS,GAAG,MAAM,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAC7C,MAAM,SAAS,GAAG,MAAM,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAE/C,MAAM,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAClC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { EncryptionKey } from "./asymmetric.js";
|
|
2
|
+
/**
|
|
3
|
+
* Hashes a public key using SHA-256 and returns the first 16 bytes as a hex string
|
|
4
|
+
*/
|
|
5
|
+
export declare const hashPublicKey: (publicKey: EncryptionKey) => Promise<string>;
|
|
6
|
+
export declare const validatePublicKeyHash: (publicKey: EncryptionKey, hash: string) => Promise<boolean>;
|
|
7
|
+
/**
|
|
8
|
+
* determines/or generates the hash and dictates wether is host or not
|
|
9
|
+
*/
|
|
10
|
+
export declare const initHash: (initialHash: string | undefined, encryptionKey: EncryptionKey) => Promise<{
|
|
11
|
+
hash: string;
|
|
12
|
+
isHost: boolean;
|
|
13
|
+
}>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const HASH_LENGTH = 16;
|
|
2
|
+
/**
|
|
3
|
+
* Hashes a public key using SHA-256 and returns the first 16 bytes as a hex string
|
|
4
|
+
*/
|
|
5
|
+
export const hashPublicKey = async (publicKey) => {
|
|
6
|
+
const buffer = new TextEncoder().encode(publicKey.toString());
|
|
7
|
+
const hash = await crypto.subtle.digest("SHA-256", buffer);
|
|
8
|
+
const hashArray = new Uint8Array(hash);
|
|
9
|
+
const hashHex = Array.from(hashArray)
|
|
10
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
11
|
+
.join("");
|
|
12
|
+
return hashHex.slice(0, HASH_LENGTH);
|
|
13
|
+
};
|
|
14
|
+
export const validatePublicKeyHash = async (publicKey, hash) => {
|
|
15
|
+
const computedHash = await hashPublicKey(publicKey);
|
|
16
|
+
return computedHash === hash;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* determines/or generates the hash and dictates wether is host or not
|
|
20
|
+
*/
|
|
21
|
+
export const initHash = async (initialHash, encryptionKey) => {
|
|
22
|
+
console.log("initialHash", initialHash);
|
|
23
|
+
const ourHash = await hashPublicKey(encryptionKey);
|
|
24
|
+
console.log("ourHash", ourHash);
|
|
25
|
+
const hash = initialHash || ourHash;
|
|
26
|
+
const isHost = hash === ourHash;
|
|
27
|
+
return {
|
|
28
|
+
hash,
|
|
29
|
+
isHost,
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
//# sourceMappingURL=hash.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../../src/encryption/hash.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,GAAG,EAAE,CAAC;AAEvB;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,EAChC,SAAwB,EACP,EAAE;IACnB,MAAM,MAAM,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC9D,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IAC3D,MAAM,SAAS,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC;IACvC,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC;SAClC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;SAC3C,IAAI,CAAC,EAAE,CAAC,CAAC;IAEZ,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,WAAW,CAAC,CAAC;AACvC,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,qBAAqB,GAAG,KAAK,EACxC,SAAwB,EACxB,IAAY,EACM,EAAE;IACpB,MAAM,YAAY,GAAG,MAAM,aAAa,CAAC,SAAS,CAAC,CAAC;IAEpD,OAAO,YAAY,KAAK,IAAI,CAAC;AAC/B,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,KAAK,EAC3B,WAA+B,EAC/B,aAA4B,EAC5B,EAAE;IACF,OAAO,CAAC,GAAG,CAAC,aAAa,EAAE,WAAW,CAAC,CAAC;IACxC,MAAM,OAAO,GAAG,MAAM,aAAa,CAAC,aAAa,CAAC,CAAC;IAEnD,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAChC,MAAM,IAAI,GAAG,WAAW,IAAI,OAAO,CAAC;IACpC,MAAM,MAAM,GAAG,IAAI,KAAK,OAAO,CAAC;IAEhC,OAAO;QACL,IAAI;QACJ,MAAM;KACP,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** biome-ignore-all lint/performance/noReExportAll: package entrypoint */
|
|
2
|
+
/** biome-ignore-all lint/performance/noBarrelFile: package entrypoint */
|
|
3
|
+
export * from "./asymmetric.js";
|
|
4
|
+
export * from "./handshake.js";
|
|
5
|
+
export * from "./hash.js";
|
|
6
|
+
export * from "./random.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** biome-ignore-all lint/performance/noReExportAll: package entrypoint */
|
|
2
|
+
/** biome-ignore-all lint/performance/noBarrelFile: package entrypoint */
|
|
3
|
+
export * from "./asymmetric.js";
|
|
4
|
+
export * from "./handshake.js";
|
|
5
|
+
export * from "./hash.js";
|
|
6
|
+
export * from "./random.js";
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/encryption/index.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,yEAAyE;AAEzE,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate a cryptographically secure URL-safe random string
|
|
3
|
+
*/
|
|
4
|
+
export declare const generateUrlSafeRandomString: (length: number) => string;
|
|
5
|
+
/**
|
|
6
|
+
* Generate a 16-character URL-safe session ID
|
|
7
|
+
*/
|
|
8
|
+
export declare const generateSessionId: () => string;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* URL-safe alphabet for session ID generation
|
|
3
|
+
*/
|
|
4
|
+
const URL_SAFE_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
5
|
+
/**
|
|
6
|
+
* Generate a cryptographically secure URL-safe random string
|
|
7
|
+
*/
|
|
8
|
+
export const generateUrlSafeRandomString = (length) => {
|
|
9
|
+
const array = new Uint8Array(length);
|
|
10
|
+
crypto.getRandomValues(array);
|
|
11
|
+
return Array.from(array)
|
|
12
|
+
.map((byte) => URL_SAFE_ALPHABET[byte % URL_SAFE_ALPHABET.length])
|
|
13
|
+
.join("");
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Generate a 16-character URL-safe session ID
|
|
17
|
+
*/
|
|
18
|
+
export const generateSessionId = () => {
|
|
19
|
+
return generateUrlSafeRandomString(16);
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=random.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random.js","sourceRoot":"","sources":["../../src/encryption/random.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,iBAAiB,GACrB,kEAAkE,CAAC;AAErE;;GAEG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG,CAAC,MAAc,EAAU,EAAE;IACpE,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IAErC,MAAM,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;IAE9B,OAAO,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;SACrB,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,iBAAiB,CAAC,IAAI,GAAG,iBAAiB,CAAC,MAAM,CAAC,CAAC;SACjE,IAAI,CAAC,EAAE,CAAC,CAAC;AACd,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,GAAW,EAAE;IAC5C,OAAO,2BAA2B,CAAC,EAAE,CAAC,CAAC;AACzC,CAAC,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
type ErrorConfig = {
|
|
2
|
+
getDocsUrl?: ((args: BaseErrorParameters) => string | undefined) | undefined;
|
|
3
|
+
version?: string | undefined;
|
|
4
|
+
};
|
|
5
|
+
export declare function setErrorConfig(config: ErrorConfig): void;
|
|
6
|
+
type BaseErrorParameters = {
|
|
7
|
+
cause?: BaseError | Error | undefined;
|
|
8
|
+
details?: string | undefined;
|
|
9
|
+
docsBaseUrl?: string | undefined;
|
|
10
|
+
docsPath?: string | undefined;
|
|
11
|
+
docsSlug?: string | undefined;
|
|
12
|
+
metaMessages?: string[] | undefined;
|
|
13
|
+
name?: string | undefined;
|
|
14
|
+
};
|
|
15
|
+
export type BaseErrorType = BaseError & {
|
|
16
|
+
name: "BaseError";
|
|
17
|
+
};
|
|
18
|
+
export declare class BaseError extends Error {
|
|
19
|
+
details: string;
|
|
20
|
+
docsPath?: string | undefined;
|
|
21
|
+
metaMessages?: string[] | undefined;
|
|
22
|
+
shortMessage: string;
|
|
23
|
+
version: string;
|
|
24
|
+
name: string;
|
|
25
|
+
constructor(shortMessage: string, args?: BaseErrorParameters);
|
|
26
|
+
walk(): Error;
|
|
27
|
+
walk(fn: (err: unknown) => boolean): Error | null;
|
|
28
|
+
}
|
|
29
|
+
export {};
|