hs256

package
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 4 Imported by: 0

README

jwt/hs256

HMAC-SHA256 implementation of core/jwt.Signer and core/jwt.Verifier. Use with core/jwt.AuthService for typed-payload tokens, or via the standalone CreateToken / ParseToken helpers.

See core/jwt for the framework overview and Signer/Verifier interfaces.

Quickstart

import (
    "github.com/sergeyslonimsky/core/jwt"
    "github.com/sergeyslonimsky/core/jwt/hs256"
)

signer   := hs256.NewSigner([]byte("super-secret"))
verifier := hs256.NewVerifier([]byte("super-secret"))

svc := jwt.NewAuthService[MyAuthInfo](signer, verifier, "myservice", "user", time.Hour)

Standalone helpers

For callers that don't use AuthService:

token, err := hs256.CreateToken(secret, claims)
claims, err := hs256.ParseToken[MyClaims](token, secret)

Errors

Error Cause
ErrInvalidSigningMethod Token signed with an algorithm other than HS256.
ErrInvalidClaimsType Generic ParseToken called with a non-pointer claims type.
ErrInvalidToken Token failed validation (expired, signature mismatch, etc.).

Algorithm details

  • HS256 (HMAC-SHA256): symmetric. The same secret is used to sign and verify.
  • Suitable for service-to-service auth where both ends share a secret. Not appropriate where the verifier should not be able to mint tokens — use RSA / EdDSA in that case (planned, see core/jwt).

Documentation

Overview

Package hs256 implements jwt.Signer and jwt.Verifier using the HS256 HMAC-SHA256 signing algorithm.

Use Signer/Verifier with the generic core/jwt.AuthService:

signer := hs256.NewSigner([]byte("secret"))
verifier := hs256.NewVerifier([]byte("secret"))
svc := jwt.NewAuthService[MyAuthInfo](signer, verifier, "myservice", "user", time.Hour)

The package also exposes the older generic CreateToken/ParseToken helpers for callers that don't need AuthService.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidSigningMethod = errors.New("unexpected signing method")
	ErrInvalidClaimsType    = errors.New("invalid claims type")
	ErrInvalidToken         = errors.New("invalid token")
)

Sentinel errors returned by ParseToken / Verifier.

Functions

func CreateToken

func CreateToken[T jwt.Claims](secret []byte, claims T) (string, error)

CreateToken is a generic helper that signs the given claims with HS256. Useful when AuthService's payload model doesn't fit your use case.

func ParseToken

func ParseToken[T jwt.Claims](tokenString string, secret []byte) (T, error)

ParseToken is a generic helper that validates a token and returns the populated claims.

Types

type Signer added in v1.3.0

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

Signer signs JWT claims using HS256 with a shared secret. Implements core/jwt.Signer.

func NewSigner added in v1.3.0

func NewSigner(secret []byte) *Signer

NewSigner returns an HS256 jwt.Signer that uses the given shared secret.

func (*Signer) Sign added in v1.3.0

func (s *Signer) Sign(claims jwt.Claims) (string, error)

Sign — see core/jwt.Signer.Sign.

type Verifier added in v1.3.0

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

Verifier validates HS256-signed JWTs against a shared secret. Implements core/jwt.Verifier.

func NewVerifier added in v1.3.0

func NewVerifier(secret []byte) *Verifier

NewVerifier returns an HS256 jwt.Verifier that validates against the given shared secret.

func (*Verifier) Verify added in v1.3.0

func (v *Verifier) Verify(tokenString string, dst jwt.Claims) error

Verify — see core/jwt.Verifier.Verify.

Jump to

Keyboard shortcuts

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