From d681773ef093d93acf034e4a8ca3b2d928676f45 Mon Sep 17 00:00:00 2001 From: Vladimir Kasatkin Date: Mon, 27 Jul 2020 11:03:46 +0300 Subject: [PATCH] #20 Make PRAGMA key to quotes (#21) In SQLCipher it's allowed to use different keys (not only alphanumeric). https://www.zetetic.net/sqlcipher/sqlcipher-api/#PRAGMA_key Co-authored-by: Vladimir.Kasatkin --- sqlite3.go | 2 +- sqlite3_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sqlite3.go b/sqlite3.go index 109a0cc..363a44a 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -1400,7 +1400,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) { // Encrypt Keys // The key pragma should be always called first if encryptKey != "" { - if err := exec(fmt.Sprintf("PRAGMA key = %s;", encryptKey)); err != nil { + if err := exec(fmt.Sprintf(`PRAGMA key = "%s";`, encryptKey)); err != nil { C.sqlite3_close_v2(db) return nil, err } diff --git a/sqlite3_test.go b/sqlite3_test.go index b184fc4..708b396 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -1207,7 +1207,7 @@ func TestEncryptoDatabase(t *testing.T) { defer os.Remove(tempFilename) defer db.Close() - _, err = db.Exec("PRAGMA key = 'password';") + _, err = db.Exec(`PRAGMA key = "x'123ABC'";`) if err != nil { t.Error("Failed to set encrypto key") } @@ -1239,7 +1239,7 @@ func TestEncryptoDatabase(t *testing.T) { t.Error("Failed to db.QueryRow: not matched results") } db.Close() - db, err = sql.Open("sqlite3", tempFilename+"?_key=not_password") + db, err = sql.Open("sqlite3", tempFilename+"?_key=x'123000'") if err != nil { t.Fatal("Failed to open database:", err) } @@ -1249,7 +1249,7 @@ func TestEncryptoDatabase(t *testing.T) { t.Error("Failed to encrypto database") } db.Close() - db, err = sql.Open("sqlite3", tempFilename+"?_key=password") + db, err = sql.Open("sqlite3", tempFilename+"?_key=x'123ABC'") if err != nil { t.Fatal("Failed to open database:", err) }