-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiceware.go
86 lines (72 loc) · 1.78 KB
/
diceware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package diceware
import (
"crypto/rand"
"fmt"
"math/big"
"strings"
)
var max = big.NewInt(6)
// Pick picks a word from the given word list
func Pick(words WordList) (string, error) {
var rs RollSet
rs.RollDice(words.Dice())
return words.At(rs), nil
}
// Passphrase generates a string composed of `nbWord` words from the given WordList, separated by a space
func Passphrase(nbWord int, words WordList) (string, error) {
return PassphraseWithSep(nbWord, words, " ")
}
// PassphraseWithSep generates a string composed of `nbWord` words from the given WordList, separated by the given separator
func PassphraseWithSep(nbWord int, words WordList, sep string) (string, error) {
var builder strings.Builder
for i := 0; i < nbWord-1; i++ {
w, err := Pick(words)
if err != nil {
return "", err
}
fmt.Fprintf(&builder, "%s%s", w, sep)
}
w, err := Pick(words)
if err != nil {
return "", err
}
builder.WriteString(w)
return builder.String(), nil
}
// RollSet is a set die rolls
// Each roll takes 3 bits
type RollSet uint16
// RollDice rolls the given number of dice
func (r *RollSet) RollDice(nb int) error {
*r = 0
for i := 0; i < nb; i++ {
roll, err := rand.Int(rand.Reader, max)
if err != nil {
return err
}
*r = *r | (RollSet(roll.Int64()+1) << (i * 3))
}
return nil
}
// WordList is a list of word
type WordList interface {
// Dice returns the number of dice used to pick a word
Dice() int
// At returns the word at the given RollSet
At(rs RollSet) string
}
type wordList map[RollSet]string
type listWith4Dice wordList
func (wl listWith4Dice) At(rs RollSet) string {
return wl[rs]
}
func (listWith4Dice) Dice() int {
return 4
}
type listWith5Dice wordList
func (listWith5Dice) Dice() int {
return 5
}
func (wl listWith5Dice) At(rs RollSet) string {
return wl[rs]
}