How to calculate the MD5 checksum of a file with Go

The MD5 hashing algorithm is commonly used to verify the integrity of a file, allowing it to generate a unique "fingerprint" for the data contained within it. In Go, calculating the MD5 checksum of a file is quite simple thanks to the standard library packages. In this article we will see how to read a file and calculate its MD5 hash.

To calculate the MD5 checksum of a file, we will use the packages crypto/md5 for the hashing algorithm and io to manage the file reading.


package main

import (
  "crypto/md5"
  "encoding/hex"
  "fmt"
  "io"
  "os"
)

// Function to calculate the MD5 checksum of a file
func calculateMD5(filePath string) (string, error) {
  // Open the file
  file, err := os.Open(filePath)
  if err != nil {
    return "", fmt.Errorf("error opening file: %w", err)
  }
  defer file.Close()

  // Create a new MD5 hash
  hash := md5.New()

  // Copy the file contents to the hash
  if _, err := io.Copy(hash, file); err != nil {
    return "", fmt.Errorf("error calculating MD5 hash: %w", err)
  }

  // Calculate the checksum in hexadecimal format
  checksum := hex.EncodeToString(hash.Sum(nil))
  return checksum, nil
}

func main() {
  // Specify the path of the file to calculate the checksum
  filePath := "testfile.txt"

  // Calculate and print the MD5 checksum
  checksum, err := calculateMD5(filePath)
  if err != nil {
    fmt.Println("Error:", err)
    return
  }

  fmt.Printf("The MD5 checksum of file %s is: %s\n", filePath, checksum)
}

Explanation:

  1. Opening the file: The calculateMD5 function takes the file path as a parameter and opens it using os.Open(). On error, it returns a formatted error.

  2. Create an MD5 hash: We use md5.New() to create a new MD5 hash instance.

  3. Read the file and update the hash: The contents of the file are read and passed to the hash using io.Copy(), which copies the data directly into the hash buffer.

  4. Generate the hex checksum: Once the hash is populated, we use hash.Sum(nil) to get the raw hash value, then hex.EncodeToString() to convert it to a readable hex string.

  5. Return the checksum: The function returns the MD5 checksum as a string hexadecimal.

Conclusion

The MD5 checksum can be useful for quickly comparing whether two files are identical or whether a file has been altered. Although it is no longer used for cryptographic purposes, it is still a quick and easy way to verify the integrity of files. With Go, this process is extremely simple thanks to its standard library and the ability to easily manage files and hashing algorithms.

Back to top