@@ -19,72 +19,60 @@ const deriveKey = async (
1919 )
2020
2121export const encryptFile = async ( fileArrayBuffer : any , password : any ) => {
22- try {
23- const plainTextBytes = new Uint8Array ( fileArrayBuffer )
24- const passwordBytes = new TextEncoder ( ) . encode ( password )
22+ const plainTextBytes = new Uint8Array ( fileArrayBuffer )
23+ const passwordBytes = new TextEncoder ( ) . encode ( password )
2524
26- const salt = window . crypto . getRandomValues ( new Uint8Array ( 16 ) )
27- const iv = window . crypto . getRandomValues ( new Uint8Array ( 12 ) )
25+ const salt = window . crypto . getRandomValues ( new Uint8Array ( 16 ) )
26+ const iv = window . crypto . getRandomValues ( new Uint8Array ( 12 ) )
2827
29- const passwordKey = await importKeyFromBytes ( passwordBytes )
28+ const passwordKey = await importKeyFromBytes ( passwordBytes )
3029
31- const aesKey = await deriveKey ( passwordKey , [ 'encrypt' ] , {
32- name : 'PBKDF2' ,
33- salt : salt ,
34- iterations : 250000 ,
35- hash : 'SHA-256' ,
36- } )
37- const cipherBytes = await window . crypto . subtle . encrypt (
38- { name : 'AES-GCM' , iv : iv } ,
39- aesKey ,
40- plainTextBytes
41- )
30+ const aesKey = await deriveKey ( passwordKey , [ 'encrypt' ] , {
31+ name : 'PBKDF2' ,
32+ salt : salt ,
33+ iterations : 250000 ,
34+ hash : 'SHA-256' ,
35+ } )
36+ const cipherBytes = await window . crypto . subtle . encrypt (
37+ { name : 'AES-GCM' , iv : iv } ,
38+ aesKey ,
39+ plainTextBytes
40+ )
4241
43- const cipherBytesArray = new Uint8Array ( cipherBytes )
44- const resultBytes = new Uint8Array (
45- cipherBytesArray . byteLength + salt . byteLength + iv . byteLength
46- )
47- resultBytes . set ( salt , 0 )
48- resultBytes . set ( iv , salt . byteLength )
49- resultBytes . set ( cipherBytesArray , salt . byteLength + iv . byteLength )
42+ const cipherBytesArray = new Uint8Array ( cipherBytes )
43+ const resultBytes = new Uint8Array (
44+ cipherBytesArray . byteLength + salt . byteLength + iv . byteLength
45+ )
46+ resultBytes . set ( salt , 0 )
47+ resultBytes . set ( iv , salt . byteLength )
48+ resultBytes . set ( cipherBytesArray , salt . byteLength + iv . byteLength )
5049
51- return resultBytes
52- } catch ( error ) {
53- console . error ( 'Error encrypting file' )
54- console . error ( error )
55- throw error
56- }
50+ return resultBytes
5751}
5852
5953export const decryptFile = async ( cipher : any , password : any ) => {
60- try {
61- const cipherBytes = new Uint8Array ( cipher )
62- const passwordBytes = new TextEncoder ( ) . encode ( password )
54+ const cipherBytes = new Uint8Array ( cipher )
55+ const passwordBytes = new TextEncoder ( ) . encode ( password )
6356
64- const salt = cipherBytes . slice ( 0 , 16 )
65- const iv = cipherBytes . slice ( 16 , 16 + 12 )
66- const data = cipherBytes . slice ( 16 + 12 )
67- const passwordKey = await importKeyFromBytes ( passwordBytes )
68- const aesKey = await deriveKey ( passwordKey , [ 'decrypt' ] , {
69- name : 'PBKDF2' ,
70- salt : salt ,
71- iterations : 250000 ,
72- hash : 'SHA-256' ,
73- } )
57+ const salt = cipherBytes . slice ( 0 , 16 )
58+ const iv = cipherBytes . slice ( 16 , 16 + 12 )
59+ const data = cipherBytes . slice ( 16 + 12 )
60+ const passwordKey = await importKeyFromBytes ( passwordBytes )
61+ const aesKey = await deriveKey ( passwordKey , [ 'decrypt' ] , {
62+ name : 'PBKDF2' ,
63+ salt : salt ,
64+ iterations : 250000 ,
65+ hash : 'SHA-256' ,
66+ } )
7467
75- const decryptedContent = await window . crypto . subtle . decrypt (
76- {
77- name : 'AES-GCM' ,
78- iv : iv ,
79- } ,
80- aesKey ,
81- data
82- )
68+ const decryptedContent = await window . crypto . subtle . decrypt (
69+ {
70+ name : 'AES-GCM' ,
71+ iv : iv ,
72+ } ,
73+ aesKey ,
74+ data
75+ )
8376
84- return decryptedContent
85- } catch ( error ) {
86- console . error ( 'Error decrypting file' )
87- console . error ( error )
88- return
89- }
77+ return decryptedContent
9078}
0 commit comments