SHA3 Hash in Golang (256-bit)

You can use the golang.org/x/crypto/sha3 [docs] package to perform SHA-3 encoding on a string. We have an example function below, it will take your string and write it as a hash. Then convert that binary back into a hexadecimal string using Sprintf.

Advantages of Using SHA3

  1. Security: SHA-3 was designed to provide higher security compared to SHA-2 by using a different construction called “sponge construction”, which makes it more resistant to various types of attacks, such as collision and preimage attacks.

  2. Performance: SHA-3 is designed to perform well on a wide range of hardware platforms, from small embedded devices to large servers. This makes it a good choice for a variety of applications where performance is a concern.

  3. Flexibility: SHA-3 comes in several variants with different output sizes, such as 224, 256, 384, and 512 bits. This allows users to choose the right size for their specific needs, balancing security and performance.

Example:

Install the package:

1
go get "golang.org/x/crypto/sha3"

Here’s an example of how to use it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package main

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

func main() {

	input := "welcome to gophercoding.com"

	output := sha3Hash(input)

	fmt.Println(input, "=", output)
}


func sha3Hash(input string) string {

	// Create a new hash & write input string
	hash := sha3.New256()
	_, _ = hash.Write([]byte(input))

	// Get the resulting encoded byte slice
	sha3 := hash.Sum(nil)

	// Convert the encoded byte slice to a string
	return fmt.Sprintf("%x", sha3)
}

Example In Action

golang write to csv