Golang hash sum and checksum to string tutorial and examples

17 April, 2016

The output of the function "hash.Sum()" is generated in byte code, and the output of "Checksum()" is a unsigned integer.

In order to convert bytes to a string you need the function "EncodeToString()" from the package "encoding/hex". For converting unsigned integers you need the function "FormatUint()" from the package "strconv".

Examples

Simple example byte to string

package main

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
)

func main() {
	md5HashInBytes := md5.Sum([]byte("Sum returns bytes"))
	md5HashInString := hex.EncodeToString(md5HashInBytes[:])
	fmt.Println(md5HashInString)
}

Note that you have to slice the variable in "hex.EncodeToString()"

Simple example uint to string

package main

import (
	"fmt"
	"hash/crc32"
	"strconv"
)

func main() {
	crc32InUint32 := crc32.ChecksumIEEE([]byte("Checksum returns uint32"))
	crc32InString := strconv.FormatUint(uint64(crc32InUint32), 16)
	fmt.Println(crc32InString)
}

Note that "strconv.FormatUint()" first parameter requires a uint64 so you'll need to convert the type to it, CRC64 for example already returns a uint64 so there it won't be needed. The second parameter tells what radix it should used (base), the 16 stands for base 16 or better known as hexadecimal.

Full example for most hashing functions

Here is an example on how to turn the outputs of md5, sha1, sha256, sha512, adler32, crc32, crc64, fnv to a readable string

package main

import (
	"crypto/md5"
	"crypto/sha1"
	"crypto/sha256"
	"crypto/sha512"
	"encoding/hex"
	"fmt"
	"hash/adler32"
	"hash/crc32"
	"hash/crc64"
	"hash/fnv"
	"strconv"
)

func main() {
	aStringToHash := []byte("Lets hash this sentence!")
	//For CRC
	crc32Table := crc32.MakeTable(0xD5828281)
	crc64Table := crc64.MakeTable(0xC96C5795D7870F42)
	//For FNV
	fnvHash := fnv.New32()

	//Get the hashes in bytes
	md5Bytes := md5.Sum(aStringToHash)
	sha1Bytes := sha1.Sum(aStringToHash)
	sha256Bytes := sha256.Sum256(aStringToHash)
	sha512Bytes := sha512.Sum512(aStringToHash)
	fnvBytes := fnvHash.Sum(aStringToHash)

	//Get hashes in integers
	adler32Int := adler32.Checksum(aStringToHash)
	crc32Int := crc32.Checksum(aStringToHash, crc32Table)
	crc64Int := crc64.Checksum(aStringToHash, crc64Table)

	//Print out what will be hashed
	fmt.Println(string(aStringToHash))

	//Bytes to string
	fmt.Println("MD5 String is ", hex.EncodeToString(md5Bytes[:]))
	fmt.Println("SHA1 String is ", hex.EncodeToString(sha1Bytes[:]))
	fmt.Println("SHA256 String is ", hex.EncodeToString(sha256Bytes[:]))
	fmt.Println("SHA512 String is ", hex.EncodeToString(sha512Bytes[:]))
	fmt.Println("FNV String is ", hex.EncodeToString(fnvBytes[:]))

	//Uint to string
	fmt.Println("Adler32 String is ", strconv.FormatUint(uint64(adler32Int), 16))
	fmt.Println("CRC32 String is", strconv.FormatUint(uint64(crc32Int), 16))
	fmt.Println("CRC64 String is ", strconv.FormatUint(crc64Int, 16))
}

This will output

Lets hash this sentence!
MD5 String is  ab5e77a9d7d5958c4be9e73b21ce282b
SHA1 String is  4cfa373b89284f186193b2977f51f37cadc547d7
SHA256 String is  4a015d8c56bb37ee7795f19333be513182fa46e53fa5f555e9d3989a8c30a870
SHA512 String is  fefdae7889407e38502ee4e92a3a4214ff0b6213400e48c3c216e20818eac4623f3e3658a7cf593fe1c6dcb493de670f63474963a6feb039579b9990fcd98773
FNV String is  4c657473206861736820746869732073656e74656e636521811c9dc5
Adler32 String is  6e2e08cb
CRC32 String is d4466054
CRC64 String is  92b1b074b93598f