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
+7.2k Golang : Accessing dataframe-go element by row, column and name example
+11.2k Golang : Post data with url.Values{}
+7.4k Golang : Dealing with struct's private part
+27.3k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+22.5k Golang : Strings to lowercase and uppercase example
+13.9k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+26.6k Golang : Convert file content into array of bytes
+22.4k Golang : Convert Unix timestamp to UTC timestamp
+12.1k Golang : List running EC2 instances and descriptions
+9.5k Javascript : Read/parse JSON data from HTTP response
+9.8k Golang : Function wrapper that takes arguments and return result example
+19.8k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader