当前位置:   article > 正文

electron 集成 SQLCipher

electron使用sqlcipher加密

mac

安装 brew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

安装 sqlcipher

  1. brew options sqlcipher
  2. brew install sqlcipher

这部分不知道有没有起作用

  1. export LDFLAGS="-L/usr/local/lib"
  2. export CPPFLAGS="-I/usr/local/include -I/usr/local/include/sqlcipher"
  3. export CXXFLAGS="$CPPFLAGS"

克隆 sqlcipher 源码,并链接

  1. git clone https://github.com/sqlcipher/sqlcipher.git
  2. pushd sqlcipher
  3. make clean
  4. ./configure --enable-tempstore=yes --enable-load-extension --disable-tcl --with-crypto-lib=commoncrypto CFLAGS="-DSQLITE_HAS_CODEC -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS5" LDFLAGS="-framework Security -framework Foundation"
  5. make

成功后该文件夹下出现 .libs 隐藏文件夹

创建 custom-binding.gyp 文件

  1. {
  2. "includes": [ "deps/common-sqlite.gypi" ],
  3. "variables": {
  4. },
  5. "targets": [
  6. {
  7. "target_name": "<(module_name)",
  8. "include_dirs": [
  9. "<!(node -e \"require('nan')\")",
  10. "/Users/jinghongqiu/Desktop/sqlcipher/"
  11. ],
  12. "libraries": [
  13. "/Users/jinghongqiu/Desktop/sqlcipher/.libs/libsqlcipher.a"
  14. ],
  15. "sources": [
  16. "src/database.cc",
  17. "src/node_sqlite3.cc",
  18. "src/statement.cc"
  19. ]
  20. },
  21. {
  22. "target_name": "action_after_build",
  23. "type": "none",
  24. "dependencies": [ "<(module_name)" ],
  25. "copies": [
  26. {
  27. "files": [ "<(PRODUCT_DIR)/<(module_name).node" ],
  28. "destination": "<(module_path)"
  29. }
  30. ]
  31. }
  32. ]
  33. }

/Users/jinghongqiu/Desktop/sqlcipher 需要替换成 sqlcipher 克隆下来的路径

electron 项目中执行命令

  1. echo "Copying custom binding.gyp (node-sqlite3)"
  2. cp custom-binding.gyp node_modules/sqlite3/binding.gyp
  3. echo "Rebuilding node-sqlite3 bindings with statically linked SQLCipher (libsqlcipher.a)"
  4. npm rebuild sqlite3 --build-from-source --static_linking_path=/Users/jinghongqiu/Desktop/sqlcipher/.libs/libsqlcipher.a --runtime=electron --target=4.0.8 --dist-url=https://atom.io/download/electron

/Users/jinghongqiu/Desktop/sqlcipher 需要替换成 sqlcipher 克隆下来的路径
--target=4.0.8 中的数字为 electron 的版本

使用

  1. // 在 electron 里才能使用
  2. var sqlite3 = require('sqlite3')
  3. var db = new sqlite3.Database('./test.sqlcipher')
  4. db.serialize(() => {
  5. db.run("PRAGMA KEY = 'secret'")
  6. db.run("CREATE TABLE messages(id INTEGER, user VARCHAR, msg TEXT)")
  7. db.run("INSERT INTO messages(id, user, msg) VALUES (1, 'coolaj86', 'this is test message number one')")
  8. db.get("SELECT * FROM messages", (err, data) => {
  9. if (err) {
  10. console.error(err)
  11. return
  12. }
  13. console.log(data)
  14. })
  15. })

用可视化工具打开 test.sqlcipher ,如果需要输入密码则集成成功。输入密码 secret,如果无法打开,则有可能是可视化工具问题(如 SQLiteManager 正确密码也无法打开)

参考

Building SQLCipher for node.js on Raspberry Pi 2
Setting up SQLCipher with node-sqlite3 and Electron
在 Node.js 中使用 SQLCipher

转载于:https://www.cnblogs.com/NKnife/p/10515662.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/492408
推荐阅读
相关标签
  

闽ICP备14008679号