Skip to content

Commit 8c36572

Browse files
authored
Error if the requested words exceeds the word list size (#15)
Also fix some things in the README
1 parent e379976 commit 8c36572

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## Golang Diceware Generator
22

33
[![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)
55

66
This library implements the [Diceware](https://en.wikipedia.org/wiki/Diceware)
77
algorithm in pure Golang. The algorithm is most-commonly used when generating
@@ -60,7 +60,7 @@ information.
6060
As a CLI:
6161

6262
```sh
63-
$ GO111MODULE=off go get github.com/sethvargo/go-diceware/cmd/diceware
63+
$ go install github.com/sethvargo/go-diceware/cmd/diceware@latest
6464
```
6565

6666
```sh

diceware/generate.go

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package diceware
22

33
import (
44
"crypto/rand"
5+
"fmt"
56
"io"
67
"math"
78
"math/big"
@@ -67,6 +68,13 @@ func NewGenerator(i *GeneratorInput) (*Generator, error) {
6768
// non-overlapping words, use a single invocation of the function and split the
6869
// resulting string list.
6970
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+
7078
list := make([]string, 0, numWords)
7179
seen := make(map[string]struct{}, numWords)
7280

diceware/word_list.go

+12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ type WordList interface {
1212
WordAt(int) string
1313
}
1414

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+
1522
var _ WordList = (*wordListInternal)(nil)
23+
var _ WordListNumWordser = (*wordListInternal)(nil)
1624

1725
type wordListInternal struct {
1826
digits int
@@ -26,3 +34,7 @@ func (w *wordListInternal) Digits() int {
2634
func (w *wordListInternal) WordAt(i int) string {
2735
return w.words[i]
2836
}
37+
38+
func (w *wordListInternal) NumWords() int {
39+
return len(w.words)
40+
}

0 commit comments

Comments
 (0)