mutecomm / go-sqlcipher

Self-contained Go sqlite3 driver with an AES-256 encrypted sqlite3 database
Other
167 stars 62 forks source link

How can I specify the key and other options, ie:`cipher_compatibility` #19

Closed huhuang03 closed 3 years ago

huhuang03 commented 3 years ago

I have a password (hex, 64 len). And I have some other options. For now, I don't know how to specify those option. I have tried three ways. But none of those works, the error is file is not a database.

Tried 1:

param_str := "_pragma_cipher_page_size=4096&_cipher_compatibility=3&_cipher_page_size=4096&_synchronous=NORMAL&_count_changes=OFF&_auto_vacuum=0&_journal_mode=WAL"
dbname := fmt.Sprintf("%s?_pragma_key=x'%s'&%s", dbPath, dbPwd, param_str)
db, err := sql.Open("sqlcipher", dbname)

Tried 2:

sql.Register("sqlcipher", &sqlcipher.SQLiteDriver{
            ConnectHook: func(conn *sqlcipher.SQLiteConn) error {
            //  fmt.Println("sqlcipher PRAGMA called")
                conn.Exec("PRAGMA cipher_compatibility = 3", nil)
                conn.Exec("PRAGMA cipher_page_size = 4096", nil)
                conn.Exec("PRAGMA synchronous=NORMAL", nil)
                conn.Exec("PRAGMA count_changes=OFF", nil)
                conn.Exec("PRAGMA auto_vacuum=0", nil)
                conn.Exec("PRAGMA journal_mode = WAL", nil)
                return nil
            },
        })
db, err := sql.Open("sqlcipher", dbname)

Tried 3:

db, err := sql.Open("sqlcipher", dbname)

if err != nil {
    log.Panic(err.Error())
}

db.Exec("PRAGMA cipher_compatibility=3")
db.Exec("PRAGMA cipher_page_size=4096")
db.Exec("PRAGMA synchronous=NORMAL")
db.Exec("PRAGMA count_changes=OFF")
db.Exec("PRAGMA auto_vacuum=0")
db.Exec("PRAGMA journal_mode=WAL")

What I want is specify the key and those pragma. What is the right way. Thank you!

huhuang03 commented 3 years ago

After read some source, I figured out the exec flow.

hazcod commented 3 years ago

@huhuang03 can you please share your solution?