Skip to content

port sqlcipher to go-sqlite3 #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*.dll
*.o

.idea

# VSCode
.vscode

Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Supported Golang version:
- [Usage](#usage)
- [Extensions](#extensions)
- [Spatialite](#spatialite)
- [Encrypted Storage](#encrypted-storage)
- [FAQ](#faq)
- [License](#license)

Expand Down Expand Up @@ -154,6 +155,7 @@ go build --tags "icu json1 fts5 secure_delete"
| Secure Delete (FAST) | sqlite_secure_delete_fast | For more information see [PRAGMA secure_delete](https://www.sqlite.org/pragma.html#pragma_secure_delete) |
| Tracing / Debug | sqlite_trace | Activate trace functions |
| User Authentication | sqlite_userauth | SQLite User Authentication see [User Authentication](#user-authentication) for more information. |
| Encrypted Storage | sqlite_encrypt | SQLite with Encrypted Storage see [Encrypted Storage](#encrypted-storage) for more information. |

# Compilation

Expand Down Expand Up @@ -321,6 +323,8 @@ This package supports the SQLite User Authentication module.

To use the User authentication module the package has to be compiled with the tag `sqlite_userauth`. See [Features](#features).

To use Encrypted Storage module the package has to be compiled with the tag `sqlite_encrypt`. See [Features](#features).

## Usage

### Create protected database
Expand Down Expand Up @@ -433,6 +437,22 @@ If you want your own extension to be listed here or you want to add a reference
Spatialite is available as an extension to SQLite, and can be used in combination with this repository.
For an example see [shaxbee/go-spatialite](https://github.com/shaxbee/go-spatialite).

## Encrypted Storage

The Encrypted Storage extension will be builtin by compiling with the tag `sqlite_encrypt`
you have 2 ways to enable cipher:

- DSN

> You can try DSN like this, `file:foo.db?_crypto_key=auxten`

- PRAGMA

> After Open db, before execute any statement do `db.Exec("PRAGMA key = auxten;")`

See also: https://github.com/sqlcipher/sqlcipher#encrypting-a-database


# FAQ

- Getting insert error while query is opened.
Expand Down
3 changes: 3 additions & 0 deletions _example/encrypt/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

CGO_ENABLED=1 go build --tags "sqlite_encrypt"
112 changes: 112 additions & 0 deletions _example/encrypt/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright (C) 2018 CovenantSQL <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package main

import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"log"
"os"
)

func main() {
os.Remove("./foo.db")

db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()

sqlStmt := `
PRAGMA key = auxten;
create table foo (id integer not null primary key, name text);
delete from foo;
`
_, err = db.Exec(sqlStmt)
if err != nil {
log.Printf("%q: %s\n", err, sqlStmt)
return
}

tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
stmt, err := tx.Prepare("insert into foo(id, name) values(?, ?)")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
for i := 0; i < 100; i++ {
_, err = stmt.Exec(i, fmt.Sprintf("こんにちわ世界%03d", i))
if err != nil {
log.Fatal(err)
}
}
tx.Commit()

rows, err := db.Query("select id, name from foo")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var id int
var name string
err = rows.Scan(&id, &name)
if err != nil {
log.Fatal(err)
}
fmt.Println(id, name)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}

stmt, err = db.Prepare("select name from foo where id = ?")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
var name string
err = stmt.QueryRow("3").Scan(&name)
if err != nil {
log.Fatal(err)
}
fmt.Println(name)

_, err = db.Exec("delete from foo")
if err != nil {
log.Fatal(err)
}

_, err = db.Exec("insert into foo(id, name) values(1, 'foo'), (2, 'bar'), (3, 'baz')")
if err != nil {
log.Fatal(err)
}

rows, err = db.Query("select id, name from foo")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var id int
var name string
err = rows.Scan(&id, &name)
if err != nil {
log.Fatal(err)
}
fmt.Println(id, name)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
}
Loading