Created
July 3, 2022 12:50
-
-
Save veggiemonk/9f563f3de66106466e19b7ab6d888851 to your computer and use it in GitHub Desktop.
Go Snippets: hashing strings with SHA256 algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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