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
+6.5k Golang : Detect sample rate, channels or latency with PortAudio
+17.7k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+13.3k Golang : Parsing or breaking down URL
+8.2k Golang : Scramble and unscramble text message by randomly replacing words
+9.5k Golang : Create matrix with Gonum Matrix package example
+14.2k Golang : Get current time from the Internet time server(ntp) example
+45.5k Golang : Convert int to byte array([]byte)
+6.4k Golang : Get YouTube playlist
+7.1k Golang : Reverse text lines or flip line order example
+4.9k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+16.1k Golang : Find smallest number in array
+16.9k Golang : Get download file size