2017-10-31 11:01:53 +00:00
|
|
|
package securepassword
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-02-28 18:51:33 +00:00
|
|
|
"github.com/Luzifer/go_helpers/v2/str"
|
2017-10-31 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type XKCD struct{}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrTooFewWords represents an error thrown if the password will
|
|
|
|
// have fewer than four words and are not considered to be safe
|
|
|
|
ErrTooFewWords = errors.New("XKCD passwords with less than 4 words makes no sense")
|
|
|
|
// DefaultXKCD contains an default instance of the XKCD password
|
|
|
|
// generator
|
|
|
|
DefaultXKCD = NewXKCDGenerator()
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewXKCDGenerator initializes a new XKCD password generator
|
|
|
|
// https://xkcd.com/936/
|
|
|
|
func NewXKCDGenerator() *XKCD { return &XKCD{} }
|
|
|
|
|
|
|
|
// GeneratePassword generates a password with the number of words
|
|
|
|
// given and optionally the current date prepended
|
|
|
|
func (x XKCD) GeneratePassword(length int, addDate bool) (string, error) {
|
|
|
|
if length < 4 {
|
|
|
|
return "", ErrTooFewWords
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
password string
|
|
|
|
usedWords []string
|
|
|
|
)
|
|
|
|
|
|
|
|
if addDate {
|
|
|
|
password = time.Now().Format("20060102.")
|
|
|
|
}
|
|
|
|
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
for len(usedWords) < length {
|
|
|
|
word := strings.Title(xkcdWordList[rand.Intn(len(xkcdWordList))])
|
|
|
|
if str.StringInSlice(word, usedWords) {
|
|
|
|
// Don't use a word twice
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
password = password + word
|
|
|
|
usedWords = append(usedWords, word)
|
|
|
|
}
|
|
|
|
|
|
|
|
return password, nil
|
|
|
|
}
|