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
+16.7k Golang : How to save log messages to file?
+6.8k Golang : Validate credit card example
+47.2k Golang : Convert int to byte array([]byte)
+5.2k Golang : If else example and common mistake
+12.2k Golang : "https://" not allowed in import path
+5.7k PHP : Get client IP address
+10.4k Golang : Simple File Server
+42.8k Golang : Get hardware information such as disk, memory and CPU usage
+5k PHP : Hide PHP version information from curl
+12.4k Swift : Convert (cast) Int or int32 value to CGFloat
+10.8k Golang : Roll the dice example
+11.3k Golang : Display a text file line by line with line number example