Golang : Generate human readable password
Got a friend who works in the banking sector and she needs to password generator that will generate a password that is easy to be read over the phone by a text-to-voice software and must be "human-friendly" for phone banking purpose. "Human-friendly" in terms of easier to write down and remember.
For example, a password such as "hello123pass" is easier to read and remember than "e1jda87dn22".
Below is a code example that I've helped to create for her that might be useful to you.
For best result, keep the number of alphabets to 6
and digits to 2
, it should be practical and random enough. Anything beyond that will be hard to read and remember.
If you plan to add some runes into the password, see https://www.socketloop.com/tutorials/golang-random-rune-generator
Here you go!
package main
import (
"fmt"
"math/rand"
"time"
)
//var vowels = []rune{'a', 'e', 'i', 'o', 'u'}
//var consonants = []rune{'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n','p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'}
func humanReadablePassword(alphabetSize, numberSize int) string {
vowels := "aeiou"
consonants := "bcdfghjklmnpqrstvwxyz"
digits := "0123456789"
prefixSize := alphabetSize / 2
if alphabetSize%2 != 0 {
prefixSize = int(alphabetSize/2) + 1
}
suffixSize := alphabetSize - prefixSize
var prefixPart = make([]byte, prefixSize)
for i := 0; i <= prefixSize-1; i++ {
if i%2 == 0 {
// use consonants
prefixPart[i] = consonants[rand.Intn(len(consonants)-1)]
} else {
// use vowels
prefixPart[i] = vowels[rand.Intn(len(vowels)-1)]
}
}
var midPart = make([]byte, numberSize)
// use digits
for k, _ := range midPart {
midPart[k] = digits[rand.Intn(len(digits))]
}
var suffixPart = make([]byte, suffixSize)
for i := 0; i <= suffixSize-1; i++ {
if i%2 == 0 {
// use consonants
suffixPart[i] = consonants[rand.Intn(len(consonants)-1)]
} else {
// use vowels
suffixPart[i] = vowels[rand.Intn(len(vowels)-1)]
}
}
return string(prefixPart) + string(midPart) + string(suffixPart)
}
func main() {
rand.Seed(time.Now().UnixNano())
fmt.Println("6 alphabets with 2 digits : ", humanReadablePassword(6, 2)) // best option
fmt.Println("3 alphabets with 8 digits : ", humanReadablePassword(3, 8))
fmt.Println("9 alphabets with 9 digits : ", humanReadablePassword(9, 9))
}
Output:
6 alphabets with 2 digits : hax32diy
3 alphabets with 8 digits : me80029642k
9 alphabets with 9 digits : tewet321311617poqe
References:
https://www.socketloop.com/tutorials/golang-random-rune-generator
https://www.socketloop.com/tutorials/golang-how-to-generate-random-string
See also : Golang : Random Rune generator
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+23.7k Golang : Read a file into an array or slice example
+6.8k Golang : Derive cryptographic key from passwords with Argon2
+7.6k Golang : Dealing with struct's private part
+9.8k Golang : List available AWS regions
+7.1k Golang : Squaring elements in array
+12.2k Golang : Find and draw contours with OpenCV example
+8.3k Golang : Get final or effective URL with Request.URL example
+9.3k Golang : How to control fmt or log print format?
+11.7k Golang : Display a text file line by line with line number example
+14.4k Golang : Simple word wrap or line breaking example
+6.4k Golang : Calculate US Dollar Index (DXY)
+11.2k Golang : How to determine a prime number?