Categorygithub.com/youthlin/go-sqlcipher
repositorypackage
0.0.2
Repository: https://github.com/youthlin/go-sqlcipher.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

go-sqlcipher

Description 简介

Fork from mutecomm/go-sqlcipher

与之区别

  • 更新了上游代码的版本
  • 支持了任意的 _pragma_xxx 参数
  • 致谢:本仓库参考了 yifengyou/go-sqlcipher fork 版本的改动

Installation 安装

go get github.com/youthlin/go-sqlcipher

Documentation 文档

示例代码

package main

import (
	"database/sql"
	"fmt"
	"strings"

	sqlite3 "github.com/youthlin/go-sqlcipher"
)

func main() {
	var (
		db   *sql.DB
		dsn = "EnMicroMsg.db"
		key  = "xxxXxxx"// 密码
	)
	encrypted, err := sqlite3.IsEncrypted(dsn)
	if err != nil {
		panic(err) // 文件不存在or空文件
	}
	if encrypted { // 已加密
		params := []string{
			fmt.Sprintf(`_pragma_key=%q`, key),
			// 注意顺序 _pragma_key 最先;注意引号
			`_pragma_cipher_page_size=1024`,
			`_pragma_kdf_iter=4000`,
			`_pragma_cipher_hmac_algorithm="HMAC_SHA1"`,
			`_pragma_cipher_kdf_algorithm="PBKDF2_HMAC_SHA1"`,
			`_pragma_cipher_use_hmac="OFF"`,
		}
		// 带参数打开加密数据库
		dsn = fmt.Sprintf("%s?%s", dsn, strings.Join(params, "&"))
	}
	db, err = sql.Open("sqlite3", dsn)
	if err != nil {
		panic(err)
	}
	row := db.QueryRow("SELECT count(*) FROM sqlite_master where type='table' ")
	var count int
	err = row.Scan(&count)
	if err != nil {
		panic(err) // may invalid key 可能是密码错误
	}
	fmt.Printf("table count = %d\n", count)
}

Maintenance 从上游更新

参见 Maintenance 文件.

License

参见上游项目各自的开源协议。

The code of the originating packages is covered by their respective licenses. See LICENSE file for details.