package main
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
)
func GenerateRandomHash(n int) (string, error) {
b := make([]byte, n)
_, err := rand.Read(b)
// Note that err == nil only if we read len(b) bytes.
if err != nil {
return "", err
}
hasher := sha256.New()
hasher.Write(b)
sha := hex.EncodeToString(hasher.Sum(nil))
return sha, nil
}
func main() {
hash, err := GenerateRandomHash(512)
if err != nil {
panic(err)
}
fmt.Println(hash)
}
// go run main.go
// 34c0fb393623843e56719b5d9d66385a55b4b4d3393187b7b1a76aee46c421c5
Here is an example of randomly generating a hash or token in Go.