Skip to content

Instantly share code, notes, and snippets.

@veggiemonk
Created July 3, 2022 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veggiemonk/9f563f3de66106466e19b7ab6d888851 to your computer and use it in GitHub Desktop.
Save veggiemonk/9f563f3de66106466e19b7ab6d888851 to your computer and use it in GitHub Desktop.
Go Snippets: hashing strings with SHA256 algorithm
package main
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
)
// Go Playground https://go.dev/play/p/1EaM_5NQmc5
func main() {
fmt.Println("There are limitations to the programs that can be run in the playground")
fmt.Println(HashSHA256([]string{"There are limitations to the programs that can be run in the playground"}))
ss := []string{
"The playground can use most of the standard library, with some exceptions. The only communication a playground program has to the outside world is by writing to standard output and standard error.",
"In the playground the time begins at 2009-11-10 23:00:00 UTC (determining the significance of this date is an exercise for the reader). This makes it easier to cache programs by giving them deterministic output.",
"There are also limits on execution time and on CPU and memory usage.",
}
fmt.Println(ss)
fmt.Println(HashSHA256(ss))
}
func HashSHA256(ss []string) string {
var sb bytes.Buffer
for _, s := range ss {
sb.WriteString(s)
}
b := sb.Bytes()
sum := sha256.Sum256(b)
return hex.EncodeToString(sum[:])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment