Sling Academy
Home/Golang/How to Hash Files in Go with SHA-1, SHA-256, and SHA-512

How to Hash Files in Go with SHA-1, SHA-256, and SHA-512

Last updated: November 27, 2024

Hashing plays a crucial role in ensuring the integrity and authenticity of files. In Go, the crypto package provides easy-to-use functions to hash files using SHA-1, SHA-256, and SHA-512 algorithms. This guide will walk you through hashing files with each of these algorithms.

Hashing Files with SHA-1

The SHA-1 algorithm produces a 160-bit hash value known as a message digest. While it is not the most secure, it is still widely used for integrity checks. Below is how you can compute SHA-1 hash of a file:

package main

import (
	"crypto/sha1"
	"fmt"
	"io"
	"log"
	"os"
)

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

	hash := sha1.New()
	if _, err := io.Copy(hash, file); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("SHA-1 checksum: %x\n", hash.Sum(nil))
}

Hashing Files with SHA-256

SHA-256 is a member of the SHA-2 cryptographic hash functions and provides better security. It creates a 256-bit (32-byte) hash value. Here is how you can compute it:

package main

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

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

	hash := sha256.New()
	if _, err := io.Copy(hash, file); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("SHA-256 checksum: %x\n", hash.Sum(nil))
}

Hashing Files with SHA-512

The SHA-512 algorithm is another member of the SHA-2 family, providing even stronger security with a 512-bit (64-byte) hash value. Below is the example:

package main

import (
	"crypto/sha512"
	"fmt"
	"io"
	"log"
	"os"
)

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

	hash := sha512.New()
	if _, err := io.Copy(hash, file); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("SHA-512 checksum: %x\n", hash.Sum(nil))
}

Conclusion

By using the crypto/sha1, crypto/sha256, and crypto/sha512 from Go's standard library, you can easily compute the hash for any file, providing essential functionalities for file integrity verification.

Next Article: Working with JSON Web Tokens (JWT) in Go for Authentication

Previous Article: PBKDF2 in Go: Strengthening Password Security

Series: Cryptography and Security in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant