@@ -175,29 +175,37 @@ async function signup(userInput, passInput, passInput2, emailInput) {
175
175
/**
176
176
* Called when the add password form is submitted.
177
177
*/
178
- function save ( siteInput , userInput , passInput ) {
178
+ async function save ( siteInput , userInput , passInput ) {
179
179
var site = siteInput . value ,
180
180
siteuser = userInput . value ,
181
- sitepasswd = passInput . value ,
182
- encrypted ; // this will need to be populated
181
+ sitepasswd = passInput . value ;
183
182
184
- // send the data, along with the encrypted password, to the server
185
- serverRequest ( "save" , // the resource to call
186
- {
187
- "site" : site ,
188
- "siteuser" : siteuser ,
189
- "sitepasswd" : encrypted
190
- } // this should be populated with any parameters the server needs
191
- ) . then ( function ( result ) {
192
- if ( result . response . ok ) {
193
- // any work after a successful save should be done here
183
+ const rawKey = sessionStorage . getItem ( 'encryption_key' ) ;
184
+ const key = await importKey ( rawKey ) ;
185
+ const {
186
+ encryptedMessage,
187
+ iv
188
+ } = await encryptMessage ( sitepasswd , key ) ;
194
189
195
- // update the sites list
196
- sites ( "save" ) ;
197
- }
198
- // show any server status messages
199
- serverStatus ( result ) ;
190
+ // send the data, along with the encrypted password, to the server
191
+ const result = await serverRequest ( "save" , {
192
+ "site" : site ,
193
+ "siteuser" : siteuser ,
194
+ "sitepasswd" : encryptedMessage ,
195
+ "siteiv" : iv
200
196
} ) ;
197
+
198
+ if ( result . response . ok ) {
199
+ // any work after a successful save should be done here
200
+ siteInput . value = '' ;
201
+ userInput . value = '' ;
202
+ passInput . value = '' ;
203
+
204
+ // update the sites list
205
+ sites ( "save" ) ;
206
+ }
207
+ // show any server status messages
208
+ serverStatus ( result ) ;
201
209
}
202
210
203
211
/**
@@ -337,3 +345,24 @@ async function hashMessage(message) {
337
345
338
346
return bufferToHexString ( digestValue ) ;
339
347
}
348
+
349
+ async function encryptMessage ( message , key ) {
350
+ const data = utf8ToUint8Array ( message ) ;
351
+ const iv = window . crypto . getRandomValues ( new Uint8Array ( 12 ) ) ;
352
+ const encryptedData = await window . crypto . subtle . encrypt ( {
353
+ name : 'AES-GCM' ,
354
+ iv : iv
355
+ } , key , data ) ;
356
+
357
+ return {
358
+ encryptedMessage : bufferToHexString ( encryptedData ) ,
359
+ iv : bufferToHexString ( iv )
360
+ } ;
361
+ }
362
+
363
+ async function importKey ( rawKey ) {
364
+ const rawKeyBuffer = hexStringToUint8Array ( rawKey ) ;
365
+ const key = await window . crypto . subtle . importKey ( 'raw' , rawKeyBuffer , 'AES-GCM' , false , [ 'encrypt' , 'decrypt' ] ) ;
366
+
367
+ return key ;
368
+ }
0 commit comments