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
+10k How to test Facebook App on localhost ?
+4.2k Python : Convert(cast) string to bytes example
+29.2k Golang : Interpolating or substituting variables in string examples
+9.6k Golang : Command line file upload program to server example
+5.1k PageSpeed : Clear or flush cache on web server
+16.2k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+24.5k Golang : Get executable name behind process ID example
+6.3k Golang : alternative to os.Exit() function
+18.8k Golang : Read directory content with os.Open
+3.6k Javascript : Access JSON data example
+6.7k Golang : Ways to recover memory during run time.
+6.5k Golang : Gorrila set route name and get the current route name