m4heshd / better-sqlite3-multiple-ciphers

better-sqlite3 with multiple-cipher encryption support 🔒
MIT License
137 stars 27 forks source link

How to use `db.pragma('key, "some-long-string-with-quotes")`? #78

Closed titanism closed 2 months ago

titanism commented 8 months ago

We've read at https://www.zetetic.net/sqlcipher/sqlcipher-api/#example-2-raw-key-data-without-key-derivation that PRAGMA key accepts a "64 character hex string".

Screen Shot 2023-12-10 at 11 54 07 AM

How might we write this with pure JavaScript? Is there a way to do db.pragma('key', Buffer.from('something-with-quotes').toString('hex')) and pad it with null or something to get to 64 character hex string?

This issue is filed as there is an edge case where you may be using another library that does not have .key and .rekey built-in wrapper function and the password may contain quote characters, e.g. " which would break key="somekey" usage when variables are interpolated. True, you could simply escape the quotes, but we were wondering how you might write with this approach:

sqlite> PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'";
// this does not work since it's not 64 character hex string
db.pragma(
  `key="x'${Buffer.from('some-string-with-quotes').toString('hex')}'"`
);
titanism commented 8 months ago

Related https://github.com/xerial/sqlite-jdbc/issues/579

m4heshd commented 7 months ago

Sorry about the extremely delayed response. Any chance you could create a minimal reproducible repo that fails with the particular error? Time is a luxury for me at the moment.