mirror of
https://github.com/ncruces/go-sqlite3.git
synced 2026-01-12 05:59:14 +00:00
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
//go:build (linux || darwin || windows || freebsd || openbsd || netbsd || dragonfly || illumos || sqlite3_flock) && !sqlite3_nosys
|
|
|
|
package adiantum_test
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/ncruces/go-sqlite3"
|
|
"github.com/ncruces/go-sqlite3/vfs"
|
|
"github.com/ncruces/go-sqlite3/vfs/adiantum"
|
|
"golang.org/x/crypto/argon2"
|
|
"lukechampine.com/adiantum/hbsh"
|
|
"lukechampine.com/adiantum/hpolyc"
|
|
)
|
|
|
|
func ExampleRegister_hpolyc() {
|
|
adiantum.Register("hpolyc", vfs.Find(""), hpolycCreator{})
|
|
|
|
db, err := sqlite3.Open("file:demo.db?vfs=hpolyc" +
|
|
"&textkey=correct+horse+battery+staple")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer os.Remove("./demo.db")
|
|
defer db.Close()
|
|
// Output:
|
|
}
|
|
|
|
type hpolycCreator struct{}
|
|
|
|
// HBSH creates an HBSH cipher given a key.
|
|
func (hpolycCreator) HBSH(key []byte) *hbsh.HBSH {
|
|
if len(key) != 32 {
|
|
// Key is not appropriate, return nil.
|
|
return nil
|
|
}
|
|
return hpolyc.New(key)
|
|
}
|
|
|
|
// KDF gets a key from a secret.
|
|
func (hpolycCreator) KDF(secret string) []byte {
|
|
if secret == "" {
|
|
// No secret is given, generate a random key.
|
|
key := make([]byte, 32)
|
|
n, _ := rand.Read(key)
|
|
return key[:n]
|
|
}
|
|
// Hash the secret with a KDF.
|
|
return argon2.IDKey([]byte(secret), []byte("hpolyc"), 3, 64*1024, 4, 32)
|
|
}
|