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
+11.5k Golang : How to detect a server/machine network interface capabilities?
+7k Golang : Dealing with postal or zip code example
+8.1k Golang: Prevent over writing file with md5 hash
+11.7k Golang : Convert(cast) bigint to string
+22.3k Generate checksum for a file in Go
+7.1k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+32k Golang : Math pow(the power of x^y) example
+5.8k Golang : Extract unicode string from another unicode string example
+5.4k Golang : Detect words using using consecutive letters in a given string
+18.2k Golang : Logging with logrus