javascriptangularjsencryptionsjcl

Access IndexedDB as a file from javascript to encrypt it


I have created an indexedDB with pouchDB in my angular webapp. Each time I close my app, I would like to be able to encrypt this database file. So I had the idea of encrypting the database file with SJCL. My web app will be accessible only when the user can insert his password that will correctly decrypt.

So my first question is how can I access the indexedDB file within javascript so I can encrypt/decrypt the whole file? Secondly, what do you think of this idea of encrypting/decrypting the file?

Thanks


Solution

  • You can't access the IndexedDB file directly. Your best bet would be to use the crypto library to encrypt the documents that you're passing into PouchDB. E.g.:

    pouch.put({
      _id: 'myid', 
      sensitiveData: whateverLibrary.encryptString('secret')
    });
    

    If you wanted, you could even implement this as a PouchDB plugin! Basically you would just need to override the get/put/post/allDocs/bulkDocs methods to encrypt the documents before they're passed to PouchDB itself.