1- import { uint8ArrayToBase64 , base64ToUint8Array , UTF8ToUint8 , uint8ToUTF8 } from '../utils' ;
2- import {
3- EmailBody ,
4- HybridEncKey ,
5- PwdProtectedKey ,
6- User ,
7- EmailPublicParameters ,
8- HybridEncryptedEmail ,
9- PwdProtectedEmail ,
10- Email ,
11- } from '../types' ;
1+ import { UTF8ToUint8 , uint8ToUTF8 } from '../utils' ;
2+ import { EmailBody , User , Email } from '../types' ;
123import { concatBytes } from '@noble/hashes/utils.js' ;
134
14- /**
15- * Converts a User type into a base64 string.
16- *
17- * @param user - The given user.
18- * @returns The base64 representation of the user.
19- */
20- export function userToBase64 ( user : User ) : string {
21- try {
22- const json = JSON . stringify ( user ) ;
23- return btoa ( json ) ;
24- } catch ( error ) {
25- throw new Error ( 'Failed to convert User to base64' , { cause : error } ) ;
26- }
27- }
28-
295export function userToBytes ( user : User ) : Uint8Array {
306 try {
317 const json = JSON . stringify ( user ) ;
@@ -44,22 +20,6 @@ export function recipientsToBytes(recipients: User[]): Uint8Array {
4420 }
4521}
4622
47- /**
48- * Converts a base64 string into a User type
49- *
50- * @param base64 - The base64 representation of the user.
51- * @returns The User type.
52- */
53- export function base64ToUser ( base64 : string ) : User {
54- try {
55- const json = atob ( base64 ) ;
56- const user : User = JSON . parse ( json ) ;
57- return user ;
58- } catch ( error ) {
59- throw new Error ( 'Failed to convert base64 to User' , { cause : error } ) ;
60- }
61- }
62-
6323/**
6424 * Converts an EmailBody type into a Uint8Array array.
6525 *
@@ -91,163 +51,6 @@ export function binaryToEmailBody(array: Uint8Array): EmailBody {
9151 }
9252}
9353
94- /**
95- * Converts a hybrid key of the type HybridEncKey into base64 string.
96- *
97- * @param encHybridKey - The HybridEncKey key.
98- * @returns The resulting base64 key encoding.
99- */
100- export function encHybridKeyToBase64 ( encHybridKey : HybridEncKey ) : string {
101- try {
102- const json = JSON . stringify ( {
103- kyberCiphertext : uint8ArrayToBase64 ( encHybridKey . kyberCiphertext ) ,
104- encryptedKey : uint8ArrayToBase64 ( encHybridKey . encryptedKey ) ,
105- } ) ;
106- const base64 = btoa ( json ) ;
107- return base64 ;
108- } catch ( error ) {
109- throw new Error ( 'Failed to convert hybrid key to base64' , { cause : error } ) ;
110- }
111- }
112-
113- /**
114- * Converts a base64 string into a hybrid key of the type HybridEncKey.
115- *
116- * @param base - The base64 encoding of the hybrid key.
117- * @returns The resulting HybridEncKey key.
118- */
119- export function base64ToEncHybridKey ( base64 : string ) : HybridEncKey {
120- try {
121- const json = atob ( base64 ) ;
122- const obj = JSON . parse ( json ) ;
123- return {
124- encryptedKey : base64ToUint8Array ( obj . encryptedKey ) ,
125- kyberCiphertext : base64ToUint8Array ( obj . kyberCiphertext ) ,
126- } ;
127- } catch ( error ) {
128- throw new Error ( 'Failed to convert base64 to hybrid key' , { cause : error } ) ;
129- }
130- }
131-
132- /**
133- * Converts a password-protected key of the type PwdProtectedKey into base64 string.
134- *
135- * @param pwdProtectedKey - The password-protected key of the type PwdProtectedKey.
136- * @returns The resulting base64 key encoding.
137- */
138- export function pwdProtectedKeyToBase64 ( pwdProtectedKey : PwdProtectedKey ) : string {
139- try {
140- const json = JSON . stringify ( {
141- encryptedKey : uint8ArrayToBase64 ( pwdProtectedKey . encryptedKey ) ,
142- salt : uint8ArrayToBase64 ( pwdProtectedKey . salt ) ,
143- } ) ;
144- const base64 = btoa ( json ) ;
145- return base64 ;
146- } catch ( error ) {
147- throw new Error ( 'Failed to convert password-protected key to base64' , { cause : error } ) ;
148- }
149- }
150-
151- /**
152- * Converts a base64 string into a password-protected key of the type PwdProtectedKey.
153- *
154- * @param base64 - The base64 string.
155- * @returns The resulting PwdProtectedKey key.
156- */
157- export function base64ToPwdProtectedKey ( base64 : string ) : PwdProtectedKey {
158- try {
159- const json = atob ( base64 ) ;
160- const obj = JSON . parse ( json ) ;
161- return {
162- encryptedKey : base64ToUint8Array ( obj . encryptedKey ) ,
163- salt : base64ToUint8Array ( obj . salt ) ,
164- } ;
165- } catch ( error ) {
166- throw new Error ( 'Failed to convert base64 to password-protected key' , { cause : error } ) ;
167- }
168- }
169-
170- /**
171- * Converts an email public parameters of type EmailPublicParameters into base64 string.
172- *
173- * @param params - The EmailPublicParameters email paramaters.
174- * @returns The resulting base64 string encoding.
175- */
176- export function paramsToBase64 ( params : EmailPublicParameters ) : string {
177- try {
178- const json = JSON . stringify ( {
179- ...params ,
180- sender : userToBase64 ( params . sender ) ,
181- recipient : userToBase64 ( params . recipient ) ,
182- recipients : params . recipients ?. map ( userToBase64 ) ,
183- } ) ;
184- const base64 = btoa ( json ) ;
185- return base64 ;
186- } catch ( error ) {
187- throw new Error ( 'Failed to convert email public parameters to base64' , { cause : error } ) ;
188- }
189- }
190-
191- /**
192- * Converts a base64 string into an email paramaters of the type EmailPublicParameters.
193- *
194- * @param base64 - The base64 string.
195- * @returns The resulting EmailPublicParameters email parameters.
196- */
197- export function base64ToParams ( base64 : string ) : EmailPublicParameters {
198- try {
199- const json = atob ( base64 ) ;
200- const obj = JSON . parse ( json ) ;
201- return {
202- ...obj ,
203- sender : base64ToUser ( obj . sender ) ,
204- recipient : base64ToUser ( obj . recipient ) ,
205- recipients : obj . recipients ?. map ( base64ToUser ) ,
206- } ;
207- } catch ( error ) {
208- throw new Error ( 'Failed to convert base64 to email params' , { cause : error } ) ;
209- }
210- }
211-
212- /**
213- * Converts an encrypted via hybrid encryption email into base64 string.
214- *
215- * @param email - The HybridEncryptedEmail encrypted via hybrid encryption email.
216- * @returns The resulting base64 string encoding.
217- */
218- export function hybridEncyptedEmailToBase64 ( email : HybridEncryptedEmail ) : string {
219- try {
220- const json = JSON . stringify ( {
221- encryptedKey : encHybridKeyToBase64 ( email . encryptedKey ) ,
222- enc : uint8ArrayToBase64 ( email . enc ) ,
223- recipientEmail : email . recipientEmail ,
224- } ) ;
225- const base64 = btoa ( json ) ;
226- return base64 ;
227- } catch ( error ) {
228- throw new Error ( 'Failed to convert hybrid email to base64' , { cause : error } ) ;
229- }
230- }
231-
232- /**
233- * Converts a pwd protected email into base64 string.
234- *
235- * @param email - The PwdProtectedEmail pwd protected email.
236- * @returns The resulting base64 string encoding.
237- */
238- export function pwdProtectedEmailToBase64 ( email : PwdProtectedEmail ) : string {
239- try {
240- const json = JSON . stringify ( {
241- encryptedKey : pwdProtectedKeyToBase64 ( email . encryptedKey ) ,
242- enc : uint8ArrayToBase64 ( email . enc ) ,
243- } ) ;
244- const base64 = btoa ( json ) ;
245- return base64 ;
246- } catch ( error ) {
247- throw new Error ( 'Failed to convert pwd protected email to base64' , { cause : error } ) ;
248- }
249- }
250-
25154/**
25255 * Converts an Email type into a Uint8Array array.
25356 *
0 commit comments