How to SHA256 a String in Golang


If you need to SHA256 a String in Go, then you can use the crypto/sha256 package.

SHA256 a String in Go

package main

import (
	"crypto/sha256"
	"fmt"
)

func main() {
	s := "A sample string to SHA256!"

	h := sha256.New()

	h.Write([]byte(s))

	bs := h.Sum(nil)

	fmt.Println(s)
	fmt.Printf("%x\n", bs)
}

The output will look like this:

A sample string to SHA256!
9abf637c7e39cc4ef84d6d92cf7ffe168dc922b8ae666260d907e0353865ce89