Golang : Generate MD5 checksum of a file
This is an update to the previous tutorial on how to generate checksum for a file, except that this tutorial uses the io.Copy
function instead of breaking up the file into chunks. io.Copy
function reads a file content until it reaches EOF and it can be used to copy the file data into a MD5 hash.
package main
import (
"crypto/md5"
"fmt"
"io"
"os"
)
func main() {
file, err := os.Open("utf8.txt")
if err != nil {
panic(err)
}
defer file.Close()
hash := md5.New()
_, err = io.Copy(hash, file)
if err != nil {
panic(err)
}
fmt.Printf("%s MD5 checksum is %x \n", file.Name(), hash.Sum(nil))
}
Sample output :
utf8.txt MD5 checksum is 5af1ca1d92340d72a29c194d3f4096e0
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 Linux/Unix/PHP : Restart PHP-FPM
+11.8k Golang : Concurrency and goroutine example
+12.5k Golang : Display list of countries and ISO codes
+7.7k Golang : Create zip/ePub file without compression(use Store algorithm)
+18.9k Golang : convert int to string
+21.4k Golang : How to get time zone and load different time zone?
+28k PHP : Convert(cast) string to bigInt
+8.2k Setting $GOPATH environment variable for Unix/Linux and Windows
+8.5k Golang : Configure Apache and NGINX to access your Go service example
+15.6k Golang : Get all local users and print out their home directory, description and group id
+7.6k Golang : Example of custom handler for Gorilla's Path usage.
+11.1k Nginx : TLS 1.2 support