@@ -77,29 +77,26 @@ aes encrypt / decrypt browser
7777 var iv = crypto . getRandomValues ( new Uint8Array ( 12 ) ) ;
7878
7979 var algorithm = { name :'AES-GCM' , iv}
80- var key = key ;
80+ key = key ;
8181 var data = buf ;
8282
8383 var buf = await crypto . subtle . encrypt ( algorithm , key , data ) ;
8484
85- iv = buf_b64 ( iv ) ;
86- var data = buf_b64 ( buf ) ;
85+ buf = new Uint8Array ( buf ) ;
8786
88- var encrypted = { iv, data } ;
89- return encrypted ;
87+ var blob = iv_buf_blob ( iv , blob ) ;
88+ return blob ;
9089
9190 } //aesEncrypt
9291
9392
94- async function aesDecrypt ( key , encrypted ) {
93+ async function aesDecrypt ( key , blob ) {
9594
96- var { iv, data} = encrypted ;
97-
98- iv = b64_uint8 ( iv ) ;
95+ var { iv, data} = await blob_iv_buf ( blob ) ;
9996
10097 var algorithm = { name :'AES-GCM' , iv} ;
10198 key = key ;
102- data = b64_uint8 ( data ) ;
99+ data = data ;
103100
104101 var buf = await crypto . subtle . decrypt ( algorithm , key , data ) ;
105102
@@ -113,9 +110,29 @@ aes encrypt / decrypt browser
113110
114111
115112 function iv_buf_blob ( iv , buf ) {
113+
114+ var n1 = iv . length ;
115+ var n = n1 + buf . length ;
116+ var uint8 = new Uint8Array ( n ) ;
117+ uint8 . set ( iv , 0 ) ;
118+ uint8 . set ( buf , n1 ) ;
119+ return uint8 ;
120+
116121 } //iv_buf
117122
118123
124+ function blob_iv_buf ( blob , iv_bits = 96 ) {
125+
126+ var n = blob . size ;
127+ var buf = await blob . arrayBuffer ( ) ;
128+ var uint8 = new Uint8Array ( buf ) ;
129+ var iv = uint8 . slice ( 0 , iv_bits ) ;
130+ var buf = uint8 . slice ( iv_bits ) ;
131+ return { iv, buf} ;
132+
133+ } //blob_iv_buf
134+
135+
119136 async function blob_b64 ( blob ) {
120137
121138 var buf = await blob . arrayBuffer ( ) ;
0 commit comments