summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/finditer/finditer.go42
-rw-r--r--tools/hashpw/hashpw.go35
2 files changed, 77 insertions, 0 deletions
diff --git a/tools/finditer/finditer.go b/tools/finditer/finditer.go
new file mode 100644
index 0000000..e788d89
--- /dev/null
+++ b/tools/finditer/finditer.go
@@ -0,0 +1,42 @@
+package main
+
+import (
+ "encoding/hex"
+ "fmt"
+ "sort"
+ "testing"
+ "time"
+
+ "roseh.moe/cmd/roseh.moe/internal/pwhash"
+)
+
+func mustHex(s string) []byte {
+ b, err := hex.DecodeString(s)
+ if err != nil {
+ panic(err)
+ }
+ return b
+}
+
+var salt = mustHex("34539b5dd2591415")
+
+func benchmarkHashIter(iter int) time.Duration {
+ const password = "wavy upend 4z jk linda k zing four atop pi"
+ return time.Duration(testing.Benchmark(func(b *testing.B) {
+ for b.Loop() {
+ pwhash.HashIter(password, salt, iter)
+ }
+ }).NsPerOp())
+}
+
+func main() {
+ const targetTime = 5 * time.Second
+ iter := 8192
+ for ; benchmarkHashIter(iter) < targetTime; iter *= 2 {
+ }
+ lo := iter / 2
+ hi := iter
+ fmt.Println(lo + sort.Search(hi-lo, func(n int) bool {
+ return benchmarkHashIter(lo+n) > targetTime
+ }))
+}
diff --git a/tools/hashpw/hashpw.go b/tools/hashpw/hashpw.go
new file mode 100644
index 0000000..f1bc10d
--- /dev/null
+++ b/tools/hashpw/hashpw.go
@@ -0,0 +1,35 @@
+package main
+
+import (
+ "crypto/rand"
+ "encoding/base64"
+ "fmt"
+ "os"
+
+ "golang.org/x/term"
+ "roseh.moe/cmd/roseh.moe/internal/pwhash"
+)
+
+func hashpw() error {
+ fmt.Fprint(os.Stderr, "Enter password: ")
+ pw, err := term.ReadPassword(int(os.Stdin.Fd()))
+ fmt.Fprintln(os.Stderr)
+ if err != nil {
+ return err
+ }
+ salt := make([]byte, pwhash.SaltSize)
+ rand.Read(salt)
+ pwHash, err := pwhash.Hash(string(pw), salt)
+ if err != nil {
+ return err
+ }
+ fmt.Printf("NotepadPassword:\"%s\"\n", base64.StdEncoding.EncodeToString(append(salt, pwHash...)))
+ return nil
+}
+
+func main() {
+ if err := hashpw(); err != nil {
+ fmt.Fprintf(os.Stderr, "hashpw: %s\n", err)
+ os.Exit(1)
+ }
+}