January 2018
Intermediate to advanced
340 pages
8h 6m
English
If a file is small enough to be contained in memory, the ReadFile() method works quickly. It loads the whole file into memory and then digests the data. The sum will be calculated with multiple different hash algorithms for demonstration:
package mainimport ( "crypto/md5" "crypto/sha1" "crypto/sha256" "crypto/sha512" "fmt" "io/ioutil" "log" "os")func printUsage() { fmt.Println("Usage: " + os.Args[0] + " <filepath>") fmt.Println("Example: " + os.Args[0] + " document.txt")}func checkArgs() string { if len(os.Args) < 2 { printUsage() os.Exit(1) } return os.Args[1]}func main() { filename := checkArgs() // Get bytes from file data, err := ioutil.ReadFile(filename) if err != nil { log.Fatal(err) } // Hash the file and output ...Read now
Unlock full access