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
+14.9k Golang : Test floating point numbers not-a-number and infinite example
+17.8k Golang : How to run your code only once with sync.Once object
+6.7k Golang : Reverse a string with unicode
+16.4k Golang : Put UTF8 text on OpenCV video capture image frame
+7k Golang : HttpRouter multiplexer routing example
+15.7k Golang : XML to JSON example
+7.8k Golang : How to get username from email address
+8.4k Golang : Format strings to SEO friendly URL example
+5.9k Golang : Mapping Iban to Dunging alphabets
+7.7k Golang : Get curl -I or head data from URL example
+4k Golang : Convert lines of string into list for delete and insert operation