Hash functions were introduced in Chapter 4, Non-Linear Data Structures. Hash implementation in Go has crc32 and sha256 implementations. An implementation of a hashing algorithm with multiple values using an XOR transformation is shown in the following code snippet. The CreateHash function takes a byte array, byteStr, as a parameter and returns the sha256 checksum of the byte array:
//main package has examples shown
// in Go Data Structures and algorithms book
package main
// importing fmt package
import (
"fmt"
"crypto/sha1"
"hash"
)
//CreateHash method
func CreateHash(byteStr []byte) []byte {
var hashVal hash.Hash
hashVal = sha1.New()
hashVal.Write(byteStr)
var bytes []byte
bytes = hashVal.Sum(nil)
return bytes
}
In the following sections, we will discuss the different methods of hash algorithms.