golang get file sha256
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
)
func main() {
filePath := "path/to/your/file.txt"
file, err := os.Open(filePath)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
hash := sha256.New()
if _, err := io.Copy(hash, file); err != nil {
fmt.Println("Error calculating hash:", err)
return
}
hashInBytes := hash.Sum(nil)
hashString := hex.EncodeToString(hashInBytes)
fmt.Println("SHA256 hash of", filePath, "is:", hashString)
}