github.com/saucelabs/saucectl@v0.175.1/internal/hashio/files.go (about)

     1  package hashio
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  )
     9  
    10  // SHA256 hashes the given file with crypto.SHA256 and returns the checksum as a
    11  // base-16 (hex) string.
    12  func SHA256(filename string) (string, error) {
    13  	h := sha256.New()
    14  	file, err := os.Open(filename)
    15  	if err != nil {
    16  		return "", err
    17  	}
    18  	if _, err := io.Copy(h, file); err != nil {
    19  		return "", err
    20  	}
    21  	return fmt.Sprintf("%x", h.Sum(nil)), nil
    22  }