Here's an example of how to create a SHA256 hash from a string in Go:
main.go399 chars24 lines
In this example, we first import the crypto/sha256
package to access the SHA256 hash algorithm. We then create a new SHA256 hash object using the sha256.New()
function.
We write our string to the hash object using the h.Write([]byte(str))
method. This method expects a byte slice, so we convert our string to a byte slice using the []byte()
type conversion.
We then calculate the hash sum using the h.Sum(nil)
method. This returns a byte slice containing the hash sum.
Lastly, we print the hash sum in hexadecimal format using %x
format specifier and Printf()
method.
gistlibby LogSnag