Skip to content

Commit

Permalink
Merge pull request #116 from swordqiu/hotfix/qj-ensure-password-start…
Browse files Browse the repository at this point in the history
…-with-letters

fix: ensure password starts with letters
  • Loading branch information
swordqiu authored Mar 3, 2024
2 parents cdf9dc0 + f7376ae commit 73685b1
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions util/seclib/seclib.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
ALL_PUNC = "~`!@#$%^&*()-_=+[]{}|:';\",./<>?"
)

var FIRSTCHARS = fmt.Sprintf("%s%s", LETTERS, UPPERS)
var CHARS = fmt.Sprintf("%s%s%s", DIGITS, LETTERS, UPPERS)

func RandomPassword(width int) string {
Expand All @@ -49,8 +50,17 @@ func RandomPassword(width int) string {
letterCnt := 0
upperCnt := 0
for i := 0; i < width; i += 1 {
index := rand.Intn(len(CHARS))
ch := CHARS[index]
var ch byte
{
var candidates string
if i == 0 {
candidates = FIRSTCHARS
} else {
candidates = CHARS
}
index := rand.Intn(len(candidates))
ch = candidates[index]
}
if strings.IndexByte(DIGITS, ch) >= 0 {
digitsCnt += 1
} else if strings.IndexByte(LETTERS, ch) >= 0 {
Expand Down Expand Up @@ -105,13 +115,15 @@ func randomPassword2(width int) string {
var buf bytes.Buffer
for i := 0; i < width; i += 1 {
var ch byte
for {
index := rand.Intn(len(CHARS2))
ch = CHARS2[index]
if i == 0 && ch == '/' {
continue
{
var candidates string
if i == 0 {
candidates = FIRSTCHARS
} else {
candidates = CHARS2
}
break
index := rand.Intn(len(candidates))
ch = CHARS2[index]
}
if strings.IndexByte(DIGITS, ch) >= 0 {
ps.Digits += 1
Expand Down

0 comments on commit 73685b1

Please # to comment.