Posts

Node.js - 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 1: AES 256 Encryption and Decryption //AES 256 Encryption Demo Program // crypto module const crypto = require( "crypto" ); // encrypt the message function encrypt(plainText, securitykey, outputEncoding, iv) { const cipher = crypto. createCipheriv( "aes-256-cbc" , securitykey, iv); return Buffer. concat([cipher.update(plainText), cipher.final()]). toString(outputEncoding); } //AES decryption function decrypt(cipherText, securitykey, outputEncoding, iv) { const cipher = crypto. createDeci

Node.js - MD5, SHA-1, SHA-256, SHA-384, SHA-512 Example

Image
What does Hashing mean? A secure password hash is an encrypted sequence of characters obtained after applying certain algorithms and manipulations on utilizer-provided passwords, which are generally very impotent and facile to conjecture. Please remember that once this password hash is engendered and stored in the database, you can not convert it back to the pristine password. Using MD5 algorithm The MD5 message-digest algorithm is a widely used hash function engendering a 128-bit hash value. Albeit MD5 was initially designed to be utilized as a cryptographic hash function, it has been found to suffer from extensive susceptibilities. It can still be utilized as a checksum to verify data integrity, but only against unintentional corruption. It remains felicitous for other non-cryptographic purposes, for example for determining the partition for a particular key in a partitioned database. Node.js MD5 Example: var crypto = require( 'crypto' ); var data = "Your Password"

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