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.7k Unix/Linux : How to open tar.gz file ?
+11.4k Golang : Secure file deletion with wipe example
+19.9k Swift : Convert (cast) Int to int32 or Uint32
+4.6k Golang : A program that contain another program and executes it during run-time
+22.5k Golang : untar or extract tar ball archive example
+16.1k Golang : Find out mime type from bytes in buffer
+9.8k Golang : Setting variable value with ldflags
+25.6k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+6.7k Golang : Normalize email to prevent multiple signups example
+12.7k Golang : Convert IPv4 address to packed 32-bit binary format
+5.9k Golang : Get missing location after unmarshal binary and gob decode time.
+5.6k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error