From 3950be71c13cc3ff82854264d793337df0984fd8 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Sun, 12 May 2024 01:35:22 +0100 Subject: [PATCH] HPolyC example. --- vfs/adiantum/example_test.go | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 vfs/adiantum/example_test.go diff --git a/vfs/adiantum/example_test.go b/vfs/adiantum/example_test.go new file mode 100644 index 0000000..e7d0eae --- /dev/null +++ b/vfs/adiantum/example_test.go @@ -0,0 +1,52 @@ +//go:build (linux || darwin || windows || freebsd || illumos) && !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) +}