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

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

7 回复

Node.js 实现 SQLite3 带加密解密功能

背景

在 Node.js 中,默认的 sqlite3 模块不支持加密和解密功能。为了实现这一需求,我们需要重新编译带有加密功能的 SQLite3 库,并将其集成到 Node.js 项目中。

步骤概述

  1. 下载并安装 OpenSSL。
  2. 下载并修改 SQLite3 源码以支持加密功能。
  3. 编译 SQLite3 库。
  4. 创建一个 C++ 扩展模块,用于与 Node.js 进行交互。
  5. 在 Node.js 中使用该扩展模块。

详细步骤

1. 安装 OpenSSL

确保你已经安装了 OpenSSL。如果没有,可以通过以下命令安装:

sudo apt-get install libssl-dev
2. 修改 SQLite3 源码

从 SQLite 官方网站下载 SQLite3 源码,并修改源码以支持加密功能。这里我们使用 SQLCipher 作为加密库。

git clone https://github.com/sqlitebrowser/sqlite.git
cd sqlite
git checkout tags/3.35.5
git submodule update --init
3. 编译 SQLite3 库

编译 SQLite3 库,使其支持加密功能:

./configure --enable-tcl=false --disable-readline --disable-editline --disable-amalgamation --with-crypto-lib=sqlcipher
make
4. 创建 C++ 扩展模块

创建一个 C++ 扩展模块,用于与 Node.js 进行交互。以下是一个简单的示例:

#include <node.h>
#include <node_object_wrap.h>
#include <sqlite3.h>
#include <sqlcipher/sqlite3.h>

namespace demo {

using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;

void InitAll(Local<Object> exports) {
  NODE_SET_METHOD(exports, "openDatabase", OpenDatabase);
}

void OpenDatabase(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();
  if (args.Length() < 2 || !args[0]->IsString() || !args[1]->IsString()) {
    isolate->ThrowException(Exception::TypeError(
        String::NewFromUtf8(isolate, "Wrong arguments")));
    return;
  }

  std::string filename = *v8::String::Utf8Value(args[0]);
  std::string password = *v8::String::Utf8Value(args[1]);

  sqlite3* db;
  int rc = sqlite3_open_v2(filename.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);

  if (rc != SQLITE_OK) {
    args.GetReturnValue().Set(String::NewFromUtf8(isolate, sqlite3_errmsg(db)));
    sqlite3_close(db);
    return;
  }

  rc = sqlite3_key(db, password.c_str(), password.size());
  if (rc != SQLITE_OK) {
    args.GetReturnValue().Set(String::NewFromUtf8(isolate, sqlite3_errmsg(db)));
    sqlite3_close(db);
    return;
  }

  args.GetReturnValue().Set(String::NewFromUtf8(isolate, "Database opened successfully"));
}

NODE_MODULE(addon, InitAll)

}  // namespace demo
5. 在 Node.js 中使用扩展模块

在 Node.js 中使用上述 C++ 扩展模块:

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

addon.openDatabase('example.db', 'mysecretkey')
  .then(result => console.log(result))
  .catch(err => console.error(err));

通过以上步骤,我们可以为 Node.js 提供一个支持加密和解密功能的 SQLite3 模块。


aguai2008@qq.com 请发一份,请附上说明

同求发一份,先谢谢楼主 zycbob@163.com

过几天吧,现在有点忙,

楼主,我也需要一份谢谢,2621872@qq.com

怎么不放到github上面。。。。

针对您的需求,可以通过修改 sqlite3 源码并重新编译来实现加密和解密功能,然后将其与 Node.js 集成。这里以 SQLCipher(一个开源的 SQLite 加密扩展)为例,展示如何完成这一过程。

步骤概述

  1. 安装 SQLCipher 和依赖

    • 安装 SQLCipher 需要一些系统依赖项,如 OpenSSL 和 sqlite3 开发库。
  2. 下载并修改 sqlite3 源码

    • 从 SQLite 官方网站下载源码。
    • 将源码与 SQLCipher 结合,确保源码支持加密功能。
  3. 编译带有加密功能的 sqlite3

    • 使用 SQLCipher 的编译脚本重新编译 sqlite3。
  4. 创建 Node.js 绑定

    • 使用 Node.js 的 node-gyp 或类似工具为新的 sqlite3 编写绑定,使其可以在 Node.js 中使用。
  5. 测试

    • 创建一个简单的 Node.js 脚本,验证加密和解密功能是否正常工作。

示例代码

编写 Node.js 绑定

// sqlite3_with_encryption.js
const sqlite3 = require('sqlite3').verbose();

let db = new sqlite3.Database(':memory:', (err) => {
    if (err) {
        return console.error(err.message);
    }
    console.log('Connected to the in-memory SQlite database.');
});

db.serialize(() => {
    db.run("CREATE TABLE lorem (info TEXT)", [], function(err) {
        if (err) {
            console.error(err.message);
        } else {
            console.log("Table created.");
        }
    });
});

db.close();

注意:上述代码假设您已经成功编译了一个带有加密功能的 sqlite3,并且该 sqlite3 可执行文件或库位于您的 Node.js 环境中可访问的位置。

解释

  • 这个 Node.js 脚本简单地连接到一个内存中的 SQLite 数据库,创建一个表,并插入数据。
  • 实际应用中,您需要确保编译后的 sqlite3 支持 SQLCipher 的加密功能。
  • 您可能还需要修改 Node.js 的 sqlite3 模块以正确处理加密操作,例如通过传递额外的参数给数据库连接。

总结

这只是一个基本框架,实际操作时可能需要更多细节和调整。重要的是要理解整个流程,包括从 SQLCipher 获取必要的加密功能,编译和集成到 Node.js 环境中。希望这些信息对您有所帮助!

回到顶部