create a sha3_256 hash from a string in go

main.go
import (
    "crypto/sha3"
    "encoding/hex"
)

func main() {
    str := "hello world"
    hash := sha3.Sum256([]byte(str))
    hashStr := hex.EncodeToString(hash[:])
    fmt.Println(hashStr)
}
195 chars
12 lines

First, we import the crypto/sha3 package for the SHA3_256 hashing function and the encoding/hex package for hex encoding.

We then create a function which takes a string as input, hashes it using SHA3_256 algorithm and encodes the hash output using hex encoding.

The input string is first converted to a byte slice using []byte(str) and then passed as an argument to the sha3.Sum256() function which computes the hash.

The resulting hash is returned as a fixed-size array of bytes, which we convert to a string using hex.EncodeToString(hash[:]).

Finally, we print the hash string to the console.

gistlibby LogSnag