Checksum Validation In Go

md5 sha256 checksum golang

In today’s snippet, let’s look at how to generate checksum of a string or a file in Go. A checksum is a cryptographic signature of a file or a string. It is usually used to check if a file or stream of bits has been tampered or modified. If the file is modified, the checksum for the file changes. There are various cryptographic algorithms available to generate the checksum of a file or string, the popular algorithms are md5 and sha256. Let’s look at how we can generate these checksums in Go.

Using crypto/md5 for md5 checksum


package main

import (
	"crypto/md5"
	"fmt"
)

func main() {
	dataOriginal := "check the validity of the string."
	md5Original := md5.Sum([]byte(dataOriginal))
	fmt.Printf("%x\n", md5Original)

	dataTampered := "check the validity of the string" // missing full-stop at the end
	md5Tampered := md5.Sum([]byte(dataTampered))
	fmt.Printf("%x\n", md5Tampered)

	if md5Original != md5Tampered {
		fmt.Println("data has been tampered")
	} else {
		fmt.Println("data has not been tampered")
	}
}

/*

Output:

b0e2955f34bd91daea2ac671cf9c0cac
0d5f102617870d313affe12144c7f005
data has been tampered

Program exited.

*/

Try it out in https://go.dev/play/p/XEeaps2P3I9

Using crypto/sha256 for sha256 checksum


package main

import (
	"crypto/sha256"
	"fmt"
)

func main() {
	dataOriginal := "check the validity of the string."
	sha256Original := sha256.Sum256([]byte(dataOriginal))
	fmt.Printf("%x\n", sha256Original)

	dataTampered := "check the validity of the string" // missing full-stop at the end
	sha256Tampered := sha256.Sum256([]byte(dataTampered))
	fmt.Printf("%x\n", sha256Tampered)

	if sha256Original != sha256Tampered {
		fmt.Println("data has been tampered")
	} else {
		fmt.Println("data has not been tampered")
	}
}

/*

Output:

9ce938d94b960c922152bd11b7229fbae1593a62fd71dd1635800b7317aeb675
e854ac17edc88cbc0554a3d8318d11a39ac355ce776bc5c5845b952911fa893f
data has been tampered

Program exited.

*/

Try it out in https://go.dev/play/p/KbPFwbVqlwH

Checksum of a file


package main

import (
	"crypto/md5"
	"crypto/sha256"
	"fmt"
	"io"
	"log"
	"os"
)

func main() {
	f, err := os.Open("secret.txt")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	fmt.Printf("md5 checksum: %s\n", checksumMD5(f))
	fmt.Printf("sha256 checksum: %s\n", checksumSHA256(f))
}

func checksumMD5(f io.Reader) string {
	h := md5.New()
	if _, err := io.Copy(h, f); err != nil {
		log.Fatal(err)
	}

	return fmt.Sprintf("%x", h.Sum(nil))
}

func checksumSHA256(f io.Reader) string {
	h := sha256.New()
	if _, err := io.Copy(h, f); err != nil {
		log.Fatal(err)
	}

	return fmt.Sprintf("%x", h.Sum(nil))
}

/*

Output:

 go run main.go 
md5 checksum: 408818265c03bda82e531d6f575183d7
sha256 checksum: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

Check out the source code here: https://github.com/abvarun226/blog-source-code/tree/master/checksum-validation-in-go

Get new posts by email

Comments

comments powered by Disqus