github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/vfs/adiantum/example_test.go (about) 1 //go:build (linux || darwin || windows || freebsd || illumos) && !sqlite3_nosys 2 3 package adiantum_test 4 5 import ( 6 "crypto/rand" 7 "log" 8 "os" 9 10 "github.com/ncruces/go-sqlite3" 11 "github.com/ncruces/go-sqlite3/vfs" 12 "github.com/ncruces/go-sqlite3/vfs/adiantum" 13 "golang.org/x/crypto/argon2" 14 "lukechampine.com/adiantum/hbsh" 15 "lukechampine.com/adiantum/hpolyc" 16 ) 17 18 func ExampleRegister_hpolyc() { 19 adiantum.Register("hpolyc", vfs.Find(""), hpolycCreator{}) 20 21 db, err := sqlite3.Open("file:demo.db?vfs=hpolyc" + 22 "&textkey=correct+horse+battery+staple") 23 if err != nil { 24 log.Fatal(err) 25 } 26 defer os.Remove("./demo.db") 27 defer db.Close() 28 // Output: 29 } 30 31 type hpolycCreator struct{} 32 33 // HBSH creates an HBSH cipher given a key. 34 func (hpolycCreator) HBSH(key []byte) *hbsh.HBSH { 35 if len(key) != 32 { 36 // Key is not appropriate, return nil. 37 return nil 38 } 39 return hpolyc.New(key) 40 } 41 42 // KDF gets a key from a secret. 43 func (hpolycCreator) KDF(secret string) []byte { 44 if secret == "" { 45 // No secret is given, generate a random key. 46 key := make([]byte, 32) 47 n, _ := rand.Read(key) 48 return key[:n] 49 } 50 // Hash the secret with a KDF. 51 return argon2.IDKey([]byte(secret), []byte("hpolyc"), 3, 64*1024, 4, 32) 52 }