暗号化APIを有効にしたSQLite3のTclバインディングをコンパイルする

コンパイルに使用したもの
ActiveTcl 8.4.19.5 http://www.activestate.com/activetcl/downloads
wxSQLite3(wxsqlite3-2.1.2.zip) http://sourceforge.net/projects/wxcode/files/Components/wxSQLite3/
SQLite3ソースコード(sqlite-autoconf-3070701.tar.gz) http://www.sqlite.org/download.html
MinGW(mingw-get-inst-20110530.exe) http://sourceforge.net/projects/mingw/files/Automated%20MinGW%20Installer/mingw-get-inst/

作業フォルダはどこでもいいですが、今回はc:\srcとします。
そこにSQLite3ソースコードとwxSQLite3を展開します。
こんな感じ。

c:\src
├─sqlite-autoconf-3070701
│ └─tea
│ ├─doc
│ ├─generic
│ ├─tclconfig
│ └─win
└─wxsqlite3-2.1.2
├─build
├─build29
├─dbadmin
│ └─images
├─docs
│ └─html
├─include
│ └─wx
├─lib
├─samples
├─sqlite3
│ ├─include
│ ├─lib
│ └─secure
│ ├─aes128
│ ├─aes256
│ └─src
│ ├─codec
│ └─codec-c
├─src
└─website

以下のフォルダ内の全てのファイルを
C:\src\wxsqlite3-2.1.2\sqlite3\secure\src\codec-c\

ここに上書きコピーします。
C:\src\sqlite-autoconf-3070701\tea\generic\

そして、以下のソースコードをテキストエディタで開きます。
C:\src\sqlite-autoconf-3070701\tea\generic\tclsqlite3.c

4行目の”sqlite3.c”を

#ifdef USE_SYSTEM_SQLITE
# include
#else
#include "sqlite3.c"
#endif

“sqlite3secure.c”に書き換える。


#ifdef USE_SYSTEM_SQLITE
# include
#else
#include "sqlite3secure.c"
#endif

これで、コンパイルの準備はできました。

さて、MinGW shellを起動しましょう。

ディレクトリを移動してコンパイルします。

cd /c/src/sqlite-autoconf-3070701/tea
$ ./configure CFLAGS="-DSQLITE_HAS_CODEC"
$ make

これで以下のファイルができました。これ単体でTclのパッケージです。
C:\src\sqlite-autoconf-3070701\tea\sqlite3771.dll

さて、テストしてみましょう。

load sqlite3771.dll Sqlite3

# create plain database file
sqlite3 pdb plain.db

# create encrypted database file
sqlite3 sdb secret.db -key password

# SQL test script
# 1. create table
# 2. populate test data
# 3. execute query
set sql {
create table users (id integer primary key autoincrement not null, name text, age integer);
insert into users (name, age) values ("山田太郎", 30);
select * from users;
}

pdb eval $sql
#=> 1 山田太郎 30
pdb close

sdb eval $sql
#=> 1 山田太郎 30
sdb close

# Re-open plain.db
sqlite3 pdb plain.db
pdb eval {
select * from users;
}
#=> 1 山田太郎 30
pdb close

# Re-open secret.db without a key
sqlite3 sdb secret.db
sdb eval {
select * from users;
}
#=> file is encrypted or is not a database
sdb close

既存のデータベースファイルを暗号化するには、rekeyするか、dumpを取得して
暗号化した新規データベースでrestoreすればOKです。
この作業には、wxSQLite3に付属するコンパイル済みのshellを使用してください。
C:\src\wxsqlite3-2.1.2\sqlite3\secure\aes256\sqlite3shell.exe

最後はちょっとはしょった説明になりましたが、できてしまえば結構簡単です。
tclsqliteの暗号化APIについてのドキュメントはないのですが、ネイティブの関数と基本的に変わりはないと思います。
間違ったコマンドを与えてやるとエラーメッセージに関数リストが出てきたり、
関数に間違った引数を与えてやることで使い方が出てきたりするので、いろいろ試してみるとよいと思います。


(tea) 1 % load sqlite3771.dll Sqlite3
(tea) 2 % sqlite3 sdb secret.db -key password
(tea) 3 % sdb aaaa
bad option "aaaa": must be authorizer, backup, busy, cache, changes, close, collate, collation_needed, commit_hook, complete, copy, enable_load_extension, errorcode, eval, exists, function, incrblob, interrupt, last_insert_rowid, nullvalue, onecolumn, profile, progress, rekey, restore, rollback_hook, status, timeout, total_changes, trace, transaction, unlock_notify, update_hook, version, or wal_hook

投稿日: 7月 7, 2011 カテゴリー: Build, Database, Tcl/Tk タグ: , , | パーマリンク コメントする.

コメントを残す