Go’s random number generator is a great way to generate hard-to-guess passwords.
The random number generator provided by the property. The GO programming language generates hard-to-guess passwords composed of ASCII characters. Although the code provided in this article is easy to read, it is best to already know the basics of understanding it. If you’re new to programming languages, go visit and learn, then come back here.
Before jumping into utilities and code, take a look at the following subset of ASCII tables, as shown in the manascii command:
30405060708090100110120
———————————-
0:(2FPZdnx
1:)3=GQ[eoy
2:*4HR\fpz
3:!+5?IS]gq{
4:”,6@JT^hr|
5:#-7AKU_is}
6:$.8BLV`jt~
7:%/9CMWakuDEL
8:0:DNXblv
9:’1;EOYcmw
Printable ASCII characters have decimal values ranging from 33 to 126; other ASCII values are not suitable for inclusion in passwords. Therefore, the utility provided in this article will generate ASCII characters in this range.
Create random integers
The first utility is named random.go and generates a specified number of random integers that reside within a given range. The most important part of this function of random.go is:
randomminmax
returnrandIntnmaxminmin
This function uses the RAND.In() go function to generate a random integer belonging to the given range. Note that rand.Intn() returns a non-negative random number in the range [0,n]; you can find documentation for the math/RAND package in the math/RAND documentation.
The random.go utility accepts three command line parameters: the minimum and maximum value of the generated integer, and the integer to be generated.
Compiling and executing random.go will create output like this:
$gobuildrandom.go
$./random
Usage:./randomMIXMAXTOTAL
$./random1310
2212211221
If you wish to generate more secure random numbers in GO, please use the GO library’s cipher/RAND package.
Create a random password
The second utility, randomPass.go, generates random passwords. randomPass.go uses the random() function to generate random numbers that will be converted to ASCII characters using the following GO code:
for{myRand:=random(MIN,MAX)newChar:=string(startChar[0]+byte(myRand))fmt.Print(newChar)ifi==LENGTH{break}i++
}
The minimum value is 0 and the value of Max is 94. The value of startChar is !, which is the first printable character in the ASCII table (its decimal ASCII code is 33). Therefore, all ASCII characters generated will be in ! In the ~ character, the decimal ASCII code of this character is 126.
Thus, each random number generated is greater than min, less than MAX, and converted to ASCII characters. The process continues until the generated password has the required length.
The randomPass.go utility accepts an (optional) command line argument that defines the length of the generated password. Its default value is 8, which is a fairly common password length. Executing randomPass.go will generate the following type of output:
$gorunrandomPass.go1
Z
$gorunrandomPass.go10
#Cw^a#IwkT
$gorunrandomPass.go
Usingdefaultvalues!
[PP8@’Ci
One last detail: don’t forget to call Rand.Seed() with the seed value to initialize the random number generator. If you always use the same seed value, the random number generator will create the same sequence of random integers.
You can find both random.go and randomPass.go on GitHub. You can also check out play.golang.org. Hope this helps
Summary
The above is the entire content of this article. I hope that the content of this article has certain reference and learning value for everyone’s study or work. Thank you for your support of #. If you want to know more related content, please check the relevant links below
Articles you may be interested in: Example codes for implementing mutex locks, random numbers, time, and random numbers in ListGoLang in Go language Detailed explanation of insertion sorting and random number generation of Go language sorting algorithm Using Golang to generate integer random numbers Example of Golang programming implementation Method to generate n non-repeating random numbers from a to b. Method to return random numbers between 1-99 in Go language. Method to generate random numbers in Go language.