38 lines
794 B
Go
38 lines
794 B
Go
package vault
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"errors"
|
|
"fmt"
|
|
"math/big"
|
|
"strings"
|
|
)
|
|
|
|
// Diceware returns a diceware password with `length` words in it.
|
|
func Diceware(length int) (string, error) {
|
|
words := []string{}
|
|
for i := 0; i < length; i++ {
|
|
// Roll five random dice.
|
|
rolls := ""
|
|
for j := 1; j <= DieCount; j++ {
|
|
roll, _ := rand.Int(rand.Reader, big.NewInt(6))
|
|
rolls += fmt.Sprintf("%d", roll.Int64()+1)
|
|
}
|
|
|
|
word, err := findWord(rolls)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
words = append(words, word)
|
|
}
|
|
return strings.Join(words, " "), nil
|
|
}
|
|
|
|
// findWord looks up a word based on a dice roll.
|
|
func findWord(roll string) (string, error) {
|
|
if word, ok := dict[roll]; ok {
|
|
return word, nil
|
|
}
|
|
return "", errors.New("roll pattern not found? this should not happen")
|
|
}
|