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 CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+15.4k Golang : rune literal not terminated error
+6.5k Golang : When to use make or new?
+5.9k Golang : Experimenting with the Rejang script
+18.7k Golang : Display list of time zones with GMT
+8.3k Golang : How to check variable or object type during runtime?
+9.2k Golang : How to get garbage collection data?
+13.3k Golang : Read XML elements data with xml.CharData example
+32.1k Golang : Convert []string to []byte examples
+8k Golang : Routes multiplexer routing example with regular expression control
+13.2k Golang : Verify token from Google Authenticator App
+16.9k Golang : How to save log messages to file?