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

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"
"fmt"
)

func main() {

//your secret text
password := "This is my password"

//Hashed value of password
hash := MD5(password)

//Print hashed value on the console
fmt.Println("Hashed value:", hash)
}

// MD5 hashes using md5 algorithm
func MD5(text string) string {
algorithm := md5.New()
algorithm.Write([]byte(text))
return hex.EncodeToString(algorithm.Sum(nil))
}

Output:




SHA-1 hash in Go

In cryptography, SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash function that takes an input and produces a 160-bit (20-byte) hash value known as a message digest – typically rendered as a hexadecimal number, 40 digits long. It was designed by the United States National Security Agency and is a U.S. Federal Information Processing Standard.

Example: Go-SHA-1 to hash a String

package main

//Required imports
import (
"crypto/sha1"
"encoding/hex"
"fmt"
)

func main() {

//your secret text
password := "This is my password"

//Hashed value of password
hash := SHA_1(password)

//Print hashed value on the console
fmt.Println("Hashed value:", hash)
}

// SHA-1 hashes using SHA-1 algorithm
func SHA_1(text string) string {
algorithm := sha1.New()
algorithm.Write([]byte(text))
return hex.EncodeToString(algorithm.Sum(nil))
}

Output:




SHA-256 hash in Go

The SHA (Secure Hash Algorithm) is one of a number of cryptographic hash functions. A cryptographic hash is like a signature for a text or a data file. SHA-256 algorithm generates an almost-unique, fixed-size 256-bit (32-byte) hash. Hash is a one-way function – it cannot be decrypted back.

Example: Go-SHA-256 to hash a String

package main

//Required imports
import (
"crypto/sha256"
"encoding/hex"
"fmt"
)

func main() {

//your secret text
password := "This is my password"

//Hashed value of password
hash := SHA_256(password)

//Print hashed value on the console
fmt.Println("Hashed value:", hash)
}

// SHA-256 hashes using SHA-256 algorithm
func SHA_256(text string) string {
algorithm := sha256.New()
algorithm.Write([]byte(text))
return hex.EncodeToString(algorithm.Sum(nil))
}

Output:




SHA-512 hash in Go

SHA-512 is a function of the cryptographic algorithm SHA-2, which is an evolution of the famous SHA-1.
SHA-512 is very close to Sha-256 except that it used 1024 bits "blocks", and accept as input a 2^128 bits maximum length string. SHA-512 also has other algorithmic modifications in comparison with Sha-256.

Example: Go-SHA-512 to hash a String

package main

//Required imports
import (
"crypto/sha512"
"encoding/hex"
"fmt"
)

func main() {

//your secret text
password := "This is my password"

//Hashed value of password
hash := SHA_512(password)

//Print hashed value on the console
fmt.Println("Hashed value:", hash)
}

// SHA-512 hashes using SHA-512 algorithm
func SHA_512(text string) string {
algorithm := sha512.New()
algorithm.Write([]byte(text))
return hex.EncodeToString(algorithm.Sum(nil))
}

Output:


 Author name,
                                                                                Seril

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring boot video streaming example-HTML5

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

Registration and Login with Spring Boot + Spring Security + Thymeleaf

DataTable-Pagination example with Spring boot, jQuery and ajax

Spring Webflux File Download - REST API Example

Node JS mini projects with source code - free download

ReactJS, Spring Boot JWT Authentication Example