Skip to content

gymshark/go-hasher

Repository files navigation

Go Hasher

A wrapper around some common hashing functions with customizable encoding

If any issues/bugs are found please raise an issue on github here: Issue Tracker


Codacy Badge Codacy Badge GoReportCard Badge Issues Open Badge License Go version

Docs

Online: https://pkg.go.dev/github.com/gymshark/go-hasher

Local: godoc -http=:6060

Getting started

import "github.com/gymshark/go-hasher"

Basic usage

import "github.com/gymshark/go-hasher"

func main() {
    h := hasher.Sha256([]byte("hello world.")).Base64()
    fmt.Println(h) //base64 encoded hash
}

Hmac usage

import "github.com/gymshark/go-hasher"

func main() {
    secretKey := "secretKey"
    hmac := hasher.HmacSha512([]byte("hello world."), secretKey).Base64()
    fmt.Println(hmac) //base64 encoded hmac
    
    //securely check for equality
    eq := hasher.Equal([]byte(hmac), []byte("random-invalid-hmac"))

    //Output: false
    fmt.Println(eq)
}

Custom Encoder

This allows you to pass a func with the signature of func ([]byte) string to the encoder when the lib may not supply a helper function for your needs

import "github.com/gymshark/go-hasher"

func main() {
	encodingFn := func(b []byte) string {
        //encode the bytes however you want
        encodedstring := string(b)
        return encodedstring
    }
	
    h := hasher.Sha1([]byte("hello world.")).Encode(encodingFn)
    fmt.Println(h) //encoded hash result of custom function
}

Functions

Hash functions

Md5([]byte)
Sha1([]byte)
Sha256([]byte)
Sha512([]byte)
Sha3([]byte)

Hmac functions

HmacMd5([]byte, string)
HmacSha1([]byte, string)
HmacSha256([]byte, string)
HmacSha512([]byte)

There is no Sha3 hmac function due to Sha3 being resistant to length extension attacks, you can append your secret key to your data then use Sha3([]byte) to generate your hash data.

𝑚𝑎𝑐=SHA3(𝑘||𝑚) is a secure MAC if 𝑘 is a fixed-length key. This is an explicit design goal of SHA3.

Encoding functions

Hex() // base16
Base32()
Base64()
Base64UrlSafe()
Encode(func([]byte) string) //allows custom encoding