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
+21.6k Golang : Get current file path of a file or executable
+8.4k Golang : Command line file upload program to server example
+11.3k Golang : Recombine chunked files example
+4.2k Fix fatal error: evacuation not done in time problem
+10k Golang : How to pass map to html template and access the map's elements
+12.9k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+8.8k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+14k Golang : Parse date string and convert to dd-mm-yyyy format
+7.7k Golang : Sort and reverse sort a slice of floats
+17.3k Golang : Convert seconds to minutes and remainder seconds
+7.1k Golang : Quadratic example
+15.4k Golang : Count JSON objects and convert to slice/array