使用Go和Gorm实现读取SQLCipher加密数据库

 更新时间:2024年06月14日 10:06:01   作者:非晓为骁  
本文档主要描述通过Go和Gorm实现生成和读取SQLCipher加密数据库以及其中踩的一些坑,文章通过代码示例讲解的非常详细, 对大家的学习或工作有一定的帮助,需要的朋友可以参考下

本文档主要描述通过 https://github.com/mutecomm/go-sqlcipher 生成和读取 SQLCipher 加密数据库以及其中踩的一些坑

软件版本

go: v1.22.2

sqlcipher cli(ubuntun):3.15.2

sqlcipher(used for encrypt):v3

go-sqlcipher-package:https://github.com/mutecomm/go-sqlcipher v0.0.0-20190227152316-55dbde17881f

Go 生成和读取 SQLCipher 数据库

生成数据库

创建一个名为 encrypt-data.db,表为 test 的数据库

import _ "github.com/mutecomm/go-sqlcipher"
func NewSQLCipherDB() {
    var (
		db      *sql.DB
		testDir = "go-sqlcipher_test"
		tables  = `CREATE TABLE test (id INTEGER PRIMARY KEY, data TEXT);`
		data    = `INSERT INTO test (data) VALUES ('Hello, World!');`
	)

	// create DB
	key := "passphrase"
	tmpdir, err := os.MkdirTemp("", testDir)
	if err != nil {
		panic(err)
	}
	dbname := filepath.Join(tmpdir, "encrypt-data.db")
	dbnameWithDSN := dbname + fmt.Sprintf("?_pragma_key=%s", key)

	if db, err = sql.Open("sqlite3", dbnameWithDSN); err != nil {
		panic(err)
	}
    
    defer db.Close()

	if _, err = db.Exec(tables); err != nil {
		panic(err)
	}

	if _, err = db.Exec(data); err != nil {
		panic(err)
	}
    
	return
}

判断数据库是否加密

import sqlite3 "github.com/mutecomm/go-sqlcipher"

func IsSQLCipherEncrypted(dbName string) {
    // make sure DB is encrypted
	encrypted, err := sqlite3.IsEncrypted(dbName)
	if err != nil {
		panic(err)
	}
	if !encrypted {
		panic(errors.New("go-sqlcipher: DB not encrypted"))
	}
    
    fmt.Println("encrypted")
} 

读取数据库

import _ "github.com/mutecomm/go-sqlcipher"
func QuerySQLCipherDB(dbPath,key string) {
    var (
		db  *sql.DB
        err error
	)

	dbnameWithDSN := dbPath + fmt.Sprintf("?_pragma_key=%s", key)

	// open DB for testing
	db, err = sql.Open("sqlite3", dbnameWithDSN)
	if err != nil {
		panic(err)
	}
	_, err = db.Exec("SELECT count(*) FROM test;")
	if err != nil {
		panic(err)
	}
    
	return
}

如果密码错误或者是数据库错误,Line 15 会报 err

Gorm 连接 SQLCipher 数据库

用原生方式读取肯定不方便,所以还是找了一下如何用 gorm 来连接并读取。其实这个 go-sqlcipher 就是一个驱动,所以跟 gorm 读取 mysql 数据库是差不多的。就是要注意把 “github.com/mutecomm/go-sqlcipher” import 进去。

import 	_ "github.com/mutecomm/go-sqlcipher"

var (
	db *gorm.DB
)

func Init(dbPath string) (err error) {
    key := "passphrase"
	dbPath = fmt.Sprintf(dbPath+"?_pragma_key=%s", key)

	db, err = gorm.Open("sqlite3", dbPath)
	if err != nil {
		return err
	}

	// logger Open
	db.LogMode(true)
	// Set Idle
	db.DB().SetMaxIdleConns(10)
    
    return nil
}

可视化工具读取 SQLCipher 加密数据库(1)

本篇下面的描述内容主要是,因为创建加密数据库参数出入,而要去修改可视化工具的一些参数,具体见下文。

踩坑 & 分析

上述的方式都是基础的,也正常是应该这么创建以及读取的,但是我接手到的代码是长下面这样子的。

key := "passphrase"
dbPath = fmt.Sprintf(dbPath+"?_pragma_key=x'%s'&_pragma_cipher_page_size=4096", key)

db, err = gorm.Open("sqlite3", dbPath)
if err != nil {
	return err
}

奇奇怪怪的事情就开始发生了,用最基础的 sqlcipher 指令读取都会说密码错误。

sqlcipher 密码错误

sqlite> PRAGMA key = x'passphrase'; # 格式错误
sqlite> PRAGMA key = '70617373706872617365'; # passphrase hex 之后,密码错误
sqlite> PRAGMA key = '78277061737370687261736527'; # x'passphrase' hex 之后,密码错误
sqlite> PRAGMA key = "x'passphrase'";

先透露,第四个才是对的

按正常情况来看,应该这样就可以正常读取了,还是报密码错误。

db browser 密码错误

之前没碰过这个,觉得 sqlcipher 是不是我不会,所以找了这个工具。

不过按照流程输入密码,也还是进不去,也选择了 SQLCipher 3 也不行。

这边 algorithm 跟源码 README 里面的 AES 256 对不上,我以为是 db browser 不支持我这种加密格式

跑单测

按照别人给的不行,就从头开始,自己创建,自己测试。

  • go 代码创建加密数据库,sqlcipher 指令读取,这个是可以的。这一个测试我用的是最上面生成数据库的代码。
  • 因为我收到的代码里面有带,_pragma_cipher_page_size=4096。然后用这个方式创建的就是不行,以为我输入的 key 是不是在第三方包内有做什么动作,所以去分析了源码库。

跑完单元测试,说明密码的输入没错,就是这个 page size 的问题。

此时我还没意识到是 page size 默认配置的问题

查源码

以下源码的 README,看得我迷糊,以为还要再 hex,多测了不同的加密方式也不行。

To create and open encrypted database files use the following DSN parameters:

key := "2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99"
dbname := fmt.Sprintf("db?_pragma_key=x'%s'&_pragma_cipher_page_size=4096", key)
db, _ := sql.Open("sqlite3", dbname)

_pragma_key is the hex encoded 32 byte key (must be 64 characters long). _pragma_cipher_page_size is the page size of the encrypted database (set if you want a different value than the default size).

key := url.QueryEscape("secret")
dbname := fmt.Sprintf("db?_pragma_key=%s&_pragma_cipher_page_size=4096", key)
db, _ := sql.Open("sqlite3", dbname)

This uses a passphrase directly as _pragma_key with the key derivation function in SQLCipher. Do not forget the url.QueryEscape() call in your code!

找 ISSUE

https://github.com/mutecomm/go-sqlcipher/issues/15

这个 issue 是对 SQLCipher V4 的,里面有这么一段:

The parameters seem to be the same. I'm wondering if you have to switch the order of key and cipher_page_size in the sqlcipher call. Also the documentation https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_default_page_size seems to indicate that you have to use cipher_default_page_size in the command line call. But it shouldn't make any difference anyway since 4096 is the default value in SQLCipher 4.

说明 SQLCipher 的 cipher_page_size 有默认值,并且在调用 sqlcipher 加密的时候,会受影响。所以,在可视化页面连接的时候要指定。

回看代码

dbname := fmt.Sprintf("db?_pragma_key=x'%s'&_pragma_cipher_page_size=4096", key)

这个库只支持 SQLCipher v3,v4 的默认值才是 4096,v3的默认值是1024(虽然我不知道这个什么用)

各个可视化工具默认都是 1024,跟代码里面 4096 对不上,改参数

改参数

sqlcipher

