hmacvalidator

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 14, 2025 License: Apache-2.0 Imports: 4 Imported by: 1

README

HMAC Validator Package

The hmacvalidator package provides a simple yet powerful HMAC validator in Go. It supports both SHA256 and SHA1 hashing algorithms for HMAC validation.

Features

  • Supports SHA256 and SHA1 HMAC validation.
  • Fluent API for HMAC validation.
  • Utility methods for both valid and invalid HMAC checks.

Installation

go get github.com/xorima/hmacvalidator

Usage

Initialization

Create a new HMAC validator by specifying the hash type and secret.

import "github.com/xorima/hmacvalidator"

validator := hmacvalidator.NewHMACValidator(hmacvalidator.HashSha256, "your-secret-here")
Validation

Use the IsValid method to validate the HMAC signature of a given body.

body := []byte("your-message-body-here")
signature := "sha256=your-signature-here"

if validator.IsValid(body, signature) {
fmt.Println("Valid signature!")
} else {
fmt.Println("Invalid signature!")
}

You can also use the IsInvalid method as a convenience for !IsValid.

Versioning

This project adheres to Semantic Versioning (SemVer). For the versions available, see the tags on this repository.

Development

Getting Set Up

Clone the repository:

git clone https://github.com/xorima/hmacvalidator.git

Navigate to the project directory:

cd hmacvalidator

Use the provided Makefile for common development tasks:

  • make lint: Run golangci-lint on the project.
  • make test: Run tests for the project.
Linting

This project uses golangci-lint for linting. Ensure you have it installed or use the provided Makefile to lint the project.

Testing

Run the tests using the Makefile:

make test

Contributing

Documentation

Index

Constants

View Source
const (
	// HashSha256 is the sha256 hash type.
	HashSha256 = Hash("sha256")
	// HashSha1 is the sha1 hash type.
	HashSha1 = Hash("sha1")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Hash

type Hash string

Hash is a hash type, for example sha256.

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator is an HMAC validator. It can be used to validate HMAC signatures for a given hash and secret. The zero value is not valid, use NewHMACValidator instead.

func NewHMACValidator

func NewHMACValidator(hash Hash, secret string) *Validator

NewHMACValidator returns a new HMAC validator for the given hash and secret.

func (*Validator) Generate added in v1.1.0

func (v *Validator) Generate(body []byte) string

Generate will return a signature for the given body

func (*Validator) IsInvalid

func (v *Validator) IsInvalid(body []byte, signature string) bool

IsInvalid returns true if the signature is invalid for the given body and secret. This is a convenience method for !IsValid(body, signature) Inputs body []byte - the body to validate signature string - the signature to validate the body against

func (*Validator) IsValid

func (v *Validator) IsValid(body []byte, signature string) bool

IsValid returns true if the signature is valid for the given body and secret. The signature should be in the format "sha256=xxxxx" or "sha1=xxxxx". Inputs body []byte - the body to validate signature string - the signature to validate the body against

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL