mirror of
https://github.com/mjl-/mox.git
synced 2025-06-28 02:28:15 +03:00

Mostly using slice.Sort, using min/max, slices.Concat, range of int and fmt.Appendf for byte slices instead of strings.
24 lines
403 B
Go
24 lines
403 B
Go
package mox
|
|
|
|
import (
|
|
cryptorand "crypto/rand"
|
|
)
|
|
|
|
func GeneratePassword() string {
|
|
chars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*-_;:,<.>/"
|
|
s := ""
|
|
buf := make([]byte, 1)
|
|
for range 12 {
|
|
for {
|
|
cryptorand.Read(buf)
|
|
i := int(buf[0])
|
|
if i+len(chars) > 255 {
|
|
continue // Prevent bias.
|
|
}
|
|
s += string(chars[i%len(chars)])
|
|
break
|
|
}
|
|
}
|
|
return s
|
|
}
|