2023-10-07 13:22:30 +00:00
|
|
|
package scsusers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base32"
|
2023-10-07 13:36:54 +00:00
|
|
|
"log"
|
2023-10-07 13:31:42 +00:00
|
|
|
mr "math/rand"
|
|
|
|
"time"
|
2023-10-07 13:22:30 +00:00
|
|
|
"unicode"
|
|
|
|
)
|
|
|
|
|
|
|
|
func flipCoin() bool {
|
2023-10-07 13:36:54 +00:00
|
|
|
r := mr.New(mr.NewSource(time.Now().UnixNano()))
|
|
|
|
return r.Intn(3) == 1
|
2023-10-07 13:22:30 +00:00
|
|
|
}
|
2023-10-07 13:36:54 +00:00
|
|
|
|
2023-10-07 13:22:30 +00:00
|
|
|
func scrambleCase(in []byte) []byte {
|
|
|
|
var out []byte
|
|
|
|
for _, x := range string(in) {
|
2023-10-07 13:36:54 +00:00
|
|
|
if unicode.IsUpper(x) && flipCoin() {
|
|
|
|
out = append(out, byte(unicode.ToLower(x)))
|
|
|
|
} else {
|
|
|
|
out = append(out, byte(x))
|
2023-10-07 13:22:30 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-07 13:36:54 +00:00
|
|
|
log.Printf("scrambleCase in %s out %s", in, out)
|
2023-10-07 13:22:30 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func randBytes(n int) []byte {
|
|
|
|
randomBytes := make([]byte, 32)
|
|
|
|
_, err := rand.Read(randomBytes)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return []byte(base32.StdEncoding.EncodeToString(randomBytes)[:n])
|
|
|
|
}
|
|
|
|
|
|
|
|
func generatePassword(n int) []byte {
|
|
|
|
for {
|
|
|
|
p := scrambleCase(randBytes(16))
|
|
|
|
if hasDigit(p) && hasUppercase(p) && hasLowercase(p) {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasDigit(in []byte) bool {
|
2023-10-07 13:36:54 +00:00
|
|
|
for _, x := range string(in) {
|
2023-10-07 13:22:30 +00:00
|
|
|
if unicode.IsDigit(x) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasUppercase(in []byte) bool {
|
2023-10-07 13:36:54 +00:00
|
|
|
for _, x := range string(in) {
|
2023-10-07 13:22:30 +00:00
|
|
|
if unicode.IsUpper(x) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasLowercase(in []byte) bool {
|
2023-10-07 13:36:54 +00:00
|
|
|
for _, x := range string(in) {
|
2023-10-07 13:22:30 +00:00
|
|
|
if unicode.IsLower(x) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|