add password generator
This commit is contained in:
20
vendor/github.com/sethvargo/go-diceware/LICENSE
generated
vendored
Normal file
20
vendor/github.com/sethvargo/go-diceware/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
Copyright 2017 Seth Vargo <seth@sethvargo.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
195
vendor/github.com/sethvargo/go-diceware/diceware/generate.go
generated
vendored
Normal file
195
vendor/github.com/sethvargo/go-diceware/diceware/generate.go
generated
vendored
Normal file
@@ -0,0 +1,195 @@
|
||||
package diceware
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
// sides is the number of sides on a die.
|
||||
var sides = big.NewInt(6)
|
||||
|
||||
var _ DicewareGenerator = (*Generator)(nil)
|
||||
|
||||
// Generator is the stateful generator which can be used to customize the word
|
||||
// list and other generation options.
|
||||
type Generator struct {
|
||||
wordList WordList
|
||||
randReader io.Reader
|
||||
}
|
||||
|
||||
// GeneratorInput is used as input to the NewGenerator function.
|
||||
type GeneratorInput struct {
|
||||
// WordList is the word list to use. There are built-in word lists like
|
||||
// WordListEffBig (default), WordListEffSmall, and WordListOriginal. You can
|
||||
// also bring your own word list by implementing the WordList interface.
|
||||
WordList WordList
|
||||
|
||||
// RandReader is an optional reader to use in place of the default
|
||||
// (crypto/rand.Reader), which can be used to generate repeatable sets of
|
||||
// words
|
||||
RandReader io.Reader
|
||||
}
|
||||
|
||||
// NewGenerator creates a new Generator from the specified configuration. If no
|
||||
// input is given, all the default values are used. This function is safe for
|
||||
// concurrent use.
|
||||
func NewGenerator(i *GeneratorInput) (*Generator, error) {
|
||||
if i == nil {
|
||||
i = new(GeneratorInput)
|
||||
}
|
||||
|
||||
if i.WordList == nil {
|
||||
i.WordList = WordListEffLarge()
|
||||
}
|
||||
|
||||
gen := &Generator{
|
||||
wordList: i.WordList,
|
||||
randReader: i.RandReader,
|
||||
}
|
||||
|
||||
if gen.randReader == nil {
|
||||
gen.randReader = rand.Reader
|
||||
}
|
||||
|
||||
return gen, nil
|
||||
}
|
||||
|
||||
// Generate generates a collection of diceware words, specified by the numWords
|
||||
// parameter.
|
||||
//
|
||||
// The algorithm is fast, but it's not designed to be performant, favoring
|
||||
// entropy over speed.
|
||||
//
|
||||
// This function is safe for concurrent use, but there is a possibility of
|
||||
// concurrent invocations generating overlapping words. To generate multiple
|
||||
// non-overlapping words, use a single invocation of the function and split the
|
||||
// resulting string list.
|
||||
func (g *Generator) Generate(numWords int) ([]string, error) {
|
||||
if typ, ok := g.wordList.(WordListNumWordser); ok {
|
||||
if l := typ.NumWords(); numWords > l {
|
||||
return nil, fmt.Errorf("number of requested words (%d) cannot exceed the size of the wordlist (%d)",
|
||||
numWords, l)
|
||||
}
|
||||
}
|
||||
|
||||
list := make([]string, 0, numWords)
|
||||
seen := make(map[string]struct{}, numWords)
|
||||
|
||||
for i := 0; i < numWords; i++ {
|
||||
n, err := g.RollWord(g.wordList.Digits())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
word := g.wordList.WordAt(n)
|
||||
if _, ok := seen[word]; ok {
|
||||
i--
|
||||
continue
|
||||
}
|
||||
|
||||
list = append(list, word)
|
||||
seen[word] = struct{}{}
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// MustGenerate is the same as Generate, but panics on error.
|
||||
func (g *Generator) MustGenerate(numWords int) []string {
|
||||
list, err := g.Generate(numWords)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
// Generate - see Generator.Generate for usage.
|
||||
func Generate(numWords int) ([]string, error) {
|
||||
gen, err := NewGenerator(nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return gen.Generate(numWords)
|
||||
}
|
||||
|
||||
// MustGenerate - see Generator.MustGenerate for usage.
|
||||
func MustGenerate(numWords int) []string {
|
||||
gen, err := NewGenerator(nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return gen.MustGenerate(numWords)
|
||||
}
|
||||
|
||||
// GenerateWithWordList generates a list of the given number of words from the
|
||||
// given word list.
|
||||
func GenerateWithWordList(numWords int, wordList WordList) ([]string, error) {
|
||||
gen, err := NewGenerator(&GeneratorInput{
|
||||
WordList: wordList,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return gen.Generate(numWords)
|
||||
}
|
||||
|
||||
// WordAt retrieves the word at the given index from EFF's large wordlist.
|
||||
//
|
||||
// Deprecated: Use WordList.WordAt instead.
|
||||
func WordAt(i int) string {
|
||||
return WordListEffLarge().WordAt(i)
|
||||
}
|
||||
|
||||
// RollDie rolls a single 6-sided die and returns a value between [1,6].
|
||||
//
|
||||
// Internally this creates a new Generator with a nil configuration and calls
|
||||
// Generator.RollDie.
|
||||
func RollDie() (int, error) {
|
||||
gen, err := NewGenerator(nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return gen.RollDie()
|
||||
}
|
||||
|
||||
// RollWord rolls and aggregates dice to represent one word in the list. The
|
||||
// result is the index of the word in the list.
|
||||
//
|
||||
// Internally this creates a new Generator with a nil configuration and calls
|
||||
// Generator.RollWord.
|
||||
func RollWord(d int) (int, error) {
|
||||
gen, err := NewGenerator(nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return gen.RollWord(d)
|
||||
}
|
||||
|
||||
// RollDie rolls a single 6-sided die and returns a value between [1,6].
|
||||
func (g *Generator) RollDie() (int, error) {
|
||||
r, err := rand.Int(g.randReader, sides)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to generate a random number: %w", err)
|
||||
}
|
||||
return int(r.Int64()) + 1, nil
|
||||
}
|
||||
|
||||
// RollWord rolls and aggregates dice to represent one word in the list. The
|
||||
// result is the index of the word in the list.
|
||||
func (g *Generator) RollWord(d int) (int, error) {
|
||||
var final int
|
||||
|
||||
for i := d; i > 0; i-- {
|
||||
res, err := g.RollDie()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
final += res * int(math.Pow(10, float64(i-1)))
|
||||
}
|
||||
|
||||
return final, nil
|
||||
}
|
||||
6
vendor/github.com/sethvargo/go-diceware/diceware/interface.go
generated
vendored
Normal file
6
vendor/github.com/sethvargo/go-diceware/diceware/interface.go
generated
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
package diceware
|
||||
|
||||
type DicewareGenerator interface {
|
||||
Generate(int) ([]string, error)
|
||||
MustGenerate(int) []string
|
||||
}
|
||||
38
vendor/github.com/sethvargo/go-diceware/diceware/mock.go
generated
vendored
Normal file
38
vendor/github.com/sethvargo/go-diceware/diceware/mock.go
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
package diceware
|
||||
|
||||
var _ DicewareGenerator = (*mockGenerator)(nil)
|
||||
|
||||
type mockGenerator struct {
|
||||
result []string
|
||||
err error
|
||||
}
|
||||
|
||||
// NewMockGenerator creates a new generator that satisfies the DicewareGenerator
|
||||
// interface. If an error is provided, the error is returned. If a result if
|
||||
// provided, the result is always returned, regardless of what parameters are
|
||||
// passed into the Generate or MustGenerate methods.
|
||||
//
|
||||
// This function is most useful for tests where you want to have predicable
|
||||
// results for a transitive resource that depends on go-diceware.
|
||||
func NewMockGenerator(result []string, err error) *mockGenerator {
|
||||
return &mockGenerator{
|
||||
result: result,
|
||||
err: err,
|
||||
}
|
||||
}
|
||||
|
||||
// Generate returns the mocked result or error.
|
||||
func (g *mockGenerator) Generate(int) ([]string, error) {
|
||||
if g.err != nil {
|
||||
return nil, g.err
|
||||
}
|
||||
return g.result, nil
|
||||
}
|
||||
|
||||
// MustGenerate returns the mocked result or panics if an error was given.
|
||||
func (g *mockGenerator) MustGenerate(int) []string {
|
||||
if g.err != nil {
|
||||
panic(g.err)
|
||||
}
|
||||
return g.result
|
||||
}
|
||||
42
vendor/github.com/sethvargo/go-diceware/diceware/word_list.go
generated
vendored
Normal file
42
vendor/github.com/sethvargo/go-diceware/diceware/word_list.go
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
package diceware
|
||||
|
||||
// WordList is an interface that must be implemented to be considered a word
|
||||
// list for use in the diceware algorithm. This interface can be implemented by
|
||||
// other libraries.
|
||||
type WordList interface {
|
||||
// Digits is the number of digits for indexes in the word list. This
|
||||
// determines the number of dice rolls.
|
||||
Digits() int
|
||||
|
||||
// WordAt returns the word at the given integer in the word list.
|
||||
WordAt(int) string
|
||||
}
|
||||
|
||||
// WordListNumWordser is an auxiliary interface that returns the number of words
|
||||
// in the list. This is a separate interface for backwards compatibility.
|
||||
type WordListNumWordser interface {
|
||||
// NumWords returns the total number of words in the list.
|
||||
NumWords() int
|
||||
}
|
||||
|
||||
var (
|
||||
_ WordList = (*wordListInternal)(nil)
|
||||
_ WordListNumWordser = (*wordListInternal)(nil)
|
||||
)
|
||||
|
||||
type wordListInternal struct {
|
||||
digits int
|
||||
words map[int]string
|
||||
}
|
||||
|
||||
func (w *wordListInternal) Digits() int {
|
||||
return w.digits
|
||||
}
|
||||
|
||||
func (w *wordListInternal) WordAt(i int) string {
|
||||
return w.words[i]
|
||||
}
|
||||
|
||||
func (w *wordListInternal) NumWords() int {
|
||||
return len(w.words)
|
||||
}
|
||||
7794
vendor/github.com/sethvargo/go-diceware/diceware/word_list_eff_large.go
generated
vendored
Normal file
7794
vendor/github.com/sethvargo/go-diceware/diceware/word_list_eff_large.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1314
vendor/github.com/sethvargo/go-diceware/diceware/word_list_eff_small.go
generated
vendored
Normal file
1314
vendor/github.com/sethvargo/go-diceware/diceware/word_list_eff_small.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
7789
vendor/github.com/sethvargo/go-diceware/diceware/word_list_original.go
generated
vendored
Normal file
7789
vendor/github.com/sethvargo/go-diceware/diceware/word_list_original.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
20
vendor/github.com/sethvargo/go-password/LICENSE
generated
vendored
Normal file
20
vendor/github.com/sethvargo/go-password/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
Copyright 2017 Seth Vargo <seth@sethvargo.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
262
vendor/github.com/sethvargo/go-password/password/generate.go
generated
vendored
Normal file
262
vendor/github.com/sethvargo/go-password/password/generate.go
generated
vendored
Normal file
@@ -0,0 +1,262 @@
|
||||
// Package password provides a library for generating high-entropy random
|
||||
// password strings via the crypto/rand package.
|
||||
//
|
||||
// res, err := Generate(64, 10, 10, false, false)
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// log.Printf(res)
|
||||
//
|
||||
// Most functions are safe for concurrent use.
|
||||
package password
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/big"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Built-time checks that the generators implement the interface.
|
||||
var _ PasswordGenerator = (*Generator)(nil)
|
||||
|
||||
// PasswordGenerator is an interface that implements the Generate function. This
|
||||
// is useful for testing where you can pass this interface instead of a real
|
||||
// password generator to mock responses for predicability.
|
||||
type PasswordGenerator interface {
|
||||
Generate(int, int, int, bool, bool) (string, error)
|
||||
MustGenerate(int, int, int, bool, bool) string
|
||||
}
|
||||
|
||||
const (
|
||||
// LowerLetters is the list of lowercase letters.
|
||||
LowerLetters = "abcdefghijklmnopqrstuvwxyz"
|
||||
|
||||
// UpperLetters is the list of uppercase letters.
|
||||
UpperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
// Digits is the list of permitted digits.
|
||||
Digits = "0123456789"
|
||||
|
||||
// Symbols is the list of symbols.
|
||||
Symbols = "~!@#$%^&*()_+`-={}|[]\\:\"<>?,./"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrExceedsTotalLength is the error returned with the number of digits and
|
||||
// symbols is greater than the total length.
|
||||
ErrExceedsTotalLength = errors.New("number of digits and symbols must be less than total length")
|
||||
|
||||
// ErrLettersExceedsAvailable is the error returned with the number of letters
|
||||
// exceeds the number of available letters and repeats are not allowed.
|
||||
ErrLettersExceedsAvailable = errors.New("number of letters exceeds available letters and repeats are not allowed")
|
||||
|
||||
// ErrDigitsExceedsAvailable is the error returned with the number of digits
|
||||
// exceeds the number of available digits and repeats are not allowed.
|
||||
ErrDigitsExceedsAvailable = errors.New("number of digits exceeds available digits and repeats are not allowed")
|
||||
|
||||
// ErrSymbolsExceedsAvailable is the error returned with the number of symbols
|
||||
// exceeds the number of available symbols and repeats are not allowed.
|
||||
ErrSymbolsExceedsAvailable = errors.New("number of symbols exceeds available symbols and repeats are not allowed")
|
||||
)
|
||||
|
||||
// Generator is the stateful generator which can be used to customize the list
|
||||
// of letters, digits, and/or symbols.
|
||||
type Generator struct {
|
||||
lowerLetters string
|
||||
upperLetters string
|
||||
digits string
|
||||
symbols string
|
||||
reader io.Reader
|
||||
}
|
||||
|
||||
// GeneratorInput is used as input to the NewGenerator function.
|
||||
type GeneratorInput struct {
|
||||
LowerLetters string
|
||||
UpperLetters string
|
||||
Digits string
|
||||
Symbols string
|
||||
Reader io.Reader // rand.Reader by default
|
||||
}
|
||||
|
||||
// NewGenerator creates a new Generator from the specified configuration. If no
|
||||
// input is given, all the default values are used. This function is safe for
|
||||
// concurrent use.
|
||||
func NewGenerator(i *GeneratorInput) (*Generator, error) {
|
||||
if i == nil {
|
||||
i = new(GeneratorInput)
|
||||
}
|
||||
|
||||
g := &Generator{
|
||||
lowerLetters: i.LowerLetters,
|
||||
upperLetters: i.UpperLetters,
|
||||
digits: i.Digits,
|
||||
symbols: i.Symbols,
|
||||
reader: i.Reader,
|
||||
}
|
||||
|
||||
if g.lowerLetters == "" {
|
||||
g.lowerLetters = LowerLetters
|
||||
}
|
||||
|
||||
if g.upperLetters == "" {
|
||||
g.upperLetters = UpperLetters
|
||||
}
|
||||
|
||||
if g.digits == "" {
|
||||
g.digits = Digits
|
||||
}
|
||||
|
||||
if g.symbols == "" {
|
||||
g.symbols = Symbols
|
||||
}
|
||||
|
||||
if g.reader == nil {
|
||||
g.reader = rand.Reader
|
||||
}
|
||||
|
||||
return g, nil
|
||||
}
|
||||
|
||||
// Generate generates a password with the given requirements. length is the
|
||||
// total number of characters in the password. numDigits is the number of digits
|
||||
// to include in the result. numSymbols is the number of symbols to include in
|
||||
// the result. noUpper excludes uppercase letters from the results. allowRepeat
|
||||
// allows characters to repeat.
|
||||
//
|
||||
// The algorithm is fast, but it's not designed to be performant; it favors
|
||||
// entropy over speed. This function is safe for concurrent use.
|
||||
func (g *Generator) Generate(length, numDigits, numSymbols int, noUpper, allowRepeat bool) (string, error) {
|
||||
letters := g.lowerLetters
|
||||
if !noUpper {
|
||||
letters += g.upperLetters
|
||||
}
|
||||
|
||||
chars := length - numDigits - numSymbols
|
||||
if chars < 0 {
|
||||
return "", ErrExceedsTotalLength
|
||||
}
|
||||
|
||||
if !allowRepeat && chars > len(letters) {
|
||||
return "", ErrLettersExceedsAvailable
|
||||
}
|
||||
|
||||
if !allowRepeat && numDigits > len(g.digits) {
|
||||
return "", ErrDigitsExceedsAvailable
|
||||
}
|
||||
|
||||
if !allowRepeat && numSymbols > len(g.symbols) {
|
||||
return "", ErrSymbolsExceedsAvailable
|
||||
}
|
||||
|
||||
var result string
|
||||
|
||||
// Characters
|
||||
for i := 0; i < chars; i++ {
|
||||
ch, err := randomElement(g.reader, letters)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if !allowRepeat && strings.Contains(result, ch) {
|
||||
i--
|
||||
continue
|
||||
}
|
||||
|
||||
result, err = randomInsert(g.reader, result, ch)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
// Digits
|
||||
for i := 0; i < numDigits; i++ {
|
||||
d, err := randomElement(g.reader, g.digits)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if !allowRepeat && strings.Contains(result, d) {
|
||||
i--
|
||||
continue
|
||||
}
|
||||
|
||||
result, err = randomInsert(g.reader, result, d)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
// Symbols
|
||||
for i := 0; i < numSymbols; i++ {
|
||||
sym, err := randomElement(g.reader, g.symbols)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if !allowRepeat && strings.Contains(result, sym) {
|
||||
i--
|
||||
continue
|
||||
}
|
||||
|
||||
result, err = randomInsert(g.reader, result, sym)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// MustGenerate is the same as Generate, but panics on error.
|
||||
func (g *Generator) MustGenerate(length, numDigits, numSymbols int, noUpper, allowRepeat bool) string {
|
||||
res, err := g.Generate(length, numDigits, numSymbols, noUpper, allowRepeat)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// Generate is the package shortcut for Generator.Generate.
|
||||
func Generate(length, numDigits, numSymbols int, noUpper, allowRepeat bool) (string, error) {
|
||||
gen, err := NewGenerator(nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return gen.Generate(length, numDigits, numSymbols, noUpper, allowRepeat)
|
||||
}
|
||||
|
||||
// MustGenerate is the package shortcut for Generator.MustGenerate.
|
||||
func MustGenerate(length, numDigits, numSymbols int, noUpper, allowRepeat bool) string {
|
||||
res, err := Generate(length, numDigits, numSymbols, noUpper, allowRepeat)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// randomInsert randomly inserts the given value into the given string.
|
||||
func randomInsert(reader io.Reader, s, val string) (string, error) {
|
||||
if s == "" {
|
||||
return val, nil
|
||||
}
|
||||
|
||||
n, err := rand.Int(reader, big.NewInt(int64(len(s)+1)))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to generate random integer: %w", err)
|
||||
}
|
||||
i := n.Int64()
|
||||
return s[0:i] + val + s[i:], nil
|
||||
}
|
||||
|
||||
// randomElement extracts a random element from the given string.
|
||||
func randomElement(reader io.Reader, s string) (string, error) {
|
||||
n, err := rand.Int(reader, big.NewInt(int64(len(s))))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to generate random integer: %w", err)
|
||||
}
|
||||
return string(s[n.Int64()]), nil
|
||||
}
|
||||
39
vendor/github.com/sethvargo/go-password/password/mock.go
generated
vendored
Normal file
39
vendor/github.com/sethvargo/go-password/password/mock.go
generated
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
package password
|
||||
|
||||
// Built-time checks that the generators implement the interface.
|
||||
var _ PasswordGenerator = (*mockGenerator)(nil)
|
||||
|
||||
type mockGenerator struct {
|
||||
result string
|
||||
err error
|
||||
}
|
||||
|
||||
// NewMockGenerator creates a new generator that satisfies the PasswordGenerator
|
||||
// interface. If an error is provided, the error is returned. If a result if
|
||||
// provided, the result is always returned, regardless of what parameters are
|
||||
// passed into the Generate or MustGenerate methods.
|
||||
//
|
||||
// This function is most useful for tests where you want to have predicable
|
||||
// results for a transitive resource that depends on go-password.
|
||||
func NewMockGenerator(result string, err error) *mockGenerator {
|
||||
return &mockGenerator{
|
||||
result: result,
|
||||
err: err,
|
||||
}
|
||||
}
|
||||
|
||||
// Generate returns the mocked result or error.
|
||||
func (g *mockGenerator) Generate(int, int, int, bool, bool) (string, error) {
|
||||
if g.err != nil {
|
||||
return "", g.err
|
||||
}
|
||||
return g.result, nil
|
||||
}
|
||||
|
||||
// MustGenerate returns the mocked result or panics if an error was given.
|
||||
func (g *mockGenerator) MustGenerate(int, int, int, bool, bool) string {
|
||||
if g.err != nil {
|
||||
panic(g.err)
|
||||
}
|
||||
return g.result
|
||||
}
|
||||
6
vendor/modules.txt
vendored
6
vendor/modules.txt
vendored
@@ -78,6 +78,12 @@ github.com/pmezard/go-difflib/difflib
|
||||
# github.com/robfig/cron/v3 v3.0.1
|
||||
## explicit; go 1.12
|
||||
github.com/robfig/cron/v3
|
||||
# github.com/sethvargo/go-diceware v0.5.0
|
||||
## explicit; go 1.22
|
||||
github.com/sethvargo/go-diceware/diceware
|
||||
# github.com/sethvargo/go-password v0.3.1
|
||||
## explicit; go 1.21
|
||||
github.com/sethvargo/go-password/password
|
||||
# github.com/stretchr/testify v1.10.0
|
||||
## explicit; go 1.17
|
||||
github.com/stretchr/testify/assert
|
||||
|
||||
Reference in New Issue
Block a user