一、背景描述:
当在一些小的项目中,需要用到数据库时,通常使用C语言库SQLite。但是它不提供加密功能,使项目产品的安全性存在风险。
更多倾向于选择SQLCipher,它扩展了SQLite数据库库,增加了安全性增强功能。
二、安装SQLCipher:
1.从Github下载最新源码:
git clone https://github.com/sqlcipher/sqlcipher.git
2.根据Github上编译说明,进行配置和编译:
Building SQLCipher is almost the same as compiling a regular version of SQLite with two small exceptions:
- You must define SQLITE_HAS_CODEC and SQLITE_TEMP_STORE=2 when building sqlcipher.
- If compiling against the default OpenSSL crypto provider, you will need to link libcrypto.
Note in this example, --enable-tempstore=yes
is setting SQLITE_TEMP_STORE=2
for the build.
同时使用动态链接库libcrypto,配置命令如下:
$ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" \
LDFLAGS="-lcrypto"
$ make install
3.在配置和编译过程,出现一些错误,进行如下过程可能解决安装问题:
- 切换到root用户:$ yum search sqlite
- 安装找到的,sqlite-devel.x86_64,sqlite-tcl.x86_64和sqlite.x86_64
- 移除SQLCipher源码文件,重新下载配置编译。
4.安装完成后,我们可以看到头文件和库文件已经放置在在对应目录中:
$ ll /usr/local/include/sqlcipher/
-rw-r--r--. 1 root root 33713 May 1 22:41 sqlite3ext.h
-rw-r--r--. 1 root root 558230 May 1 22:41 sqlite3.h
$ ll /usr/local/lib/
-rw-r--r--. 1 root root 1212694 May 1 22:41 libsqlcipher.a
-rwxr-xr-x. 1 root root 979 May 1 22:41 libsqlcipher.la
lrwxrwxrwx. 1 root root 21 May 1 22:41 libsqlcipher.so -> libsqlcipher.so.0.8.6
lrwxrwxrwx. 1 root root 21 May 1 22:41 libsqlcipher.so.0 -> libsqlcipher.so.0.8.6
-rwxr-xr-x. 1 root root 1074184 May 1 22:41 libsqlcipher.so.0.8.6
三、使用SQLCipher:
1.本文以拷贝的方式,将上述目录下的头文件sqlite3.h和sqlite3ext.h添加到C++项目中。
2.同样以拷贝的方式,将libsqlcipher.a静态库拷贝到C++项目自定义的库目录下。
3.编译C++项目时,需要加入CFLAGS="-DSQLITE_HAS_CODEC",这一点其实与SQLCipher中的编译说明相同。加入原因是,SQLCipher扩展的API接口,如sqlite3_key(...),需要该定义。
4.同样的,需要加入LDFLAGS="-lcrypto",让其动态链接库libcrypto。
5. 使用静态链接的方式,将libsqlcipher.a库链接进项目的生成文件中,即将C++项目自定义的库目录加入编译选项中,并说明库名称LDFLAGS="-lsqlcipher"。