Skip to content

Commit 3950be7

Browse files
committed
HPolyC example.
1 parent f3dc9bd commit 3950be7

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

vfs/adiantum/example_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}

0 commit comments

Comments
 (0)