百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

sqleet

> 数据库
开源

更少消耗的 SQLite3 加密

394 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

更少消耗的 SQLite3 加密

Warning: This project is currently unmaintained due to the deprecation of codec API in SQLite3 upstream. Therefore, sqleet is stuck at SQLite version 3.31.1 until a major rewrite happens. Meanwhile, I recommend using wxSQLite3 which offers sqleet-compatible encryption for the latest version(s) of SQLite3.


sqleet is an encryption extension for SQLite3. The encryption is transparent (on-the-fly) and based on modern cryptographic algorithms designed for high performance in software and robust side-channel resistance. The compilation of sqleet is easy because there are no external dependencies, which simplifies cross-compiling and cross-platform development.

In the spirit of SQLite3, the sqleet source code is in the public domain.

  • Compiling
  • Example
  • Cryptography
  • sqleet API
    • C programming interface
    • URI configuration interface
  • Android/iOS support
  • Versioning scheme
  • License

Compiling

SQLite3 shell with sqleet encryption support can be compiled as follows:

bash
% # UNIX
% gcc sqleet.c shell.c -o sqleet -lpthread -ldl

% # Windows
% gcc sqleet.c shell.c -o sqleet

Example demonstrates the use of the sqleet encryption extension with the compiled shell. For application programmers, sqleet API offers C programming interface and language-agnostic URI-based configuration interface for run-time management of the encryption settings.

To use sqleet as a library, the recommended way is to download a preconfigured release package instead of cloning the git repository. Release package contains sqleet.c and sqleet.h amalgamations that are drop-in replacements for the official sqlite3.c and sqlite3.h amalgamations. Non-amalgamated sqleet.c and sqleet.h files from master branch can be used as drop-in replacements similarly, assuming all necessary sqleet source files are available during compilation. However, sqleet development mainly happens in the master branch, so release are considered to be more stable and a better choice for the average user.

Building a custom release version of sqleet is a straightforward task.

  • Clone or fork sqleet and patch it as you wish
  • Create source and header amalgamations for release
    • ./script/amalgamate.sh sqlame.c
    • ./script/amalgamate.sh sqlame.h
  • Package the amalgamations with other release files

script/release.sh shows the exact release procedure of sqleet.

Example

Encrypting an existing database hello.db with a key (i.e., password) "swordfish".

…

Notice that the data of the encrypted database is indistinguishable from random. After encryption, the unencrypted data is accessible only with the correct key.

[sqleet]% ./sqleet hello.db
SQLite version 3.28.0 2019-04-16 19:49:53
Enter ".help" for usage hints.
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
/**** ERROR: (26) file is not a database *****/
ROLLBACK; -- due to errors
sqlite> PRAGMA key='swordfish';
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE hello(x);
INSERT INTO hello VALUES('Hello, world!');
COMMIT;

Instead of PRAGMA commands, the key can also be provided in SQLite3 URI filename using key parameter.

[sqleet]% ./sqleet 'file:hello.db?key=swordfish' 'SELECT * FROM hello'
Hello, world!

Cryptography

  • PBKDF2-HMAC-SHA256 key derivation with a 16-byte salt and 12345 iterations.
  • ChaCha20 stream cipher with one-time keys.
  • Poly1305 authentication tags.

A low-level description of the database encryption scheme is available in sqleet.c:265.

sqleet API

The public sqleet API consists of C programming interface and URI configuration interface.

C programming interface

sqleet defines SQLITE_HAS_CODEC compile-time option to expose SQLite3 encryption API, i.e., C functions sqlite3_key() and sqlite3_rekey() for managing database encryption keys. These functions can be called directly from C code, while other programming languages need to call the C functions via FFI mechanism. Another way to invoke the functions is with PRAGMA key and PRAGMA rekey commands (see Example).

c
SQLITE_API int sqlite3_key(      /* Invoked by PRAGMA key='x' */
  sqlite3 *db,                   /* Database to key */
  const void *pKey, int nKey     /* Key (password) */
);

sqlite3_key() is typically called immediately after sqlite3_open() to specify an encryption key (password) for the opened database. The function validates the key by decrypting the first page of the database from disk. Return value is SQLITE_OK if the given key was correct; otherwise, a non-zero SQLite3 error code is returned and subsequent attempts to read or write the database will fail.

c
SQLITE_API int sqlite3_rekey(    /* Invoked by PRAGMA rekey='x' */
  sqlite3 *db,                   /* Database to rekey */
  const void *pKey, int nKey     /* New key (password) */
);

