File tree 3 files changed +22
-2
lines changed
3 files changed +22
-2
lines changed Original file line number Diff line number Diff line change 1
1
## Golang Diceware Generator
2
2
3
3
[ ![ GoDoc] ( https://img.shields.io/badge/go-documentation-blue.svg?style=flat-square )] ( https://pkg.go.dev/github.com/sethvargo/go-diceware/diceware )
4
- [ ![ GitHub Actions] ( https://img.shields.io/github/workflow/status/sethvargo/go-diceware/Test ?style=flat-square )] ( https://github.com/sethvargo/go-diceware/actions?query=workflow%3ATest )
4
+ [ ![ GitHub Actions] ( https://img.shields.io/github/actions/ workflow/status/sethvargo/go-diceware/test.yml ?style=flat-square )] ( https://github.com/sethvargo/go-diceware/actions?query=workflow%3ATest )
5
5
6
6
This library implements the [ Diceware] ( https://en.wikipedia.org/wiki/Diceware )
7
7
algorithm in pure Golang. The algorithm is most-commonly used when generating
@@ -60,7 +60,7 @@ information.
60
60
As a CLI:
61
61
62
62
``` sh
63
- $ GO111MODULE=off go get github.com/sethvargo/go-diceware/cmd/diceware
63
+ $ go install github.com/sethvargo/go-diceware/cmd/diceware@latest
64
64
```
65
65
66
66
``` sh
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package diceware
2
2
3
3
import (
4
4
"crypto/rand"
5
+ "fmt"
5
6
"io"
6
7
"math"
7
8
"math/big"
@@ -67,6 +68,13 @@ func NewGenerator(i *GeneratorInput) (*Generator, error) {
67
68
// non-overlapping words, use a single invocation of the function and split the
68
69
// resulting string list.
69
70
func (g * Generator ) Generate (numWords int ) ([]string , error ) {
71
+ if typ , ok := g .wordList .(WordListNumWordser ); ok {
72
+ if l := typ .NumWords (); numWords > l {
73
+ return nil , fmt .Errorf ("number of requested words (%d) cannot exceed the size of the wordlist (%d)" ,
74
+ numWords , l )
75
+ }
76
+ }
77
+
70
78
list := make ([]string , 0 , numWords )
71
79
seen := make (map [string ]struct {}, numWords )
72
80
Original file line number Diff line number Diff line change @@ -12,7 +12,15 @@ type WordList interface {
12
12
WordAt (int ) string
13
13
}
14
14
15
+ // WordListNumWordser is an auxillary interface that returns the number of words
16
+ // in the list. This is a separate interface for backwards compatability.
17
+ type WordListNumWordser interface {
18
+ // NumWords returns the total number of words in the list.
19
+ NumWords () int
20
+ }
21
+
15
22
var _ WordList = (* wordListInternal )(nil )
23
+ var _ WordListNumWordser = (* wordListInternal )(nil )
16
24
17
25
type wordListInternal struct {
18
26
digits int
@@ -26,3 +34,7 @@ func (w *wordListInternal) Digits() int {
26
34
func (w * wordListInternal ) WordAt (i int ) string {
27
35
return w .words [i ]
28
36
}
37
+
38
+ func (w * wordListInternal ) NumWords () int {
39
+ return len (w .words )
40
+ }
You can’t perform that action at this time.
0 commit comments