Random Passwords with Go - Part 1
Hola a todos :)
Hoy vamos a ver una pequeña entrada en el blog que tenía muchas ganas de traer, y es, como generar contraseñas aleatorias en Go. Para todos aquellos que no sepan Go, poco a poco iré publicando posts, en los que iremos viendo y aprendiendo la sintaxis del lenguaje. Además, continuó trabajando en en los posts sobre seguridad en redes inalámbricas.
Por otro lado, voy a forzarme a escribir más en inglés, es por eso, por lo que las próximas entradas del blog, van a ser todas en inglés, sin más dilación, comencemos!!!
Hello everyone, in this post we are going to see one way to generate random passwords in Go.
The first
thing that we are going to do, it’s to define our own function type, which is similar to defineing structs in Go.
The code
snippet above creates a new function type Functions which has no
arguments but returns a string. At this point we can define variables of type Functions.
What we are
going to do, it’s to generate one function for each type of element or char that it’s going to compose our random password.
In this
case, our password will have lowercase and uppercase letters, special characters
and numbers, so we are going to define one function for each case.
Let me
explain this code above. First of all, we are using a map to store each
lowercase letter.
A map, maps
keys to values. In order languages are call hashes (like in C) or dictionaries
(like in C#). If we want to generate an empty map, we must use make function,
which will return a map of the given type, initialized and ready for use. In
this case we are defining map literals, which are like struct literals, but the
keys are required.
Inside a function, the short assignment ( := ) statement can be used in place of a var declaration with implicit type.
In this
case, our map is using int64 as type for the key value and rune
as type for the value.
The type rune
is a special type. Rune literals are just 32-bit integer values (however
they're untyped constants, so their type can change). They represent unicode
codepoints.
For
example, the rune literal 'a' is the
number 97.
For the case of using numbers, we will use the rand function available in the crypto package for Go. Once we have the random number, we will convert that number to a char so that we can append that char to the password string. If you want to cast one number to char, you must use the strconv package. In this function we will generate one random number between 0 and 9 and we will use big.NewInt for that. NewInt function is from math/big package for Go.
Till here the first part, in the next post the second part. We will go little by little and I will continue publishing posts :)
Thank you very much!!!






Comentarios
Publicar un comentario