Posts

Go Language - AES Encryption And Decryption Example

Image
Advanced Encryption Standard is a symmetric encryption algorithm. AES encryption is used by the U.S. for securing sensitive but unclassified material, so we can say it is enough secure. I t allows 128 bit, 192 bit and 256-bit encryption. Symmetric encryption is very fast as compared to asymmetric encryption and are used in systems such as database system. The following illustration highlights how symmetric cryptography works: Example: package main import ( "crypto/aes" "crypto/cipher" "crypto/rand" "encoding/hex" "fmt" "io" ) func main() { //generate a random 32 byte key key := GenerateRandomKey() //your secret text secret := "This is my password" //encryption encrypted := encrypt(secret, key) fmt.Printf( "encrypted data: %s\n" , encrypted) //decryption decrypted := decrypt(encrypted, key) fmt.Printf( "decrypted data: %s\n" , decry

Go Language - Program to find the duplicate words in a string

Image
Hello everyone, today we need to find out the duplicate words present in the string and print those words. Example 1: Program to find the duplicate words in a given string //Program to find the duplicate words in a string package main import ( "fmt" "strings" ) // Main function func main() { str := "hola hello hi hola good sleep hello hi night morning" //Converts the string into lowercase strl := strings.ToLower(str) //Split the string into words words := strings.Split(strl, " " ) fmt.Println( "Duplicate words in a given string are, \n" ) for i := 0 ; i < len(words); i++ { count := 1 for j := i + 1 ; j < len(words); j++ { if words[i] == words[j] { count++ //Set words[j] to 0 to avoid printing visited word words[j] = "0" } } //Displays the duplicate word if count is greater tha

Go Language - Maps - Example

Image
A map is an unordered collection of key/value pairs, where each key is unique. We can create a new map with a make statement or a map literal. The default zero value of a map is nil. The len function returns the size of a map. Go Range keyword is used in different kinds of data structures in order to iterate over elements. Create a Map: Create a Map called users that will store int keys and String values: /* Create a new map * Empty map of string-string pairs */ users := make( map [ int ] string ) Add Items: Add a new key-value pair, //Add Items users[ 1 ] = "scott" users[ 2 ] = "june" users[ 3 ] = "april" users[ 4 ] = "carl" users[ 5 ] = "rick" users[ 6 ] = "daryl" Update Value: // Update value users[ 6 ] = "michonne" Iterate over all keys and values: // Iterating over all keys and values for id, name := range users { fmt.Printf( "id :%d name:

Go Arrays - Program to Find Largest & Smallest Element

Image
Example 1: Finding the largest element in an Array // Go program to find largest number in an Array package main import "fmt" // Main function func main() { // Let’s define an array first arr := [8] int { 2 , 11 , 22 , 5 , 6 , 3 , 1 , 9 } fmt.Println( "Largest Number:" , LargestNumber(arr)) //Another Example anotherarr := [8] int { 144 , 666 , 99 , 677 , 433 , 422 , 255 } fmt.Println( "Largest Number:" , LargestNumber(anotherarr)) } func LargestNumber(arr [8] int ) int { var temp int for i := 0 ; i < len(arr); i++ { for j := i + 1 ; j < len(arr); j++ { if arr[i] > arr[j] { temp = arr[i] arr[i] = arr[j] arr[j] = temp } } } return arr[len(arr)- 1 ] } Output: Example 2: Finding the smallest element in an Array // Go program to find smallest number in an Array package main import "fmt" // Main function f

How to iterate over a map in Go?

Image
A map is an unordered collection of key/value pairs, where each key is unique. Go Range keyword is used in different kinds of data structures in order to iterate over elements. Example 1: Iterate over all keys and values // Go program to illustrate how // to iterate the map package main import "fmt" // Main function func main() { // Let’s define a map first usermap := map [ string ] string { "North America" : "Adam" , "Asia" : "Yusuke" , "Africa" : "Arif" , "South America" : "Rahul" , "Australia" : "Joy" , "Europe" : "Roman" , "Antarctica" : "Penguin" , } // Iterating over all keys and values for id, name := range usermap { fmt.Printf( "id :%s name: %s\n" , id, name) } } Output: Example 2: Iterate over all k

Hashing in Go Language - MD5 ,SHA-1, SHA-256, & SHA-512

Image
What does Hashing mean? A secure password hash is an encrypted sequence of characters obtained after applying certain algorithms and manipulations on user-provided password, which are generally very weak and easy to guess. Please remember that once this password hash is generated and stored in the database, you can not convert it back to the original password. MD5 hash in Go The MD5 message-digest algorithm is a widely used hash function producing a 128-bit hash value. Although MD5 was initially designed to be used as a cryptographic hash function, it has been found to suffer from extensive vulnerabilities. It can still be used as a checksum to verify data integrity, but only against unintentional corruption. It remains suitable for other non-cryptographic purposes, for example for determining the partition for a particular key in a partitioned database. Example: Go-MD5 to hash a String package main //Required imports import ( "crypto/md5" "encoding/hex"