Then, after stating package main, tell Go that this is an executable file, followed by importing the encoding/base64 and fmt modules: package main To begin, create a file called strings.go in the root directory of the project. To generate random strings in Go, we’ll use Base64 encoding and an external package because it’s a more practical and secure way of generating random numbers. Then, we can run the code again and finally get a different random number between zero and 99 without it repeating: run go numbers.go Our code should now look like so: package main So, when we now run the numbers.go file, we always get a different random number. Since we are using time, we must import the time package time.Now().UnixNano(), which gives us the current time down to the second, thereby changing the Seed() parameter. This runs under the hood, but it takes 1 as the default parameter.Īdd the following code at the beginning of the main() function: rand.Seed(time.Now().UnixNano()) To fix this, we must use the Seed() method with rand. Nothing a computer does is simply random it follows algorithms. While this isn’t technically a problem, it does defeat the aim of generating a random number whenever running the code. The problem now, however, is that when I rerun the program, I always get 81. To do this, let’s run the following: run go numbers.go The additional main() function, which is an entry point of every executable file, prints a random integer that ranges from zero to 99 using the rand.Intn() function. While these two packages are built into Go, be mindful that Go will not run successfully if there is an imported package that is unused in your program. Here, we imported the fmt package to format data and the math/rand package to generate random numbers. Next, copy and paste the following code into the newly created file: package main To generate random numbers in Go, let’s create a new Go file in the project directory: touch numbers.go Without generating random numbers, encryption would be useless and the encrypted data predictable. Generating random numbers or strings is important in programming and is the base of encryption. This go.mod file is where all external modules used in any Go project are listed.įor this tutorial, we do not necessarily need to install external dependencies because Go comes with many modules that can generate, encrypt, and decrypt data. Think of this as package.json in JavaScript or composer.json in PHP. You can then enable dependency tracking by running the following: go mod init code/encrypt Next, create a folder and cd into it: mkdir Encrypt You will see the version of Go that you are running in the terminal: To ensure you have Go working properly in the folder you are at, run the following command in your terminal: go version This all depends on the operating system you use and your Go installation method. If you did not install Golang globally, create a folder in the root folder where your Go installation is. If you installed Golang globally on your machine, you can create a folder where your Go project will reside. To get started, let’s set up our Go project quickly. To follow this tutorial, you must have the following: In this tutorial, we will look at how to encrypt and decrypt data in Go, keeping our data safe by making it difficult to use if it falls into the wrong hands. Imagine if we leave users’ passwords in plain text in a database and the database becomes compromised this can be catastrophic, leading the wrong people to your information. By using encryption, we can convert information into computer code, thereby preventing unauthorized access.įor developers, encryption is essential to protect data in our applications. Given that security is not always 100% guaranteed, there is always a need to protect your information, especially online data. I love building stuff with great technology. Precious Luke Follow Open source crusader.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |