package main import ( "crypto/sha256" "fmt" ) func main() { data := []byte("hello world") hash := sha256.Sum256(data) fmt.Printf("SHA-256 hash of '%s': %x\n", string(data), hash) }
package main import ( "crypto/sha1" "fmt" "io" "os" ) func main() { datafile := "data.txt" hashsum := "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" // expected SHA-1 hash f, err := os.Open(datafile) if err != nil { fmt.Println(err) return } defer f.Close() h := sha1.New() if _, err := io.Copy(h, f); err != nil { fmt.Println(err) return } actualsum := fmt.Sprintf("%x", h.Sum(nil)) if actualsum == hashsum { fmt.Printf("Hash sum of '%s': %s (matched expected value)\n", datafile, hashsum) } else { fmt.Printf("Hash sum of '%s': %s (does not match expected value)\n", datafile, actualsum) } }This code reads the contents of the file "data.txt" using the `os.Open` function, then creates a new SHA-1 hash object using the `sha1.New` function. It then copies the contents of the file to the hash object using the `io.Copy` function, which writes to the `io.Writer` interface implemented by the hash object. Finally, it computes the hash sum of the file using the `Sum` method and compares it with the expected value. The above examples use hash functions and packages from the standard library, which are included in the Go language itself.