msiemens / tinydb

TinyDB is a lightweight document oriented database optimized for your happiness :)
https://tinydb.readthedocs.org
MIT License
6.84k stars 550 forks source link

is it possible to encrypt DB file and add authentication? #562

Closed jackywu closed 1 month ago

jackywu commented 6 months ago

is it possible to encrypt DB file and add authentication for connection ?

MrPigss commented 6 months ago

Definitely!  You could try to write your own storage (see docs). It's fairly simple; you only need four methods. __init__, read, write, and close.

Since you want to use authentication, I assume adding a password to the __init__ function would be the easiest. To actually encrypt the data, you can use something like cryptography. You can use these docs to create a password.

  1. On init, check if a password and/or key file exist (if not, create them).
  2. Check the provided password (given through the __init__ function).
  3. If it's correct, finish initialization; otherwise, raise an error or something.
  4. On every read, we'll decrypt; on every write, we'll encrypt.

Decrypting and encrypting on every read and write, respectively, will have a certain performance penalty, of course.  You could also decrypt the file once on initialization and encrypt it during the close, but this will result in the file being readable on disk for as long as the program is active. 

msiemens commented 1 month ago

As @MrPigss, it's possible to create a new storage that stores files on disk with encryption. Including such a storage within TinyDB core is out of scope for this project, though 🙂