Files
sqlite3/vfs/adiantum/example_test.go

55 lines
1.2 KiB
Go
Raw Permalink Normal View History

2024-11-29 15:51:51 +00:00
//go:build linux || darwin || windows || freebsd || openbsd || netbsd || dragonfly || illumos || sqlite3_flock || sqlite3_dotlk
2024-05-12 01:35:22 +01:00
package adiantum_test
import (
"crypto/rand"
"log"
"os"
"golang.org/x/crypto/argon2"
2025-11-20 11:35:01 +00:00
2024-05-12 01:35:22 +01:00
"lukechampine.com/adiantum/hbsh"
"lukechampine.com/adiantum/hpolyc"
2024-10-18 12:20:32 +01:00
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/vfs"
"github.com/ncruces/go-sqlite3/vfs/adiantum"
2024-05-12 01:35:22 +01:00
)
func ExampleRegister_hpolyc() {
2024-10-25 00:12:29 +01:00
vfs.Register("hpolyc", adiantum.Wrap(vfs.Find(""), hpolycCreator{}))
2024-05-12 01:35:22 +01:00
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)
2025-09-18 18:40:56 +01:00
rand.Read(key)
return key
2024-05-12 01:35:22 +01:00
}
// Hash the secret with a KDF.
return argon2.IDKey([]byte(secret), []byte("hpolyc"), 3, 64*1024, 4, 32)
}