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
+3.7k Detect if Google Analytics and Developer Media are loaded properly or not
+6.2k Golang : Calculate diameter, circumference, area, sphere surface and volume
+31.7k Golang : Convert []string to []byte examples
+11.3k Golang : Gorilla web tool kit secure cookie example
+11.6k Golang : Convert a rune to unicode style string \u
+9.1k Golang : Qt Yes No and Quit message box example
+11.1k CodeIgniter : Import Linkedin data
+5.5k Golang : List all packages and search for certain package
+10.6k Golang : Read until certain character to break for loop
+50.2k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+8.9k Golang : How to control fmt or log print format?