Changeset

13632:844e7bf7b48a

util.sql: SQLCipher support This enables use of encrypted databases if LuaDBI or LuaSQLite3 has been linked against SQLCipher. Using `LD_PRELOAD` may work as well. Requires SQLCipher >= 4.0.0 due to the use of UPSERT
author Kim Alvefur <zash@zash.se>
date Thu, 23 Jan 2025 16:38:56 +0100
parents 13631:0fe27632a837
children 13633:6b84d11aa09b
files CHANGES util/sql.lua util/sqlite3.lua
diffstat 3 files changed, 13 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGES	Tue Jan 21 17:21:48 2025 +0100
+++ b/CHANGES	Thu Jan 23 16:38:56 2025 +0100
@@ -46,6 +46,7 @@
 - New 'keyval+' combined keyval/map store type
 - Performance improvements in internal archive stores
 - Ability to use SQLite3 storage using LuaSQLite3 instead of LuaDBI
+- SQLCipher support
 
 ### Module API
 
--- a/util/sql.lua	Tue Jan 21 17:21:48 2025 +0100
+++ b/util/sql.lua	Thu Jan 23 16:38:56 2025 +0100
@@ -84,6 +84,12 @@
 	dbh:autocommit(false); -- don't commit automatically
 	self.conn = dbh;
 	self.prepared = {};
+	if params.password then
+		local ok, err = self:execute(("PRAGMA key='%s'"):format(dbh:quote(params.password)));
+		if not ok then
+			return ok, err;
+		end
+	end
 	local ok, err = self:set_encoding();
 	if not ok then
 		return ok, err;
--- a/util/sqlite3.lua	Tue Jan 21 17:21:48 2025 +0100
+++ b/util/sqlite3.lua	Thu Jan 23 16:38:56 2025 +0100
@@ -114,6 +114,12 @@
 	if not dbh then return nil, err; end
 	self.conn = dbh;
 	self.prepared = {};
+	if params.password then
+		local ok, err = self:execute(("PRAGMA key='%s'"):format((params.password:gsub("'", "''"))));
+		if not ok then
+			return ok, err;
+		end
+	end
 	local ok, err = self:set_encoding();
 	if not ok then
 		return ok, err;