From d46e295fd5e9c42225940bf4afc7fca5634ffd84 Mon Sep 17 00:00:00 2001 From: Tommi2Day Date: Fri, 27 Oct 2023 20:02:42 +0200 Subject: [PATCH] pwlib: expose GenerateRandomString --- CHANGELOG.md | 7 +++++++ pwlib/password_generate.go | 9 ++++++--- pwlib/password_generate_test.go | 12 ++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c48a46e..97d6d84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Go Library +# [v1.10.0 - 2023-10-27] +### New +- use go 1.21 +- pwlib: expose GenerateRandomString function +### Fixed +- linter issues + # [v1.9.6 - 2023-10-19] ### New - common: add Git functions diff --git a/pwlib/password_generate.go b/pwlib/password_generate.go index 00c2be6..b31abb0 100644 --- a/pwlib/password_generate.go +++ b/pwlib/password_generate.go @@ -8,9 +8,12 @@ import ( log "github.com/sirupsen/logrus" ) -// generateRandomString generate a randow string with the given length and out of allowed charset -func generateRandomString(length int, allowedChars string) string { +// GenerateRandomString generate a randow string with the given length and out of allowed charset +func GenerateRandomString(length int, allowedChars string) string { letters := allowedChars + if len(allowedChars) == 0 { + letters = charset.AllChars + } ret := make([]byte, length) for i := 0; i < length; i++ { num, _ := rand.Int(rand.Reader, big.NewInt(int64(len(letters)))) @@ -48,7 +51,7 @@ func GenPassword(length int, upper int, lower int, numeric int, special int, fir SilentCheck = true // max 50 tries to generate a valid password for c := 0; c < 50; c++ { - newPassword = generateRandomString(length, allowedChars) + newPassword = GenerateRandomString(length, allowedChars) ok = DoPasswordCheck(newPassword, length, upper, lower, numeric, special, firstCharCheck, allowedChars) if ok { break diff --git a/pwlib/password_generate_test.go b/pwlib/password_generate_test.go index b329538..f2d1434 100644 --- a/pwlib/password_generate_test.go +++ b/pwlib/password_generate_test.go @@ -65,6 +65,18 @@ func TestGenPassword(t *testing.T) { true, AllChars, }, + { + "User-16-2-2-2-2-1-no-charset", + true, + true, + 16, + 2, + 2, + 2, + 2, + true, + "", + }, { "Only-8-UpperAndDigits", true,