Go SHA-3 Hash Code Example (Online Runner)
Go SHA-3 hashing examples for SHA3-224/256/384/512 with runnable snippets and file hashing.
Online calculator: use the site SHA-3 text tool.
Note: This snippet requires locally installed dependencies and will not run in the online runner. Run it locally with:
go mod init sha3-demo && go get golang.org/x/crypto/sha3 && go run sha3_basic.go
Calculation method
Use golang.org/x/crypto/sha3 and select the desired output size (224/256/384/512).
Implementation notes
- Package:
golang.org/x/crypto/sha3. - Implementation: file hashing streams bytes to avoid loading large files into memory.
- Notes: SHA-3 is unkeyed; for keyed integrity use HMAC-SHA3.
Text hashing example
go
package main
import (
"encoding/hex"
"fmt"
"golang.org/x/crypto/sha3"
)
func sha3Text(text string) string {
sum := sha3.Sum256([]byte(text))
return hex.EncodeToString(sum[:])
}
func main() {
fmt.Println(sha3Text("hello world"))
}File hashing example
go
package main
import (
"encoding/hex"
"fmt"
"io"
"os"
"golang.org/x/crypto/sha3"
)
func sha3File(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
hash := sha3.New512()
if _, err := io.Copy(hash, file); err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(nil)), nil
}
func main() {
file, err := os.CreateTemp("", "sha3-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 := sha3File(file.Name())
if err != nil {
panic(err)
}
fmt.Println(value)
}Complete script (implementation + tests)
go
package main
import (
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"golang.org/x/crypto/sha3"
)
type Variant string
const (
SHA3_224 Variant = "sha3-224"
SHA3_256 Variant = "sha3-256"
SHA3_384 Variant = "sha3-384"
SHA3_512 Variant = "sha3-512"
)
func sha3Hex(data []byte, variant Variant) (string, error) {
switch variant {
case SHA3_224:
sum := sha3.Sum224(data)
return hex.EncodeToString(sum[:]), nil
case SHA3_256:
sum := sha3.Sum256(data)
return hex.EncodeToString(sum[:]), nil
case SHA3_384:
sum := sha3.Sum384(data)
return hex.EncodeToString(sum[:]), nil
case SHA3_512:
sum := sha3.Sum512(data)
return hex.EncodeToString(sum[:]), nil
default:
return "", errors.New("unsupported SHA-3 variant")
}
}
func sha3Text(text string, variant Variant) (string, error) {
return sha3Hex([]byte(text), variant)
}
func sha3File(path string, variant Variant) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
var hash io.Writer
var sumFunc func() []byte
switch variant {
case SHA3_224:
h := sha3.New224()
hash = h
sumFunc = func() []byte { return h.Sum(nil) }
case SHA3_256:
h := sha3.New256()
hash = h
sumFunc = func() []byte { return h.Sum(nil) }
case SHA3_384:
h := sha3.New384()
hash = h
sumFunc = func() []byte { return h.Sum(nil) }
case SHA3_512:
h := sha3.New512()
hash = h
sumFunc = func() []byte { return h.Sum(nil) }
default:
return "", errors.New("unsupported SHA-3 variant")
}
if _, err := io.Copy(hash, file); err != nil {
return "", err
}
return hex.EncodeToString(sumFunc()), nil
}
func main() {
digest, err := sha3Text("hello", SHA3_256)
if err != nil {
panic(err)
}
fmt.Println("sha3-256(hello)=", digest)
file, err := os.CreateTemp("", "sha3-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 := sha3File(file.Name(), SHA3_512)
if err != nil {
panic(err)
}
fmt.Println("sha3-512(file)=", fileHash)
}