Go SHA-512 Hash Code Example (Online Runner)
Go SHA-512 hashing code example with runnable snippets to calculate SHA-512 for text and files.
Online calculator: use the site SHA-512 text tool.
Calculation method
Use crypto/sha512, hash UTF-8 bytes, and encode the 64-byte digest as hex.
Implementation notes
- Package: built-in
crypto/sha512. - Implementation: file hashing streams bytes to avoid loading large files into memory.
- Notes: SHA-512 is unkeyed; use HMAC for integrity or a KDF for passwords.
Text hashing example
go
package main
import (
"crypto/sha512"
"encoding/hex"
"fmt"
)
func sha512Text(text string) string {
sum := sha512.Sum512([]byte(text))
return hex.EncodeToString(sum[:])
}
func main() {
fmt.Println(sha512Text("hello world"))
}File hashing example
go
package main
import (
"crypto/sha512"
"encoding/hex"
"fmt"
"io"
"os"
)
func sha512File(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
hash := sha512.New()
if _, err := io.Copy(hash, file); err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(nil)), nil
}
func main() {
file, err := os.CreateTemp("", "sha512-example-*.bin")
if err != nil {
panic(err)
}
defer os.Remove(file.Name())
if _, err := file.Write([]byte("example payload\n")); err != nil {
panic(err)
}
if err := file.Close(); err != nil {
panic(err)
}
value, err := sha512File(file.Name())
if err != nil {
panic(err)
}
fmt.Println(value)
}Complete script (implementation + tests)
go
package main
import (
"crypto/sha512"
"encoding/hex"
"fmt"
"io"
"os"
)
func sha512Hex(data []byte) string {
sum := sha512.Sum512(data)
return hex.EncodeToString(sum[:])
}
func sha512Text(text string) string {
return sha512Hex([]byte(text))
}
func sha512File(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
hash := sha512.New()
if _, err := io.Copy(hash, file); err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(nil)), nil
}
func main() {
vectors := map[string]string{
"": "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
"abc": "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
"message digest": "107dbf389d9e9f71a3a95f6c055b9251bc5268c2be16d6c13492ea45b0199f3309e16455ab1e96118e8a905d5597b72038ddb372a89826046de66687bb420e7c",
"abcdefghijklmnopqrstuvwxyz": "4dbff86cc2ca1bae1e16468a05cb9881c97f1753bce3619034898faa1aabe429955a1bf8ec483d7421fe3c1646613a59ed5441fb0f321389f77f48a879c7b1f1",
}
for input, expected := range vectors {
if got := sha512Text(input); got != expected {
panic(fmt.Sprintf("SHA-512 mismatch for %q: %s != %s", input, got, expected))
}
}
file, err := os.CreateTemp("", "sha512-example-*.bin")
if err != nil {
panic(err)
}
defer os.Remove(file.Name())
if _, err := file.Write([]byte("example payload\n")); err != nil {
panic(err)
}
if err := file.Close(); err != nil {
panic(err)
}
fileHash, err := sha512File(file.Name())
if err != nil {
panic(err)
}
fmt.Println("sha512(file)=", fileHash)
fmt.Println("All SHA-512 test vectors passed.")
}