Skip to content

Commit

Permalink
Switching to use crypto/rand
Browse files Browse the repository at this point in the history
  • Loading branch information
safaci2000 committed Mar 11, 2024
1 parent 7d820f7 commit 5de280f
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions internal/config/types.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package config

import (
"crypto/rand"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"github.com/sethvargo/go-password/password"
"github.com/spf13/viper"
"log/slog"
"math/rand/v2"
"math/big"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -74,7 +75,12 @@ func (u *UserSettings) GetPassword(username string) string {
return u.defaultUserPassword(username)
}

passLength := rand.IntN(u.MaxLength-u.MinLength) + u.MinLength
nBig, err := rand.Int(rand.Reader, big.NewInt(int64(u.MaxLength)))
if err != nil {
slog.Warn("Failed to get random value")
return u.defaultUserPassword(username)
}
passLength := int(nBig.Int64() + int64(u.MinLength))
res, err := password.Generate(passLength, 1, 1, false, false)
if err != nil {
slog.Warn("unable to generate a proper random password, falling back on default password pattern",
Expand Down

0 comments on commit 5de280f

Please sign in to comment.