before adding cryptoshuffler

This commit is contained in:
2022-03-03 05:51:22 -05:00
parent 480dcd7e24
commit 77d5271b5a
5 changed files with 116 additions and 23 deletions
+32 -3
View File
@@ -1,12 +1,37 @@
package pwgenerator
import (
"bytes"
"crypto/rand"
"math/big"
"sort"
"strings"
)
/*
GetCharset returns a CharSet from a set of characters.
chars can be one of []rune, []byte, []string, or string.
*/
func GetCharset(chars interface{}) (charset CharSet, err error) {
switch t := chars.(type) {
case []rune:
charset = CharSet(string(t))
case []byte:
charset = CharSet(string(t))
case []string:
s := strings.Join(t, "")
charset = CharSet(s)
case string:
charset = CharSet(t)
default:
err = ErrBadType
return
}
return
}
// sortDedupe sorts a slice of runes and deduplicates them.
func sortDedupe(charset *CharSet) {
@@ -42,12 +67,16 @@ func sortDedupe(charset *CharSet) {
*/
func saferRandInt(max int) (randInt int, err error) {
// Avoid a panic on max being 0 per rand.Int.
if max == 0 {
max = 1
}
var max64 int64 = int64(max)
var cryptoInt *big.Int = big.NewInt(max64)
var cryptoReader *bytes.Buffer = new(bytes.Buffer)
var randInt64 int64
if cryptoInt, err = rand.Int(cryptoReader, cryptoInt); err != nil {
if cryptoInt, err = rand.Int(rand.Reader, cryptoInt); err != nil {
return
}