Generating checksums
The hash, or so-called checksum, is the easiest way to quickly compare any content. This recipe demonstrates how to create the checksum of the file content. For demonstration purposes, the MD5 hash function will be used.
How to do it...
- Open the console and create the folder
chapter03/recipe12
. - Navigate to the directory.
- Create the
content.dat
file with the following content:
This is content to check
- Create the
checksum.go
file with the following content:
package main import ( "crypto/md5" "fmt" "io" "os" ) var content = "This is content to check" func main() { checksum := MD5(content) checksum2 := FileMD5("content.dat") fmt.Printf("Checksum 1: %s\n", checksum) fmt.Printf("Checksum 2: %s\n", checksum2) if checksum == checksum2 { fmt.Println("Content matches!!!") } } // MD5 creates the md5 ...