sqlite3_rekey() changes the database encryption key. This includes encrypting the database the first time, fully decrypting the database (if nKey == 0), as well as re-encrypting it with a new key. Internally, the function runs VACUUM command to encrypt or decrypt all pages of the database, whereas re-encryption with a new key is performed directly by processing each page sequentially. Return value is SQLITE_OK on success and an SQLite3 error code on failure.

In addition, there are sqlite3_key_v2() and sqlite3_rekey_v2() functions that accept name of the target database as the second parameter.


Note:
In sqleet, the contents of an encrypted database file are indistinguishable from random data (of the same length). This is a conscious design decision, but as a drawback, database settings cannot be read from the database file. Therefore, it is the user's responsibility to properly initialize database settings before accessing the database. The most common issue is that opening a database fails regardless of valid key because the page size of the database differs from the default 4096 and page_size has not been set to the correct value with PRAGMA or URI API.

The official SQLite Encryption Extension (SEE) leaves bytes 16..23 of the database header unencrypted so that page size and other settings can be directly read from encrypted databases, which obviously makes SEE-encrypted databases distinguishable from random data. In sqleet, this behavior can be optionally enabled with -DSKIP_HEADER_BYTES=24 compile-time flag (bytes 0..15 contain the KDF salt so only the bytes 16..23 are actually skipped and left unencrypted). At run-time, the compile-time default can be overridden with URI parameter skip=n where n is the skip amount.


URI configuration interface

Disclaimer: URI interface is experimental and subject to changes in future versions. Use at your own risk!

Run-time configuration of sqleet encryption is implemented based on SQLite3 URI filenames which contain configuration parameters for databases. List of URI parameters supported by sqleet:

Parameter Description
key Encryption key for sqlite3_key() after opening the database
salt 16-byte salt for the key derivation function (KDF)
header 16-byte header overwriting the database magic header (or salt)
kdf Key derivation function (only none supported for now)
skip Run-time setting overriding compile-time SKIP_HEADER_BYTES
page_size Equivalent to page_size PRAGMA

Parameters key, salt and header have additional hex-prefixed versions that accept hex input strings such as '73716c656574'.

Parameters salt and header expect 16-byte input strings. Shorter strings are zero-padded to 16-bytes, while longer inputs get automatically rejected.

Parameter header represents the first 16 bytes of the database file, that is, SQLite3 magic header string for unencrypted databases. For encrypted databases, header defaults to the value of salt unless explicitly set to other value. Remember that salt is a parameter for the key derivation function (KDF) which is stored in the beginning of the database file by default, in which case both salt and header contain the same value (KDF salt). Sometimes, however, the user may want to keep the salt secret, or control the first 16 bytes of the database file for some purpose. In such cases, the user stores the salt and then overwrites the beginning of the database file with any 16-byte header value. (If this explanation was too abstract or nonsensical to fully grasp, see the iOS workaround in the end of Android/iOS support for a practical real-world use-case of header feature).

URI parameter kdf=none disables the default PBKDF2-HMAC-SHA256 key derivation. If KDF is disabled, key and hexkey accept a 32-byte raw key that becomes the master encryption key which otherwise would be derived by the KDF from the key and salt. Disabling KDF is a powerful feature for advanced users who need full control of the key derivation process.

Parameters skip and page_size override the compile-time SKIP_HEADER_BYTES value and the database PRAGMA page_size configuration.

Changing URI settings of an existing database can be accomplished with VACUUM INTO (introduced in SQLite 3.27.0) by giving new URI parameter values in the INTO filename. For example, VACUUM INTO 'file:skipped.db?skip=24' vacuums the current main database to file skipped.db with skip set to 24. Other URI settings, including the encryption key, are inherited from the main database unless key parameter is specified, in which case any undefined parameters are initialized to default values. Database settings update with VACUUM INTO is complex operation with many special cases and important details (omitted here). So be prepared for some undocumented behavior, but please open an issue if encountering obviously broken on wrong behavior.

Erroneus parameters, such as unsupported parameter value or otherwise bad input, returns a non-zero SQLite3 error code when opening (or vacuuming) a database. The current version returns SQLITE_MISUSE error, in most cases, if URI parsing fails or the resulting configuration is invalid.

Android/iOS support

sqleet does not have an out-of-the-box support for Android. However, SQLite Android Bindings project provides an easy way to bundle a custom SQLite3 version (such as sqleet) into an Android application with the standard Android interface android.database.sqlite. In particular, see Using The SQLite Encryption Extension page for build & usage instructions.

Likewise, sqleet does not offer an iOS version either, but compiling a custom SQLite3 with sqleet encryption support for iOS is a straightforward task (e.g., compile [switflyfalling/SQLiteLib](https://github.com/swi

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ccryptographyencryptionsqlitesqlite3

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库