sqlite> PRAGMA key = "x'passphrase'";
sqlite> PRAGMA cipher_page_size=4096;
sqlite> SELECT * from test;
1|Hello, World!
sqlite> .exit

db browser

先选 SQLCipher 3, 然后选择 Custom,再点击 Page size 的下拉选择 4096,就可以了

DBeaver

修改 legacy_page_size 为 4096 就可以了

总结

其实这个懂的人,估计看到这个 page size 不同就知道要去配置了。对于不懂的人,看密码,又登入不进去就会很烦,就会乱。

后面分析的方法就是从单测入手,用最简单的方式先跑通一个。比如,密码先不要设置那么复杂的,就设置 123456,然后测试。通过再往下一步,往自己收到的问题去靠。

以上就是使用Go和Gorm实现读取SQLCipher加密数据库的详细内容,更多关于Go Gorm读取SQLCipher的资料请关注脚本之家其它相关文章!

相关文章

  • go语言中的Carbon库时间处理技巧

    go语言中的Carbon库时间处理技巧

    这篇文章主要介绍了go语言中的Carbon库时间处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-02-02
  • Go语言学习笔记之错误和异常详解

    Go语言学习笔记之错误和异常详解

    Go语言采用返回值的形式来返回错误,这一机制既可以让开发者真正理解错误处理的含义,也可以大大降低程序的复杂度,下面这篇文章主要给大家介绍了关于Go语言学习笔记之错误和异常的相关资料,需要的朋友可以参考下
    2022-07-07
  • Go语言中defer使用的陷阱小结

    Go语言中defer使用的陷阱小结

    本文主要介绍了Go语言中defer使用的陷阱小结,分别是defer语句不可以在return语句之后,defer语句执行的匿名函数,匿名函数的参数会被预先处理,具有一定的参考价值,感兴趣的可以了解一下
    2024-01-01
  • Go的gin参数校验中的validator库详解

    Go的gin参数校验中的validator库详解

    这篇文章主要介绍了Go的gin参数校验之validator库,使用 validator 以后,只需要在定义结构体时使用 binding 或 validate tag标识相关校验规则,就可以进行参数校验了,而不用自己单独去写常见的校验规则,需要的朋友可以参考下
    2023-08-08
  • 深入了解Golang的指针用法

    深入了解Golang的指针用法

    与C语言一样,Go语言中同样有指针,通过指针,我们可以只传递变量的内存地址,而不是传递整个变量,这在一定程度上可以节省内存的占用。本文将通过示例详细讲讲Golang的指针用法,需要的可以参考一下
    2022-07-07
  • 关于golang中map使用的几点注意事项总结(强烈推荐!)

    关于golang中map使用的几点注意事项总结(强烈推荐!)

    map是一种无序的基于key-value的数据结构,Go语言中的map是引用类型,必须初始化才能使用,下面这篇文章主要给大家介绍了关于golang中map使用的几点注意事项,需要的朋友可以参考下
    2023-01-01
  • go流程控制代码详解

    go流程控制代码详解

    这篇文章主要介绍了go流程控制,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下
    2019-05-05
  • Go语言开发快速学习CGO编程

    Go语言开发快速学习CGO编程

    这篇文章主要为大家介绍了Go语言开发之快速学习CGO编程,看了本文你就会发现CGO编程其实没有想象的那么难,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-04-04
  • Golang 并发读写锁的具体实现

    Golang 并发读写锁的具体实现

    Go语言中的sync.RWMutex提供了读写锁机制,允许多个协程并发读取共享资源,但在写操作时保持独占性,本文主要介绍了Golang 并发读写锁的具体实现,感兴趣的可以了解一下
    2025-02-02
  • 浅谈go语言中别名类型的使用

    浅谈go语言中别名类型的使用

    类型别名是 Go 1.9 版本添加的新功能,主要用于解决代码升级、迁移中存在的类型兼容性问题,本文主要介绍了go语言中别名类型的使用,感兴趣的可以了解一下
    2024-01-01

最新评论