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
+17.7k Golang : How to log each HTTP request to your web server?
+14.4k Golang : Find commonalities in two slices or arrays example
+21.4k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+10.3k Golang : ISO8601 Duration Parser example
+12.3k Golang : Sort and reverse sort a slice of bytes
+31.7k Golang : Validate email address with regular expression
+27.7k Golang : Move file to another directory
+5.2k Golang : fmt.Println prints out empty data from struct
+37.6k Golang : Read a text file and replace certain words
+24.1k Golang : GORM read from database example
+11.7k Golang : Convert a rune to unicode style string \u
+5.1k Unix/Linux : How to archive and compress entire directory ?