To hash a string using SHA-512 in Go, you can use the crypto/sha512 package provided by the Go standard library. Here's an example code snippet that demonstrates how you can do it:
package main
import (
"crypto/sha512"
"encoding/hex"
"fmt"
)
func main() {
str := "Hello, World!"
// Create a new SHA-512 hash object
hash := sha512.New()
// Write the string to the hash object
hash.Write([]byte(str))
// Calculate the hash sum
hashSum := hash.Sum(nil)
// Convert the hash sum to a hexadecimal string
hashStr := hex.EncodeToString(hashSum)
fmt.Println("Original String:", str)
fmt.Println("SHA-512 Hash:", hashStr)
}
In this example, we import the crypto/sha512
package to access the SHA-512 hashing algorithm. We create a new hash object using sha512.New()
, then write the string to the hash object using hash.Write()
. Next, we calculate the hash sum by calling hash.Sum(nil)
. Finally, we convert the hash sum to a hexadecimal string using hex.EncodeToString()
. The result prints out the original string and its corresponding SHA-512 hash.