keccak257 hash in go

To compute the Keccak-256 hash in Go, you can use the golang.org/x/crypto/sha3 package. This package provides the implementation of the Keccak hash function.

Here is an example function that calculates the Keccak-256 hash of a given input string:

main.go
package main

import (
	"fmt"
	"golang.org/x/crypto/sha3"
)

func keccak256(input string) []byte {
	hash := sha3.NewShake256()
	hash.Write([]byte(input))
	return hash.Sum(nil)
}

func main() {
	input := "Hello, World!"
	hash := keccak256(input)
	fmt.Printf("Keccak-256 hash of '%s': %x\n", input, hash)
}
305 chars
19 lines

In the example above, the keccak256 function takes an input string and returns the Keccak-256 hash of it as a byte slice. The main function demonstrates how to use the keccak256 function.

To use this code, make sure you have the golang.org/x/crypto/sha3 package installed. You can install it by running the following command:

go get golang.org/x/crypto/sha3
32 chars
2 lines

Then, you can compile and run the code using the go run command:

go run main.go
15 chars
2 lines

This will output the Keccak-256 hash of the input string "Hello, World!".

Note that this example uses Keccak-256, which is a specific variant of the Keccak hash function. If you specifically need Keccak-257, you may need to look for a different package or modify the code accordingly.

related categories

gistlibby LogSnag