Skip to content

Commit

Permalink
pwlib: expose GenerateRandomString
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommi2Day committed Oct 27, 2023
1 parent 23bbf47 commit d46e295
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 6 additions & 3 deletions pwlib/password_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions pwlib/password_generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d46e295

Please sign in to comment.