security

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package security provides cryptographic utilities for session token binding and hijacking prevention. It handles HMAC-SHA256 token hashing, salt generation, and constant-time comparison to prevent timing attacks.

Index

Constants

View Source
const (
	// SHA256HexLen is the length of a hex-encoded SHA256 hash (32 bytes = 64 hex characters)
	SHA256HexLen = 64
)

Variables

This section is empty.

Functions

func GenerateSalt

func GenerateSalt() ([]byte, error)

GenerateSalt generates a cryptographically secure random salt for token hashing. Returns 16 bytes of random data from crypto/rand.

Each session should have a unique salt to provide additional entropy and prevent attacks that work across multiple sessions.

func HashToken

func HashToken(token string, secret, salt []byte) string

HashToken returns the hex-encoded HMAC-SHA256 hash of a raw bearer token string. Uses HMAC with a server-managed secret and per-session salt to prevent offline attacks if session storage is compromised.

For empty tokens (anonymous sessions) it returns the empty string, which is the sentinel value used to identify sessions created without credentials. The raw token is never stored — only the hash.

Parameters:

  • token: The bearer token to hash
  • secret: Server-managed HMAC secret (should be 32+ bytes)
  • salt: Per-session random salt (typically 16 bytes)

Security: Uses HMAC-SHA256 instead of plain SHA256 to prevent rainbow table attacks and offline brute force if session state leaks from Redis/Valkey.

Types

This section is empty.

Jump to

Keyboard shortcuts

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