Nodejs:由于找不到支持加密解密的sqlite3模块,利用C++重新编译sqlite3实现带加密解密功能,支持nodejs调用。有需要用到加密数据库的可以留言。

Nodejs:由于找不到支持加密解密的sqlite3模块,利用C++重新编译sqlite3实现带加密解密功能,支持nodejs调用。有需要用到加密数据库的可以留言。

7 回复

Node.js:自定义加密解密功能的 SQLite3 模块

背景

在某些场景下,我们可能需要对 SQLite 数据库进行加密处理,以保护敏感数据。然而,现有的 sqlite3 模块并不直接支持加密功能。本文将介绍如何通过重新编译 SQLite3 库,并结合 C++ 实现加密和解密功能,最终让 Node.js 能够调用这些功能。

步骤概览

  1. 安装依赖:确保系统中已安装了 Node.js 和必要的编译工具。
  2. 下载并修改 SQLite3 源码:从 SQLite 官方网站下载源码,并对其进行修改以添加加密功能。
  3. 编译 SQLite3:使用 C++ 编译器重新编译带有加密功能的 SQLite3。
  4. 创建 Node.js 扩展:编写一个 Node.js 扩展模块来调用加密后的 SQLite3。
  5. 测试与验证:编写测试用例以验证加密和解密功能是否正常工作。

示例代码

1. 修改 SQLite3 源码

首先,从 SQLite 官方网站下载 SQLite3 源码(例如 sqlite-amalgamation-3370200.zip)。然后,在 sqlite3.c 文件中添加加密相关的代码。这里我们使用 SQLCipher 作为加密库的一个简单示例:

// 在 sqlite3.c 中添加以下内容
#include "sqlcipher/sqlite3.h"
2. 编译 SQLite3

使用以下命令重新编译 SQLite3:

gcc -DSQLITE_HAS_CODEC -I/path/to/sqlcipher/include -L/path/to/sqlcipher/lib -lsqlcipher -o sqlite3 sqlite3.c
3. 创建 Node.js 扩展

接下来,我们需要创建一个 Node.js 扩展模块来调用加密后的 SQLite3。这里是一个简单的示例:

// sqlite3-ext.js
const sqlite3 = require('sqlite3').verbose();
const sqlcipher = require('bindings')('sqlcipher.node');

class SecureDB {
    constructor(filename) {
        this.db = new sqlite3.Database(filename, (err) => {
            if (err) {
                console.error(err.message);
            }
            console.log('Connected to the database.');
        });
    }

    async encryptDatabase(password) {
        await sqlcipher.encrypt(this.db, password);
    }

    async decryptDatabase(password) {
        await sqlcipher.decrypt(this.db, password);
    }

    close() {
        this.db.close((err) => {
            if (err) {
                console.error(err.message);
            }
            console.log('Closed the database connection.');
        });
    }
}

module.exports = SecureDB;
4. 测试与验证

最后,我们可以编写一个简单的测试脚本来验证加密和解密功能:

const SecureDB = require('./sqlite3-ext');

async function testEncryption() {
    const db = new SecureDB(':memory:');
    const password = 'mysecret';

    await db.encryptDatabase(password);
    console.log('Database encrypted.');

    await db.decryptDatabase(password);
    console.log('Database decrypted.');

    db.close();
}

testEncryption().catch(console.error);

结论

通过上述步骤,我们成功地为 SQLite3 添加了加密和解密功能,并创建了一个 Node.js 扩展模块来调用这些功能。这使得我们在 Node.js 应用中能够安全地存储和访问敏感数据。如果您有任何问题或需要进一步的帮助,请随时留言。


楼主用什么编译的?GCC么 ?怎么支持多种平台 ?

我最近用gyp 编译成功了

为了实现一个支持加密和解密功能的 SQLite3 模块,并且让其支持 Node.js 调用,我们需要从源码开始编译一个带有加密功能的 SQLite3 库。SQLite3 自身不直接提供加密功能,但可以通过添加扩展来实现。这里我们使用 SQLCipher,它是一个基于 SQLite 的库,提供了端到端的加密。

步骤 1: 安装依赖

确保安装了必要的开发工具:

sudo apt-get install build-essential libssl-dev

步骤 2: 获取源码并编译

获取 SQLCipher 源码:

git clone https://github.com/sqlcipher/sqlcipher.git
cd sqlcipher

编译 SQLCipher:

./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="-lcrypto"
make

步骤 3: 编写 Node.js 扩展(可选)

如果你想为 Node.js 编写一个绑定模块来调用 SQLCipher,你可以使用 node-addon-api 或者 nan。这需要一些 C++ 编程基础。

示例代码 (使用 node-addon-api)

首先,创建一个文件夹作为项目根目录,然后初始化 npm 项目:

npm init -y
npm install node-addon-api

创建 binding.cc 文件:

#include <napi.h>
#include "sqlcipher/sqlite3.h"

Napi::String HelloWorld(const Napi::CallbackInfo& info) {
  sqlite3 *db;
  int rc = sqlite3_open("test.db", &db);
  
  if (rc) {
    return Napi::String::New(info.Env(), "Can't open database");
  }
  
  // 在这里执行 SQL 操作
  
  sqlite3_close(db);
  return Napi::String::New(info.Env(), "Hello World!");
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
  exports.Set(Napi::String::New(env, "hello"), Napi::Function::New(env, HelloWorld));
  return exports;
}

NODE_API_MODULE(addon, Init)

编译扩展:

npm install --build-from-source

在 Node.js 中加载扩展:

const addon = require('./build/Release/addon');
console.log(addon.hello());

注意,实际应用中你需要处理更多细节,比如错误处理、SQLCipher 初始化等。

结论

以上步骤展示了如何从零开始构建一个支持加密的 SQLite3 模块。对于更复杂的应用场景,建议参考官方文档或现有的开源项目,如 node-sqlcipher,这可能更适合你的需求。

回到顶部