0.00

Hash methods in Go Lang: SHA512, SHA384, SHA256, SHA1, MD5 - HEX encoded

Sample hex Hash functions for Go Lang


programming language: go lang 1.8 or later
operating system: any
Updated:


Method definition: SHA512 / Hex - String

import (
	"strings"
	"encoding/hex"
	"crypto/sha512"
)

func Sha512Hex(str string) string {

	hash := sha512.New()
	hash.Write([]byte(str))

//	return strings.ToLower(fmt.Sprintf("%x", hash.Sum(nil))) // as an alternative, can do use `fmt` (slower) instead of `hex`
	return strings.ToLower(hex.EncodeToString(hash.Sum(nil)))

}



Method definition: SHA384 / Hex - String

import (
	"strings"
	"encoding/hex"
	"crypto/sha512"
)

func Sha384Hex(str string) string {

	hash := sha512.New384() // SHA384 is a subset of SHA512
	hash.Write([]byte(str))

//	return strings.ToLower(fmt.Sprintf("%x", hash.Sum(nil))) // as an alternative, can do use `fmt` (slower) instead of `hex`
	return strings.ToLower(hex.EncodeToString(hash.Sum(nil)))

}



Method definition: SHA256 / Hex - String

import (
	"strings"
	"encoding/hex"
	"crypto/sha256"
)

func Sha256Hex(str string) string {

	hash := sha256.New()
	hash.Write([]byte(str))

//	return strings.ToLower(fmt.Sprintf("%x", hash.Sum(nil))) // as an alternative, can do use `fmt` (slower) instead of `hex`
	return strings.ToLower(hex.EncodeToString(hash.Sum(nil)))

}



Method definition: SHA1 / Hex - String

import (
	"strings"
	"encoding/hex"
	"crypto/sha1"
)

func Sha1Hex(str string) string {

	hash := sha1.New()
	hash.Write([]byte(str))

//	return strings.ToLower(fmt.Sprintf("%x", hash.Sum(nil))) // as an alternative, can do use `fmt` (slower) instead of `hex`
	return strings.ToLower(hex.EncodeToString(hash.Sum(nil)))

}



Method definition: MD5 / Hex - String

import (
	"strings"
	"encoding/hex"
	"crypto/md5"
	"io"
)

func Md5Hex(str string) string {

	hash := md5.New()
	io.WriteString(hash, str)

//	return strings.ToLower(fmt.Sprintf("%x", hash.Sum(nil))) // as an alternative, can do use `fmt` (slower) instead of `hex`
	return strings.ToLower(hex.EncodeToString(hash.Sum(nil)))

}