Golang : md5 hash of a string
This is the simplified version of the previous tutorial on generating checksum in Go. The aim is to learn about generating md5 hash from a string instead of a file.
package main
import (
"fmt"
"io"
"crypto/md5"
)
func main() {
hash := md5.New()
io.WriteString(hash, "I don't want anyone else to know about this string message!") // append into the hash
fmt.Printf("%x", hash.Sum(nil))
}
Execute >go run md5str.go
and you will see
f684c935121d80cc91398db196fef6b1
as the output. To change the crypto algorithm, just replace md5 with SHA1, SHA256 easily by changing the crypto package.
Hope you can learn something here.
Reference :
See also : Generate checksum for a file in Go
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.9k Golang : Shuffle array of list
+10.1k Golang : Compare files modify date example
+23.4k Golang : Read a file into an array or slice example
+13.1k Golang : How to calculate the distance between two coordinates using Haversine formula
+8.7k Golang : On lambda, anonymous, inline functions and function literals
+8.4k Golang : How to check variable or object type during runtime?
+32.3k Golang : Math pow(the power of x^y) example
+11k Golang : Read until certain character to break for loop
+4.9k Nginx and PageSpeed build from source CentOS example
+16.4k Golang : Test floating point numbers not-a-number and infinite example
+25.9k Golang : Convert IP address string to long ( unsigned 32-bit integer )