crypto

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package crypto provides cryptographic utilities for the OAuth authorization server.

Index

Constants

View Source
const MinRSAKeyBits = 2048

MinRSAKeyBits is the minimum required RSA key size in bits. NIST recommends at least 2048 bits for RSA keys.

View Source
const MinSecretLength = 32

MinSecretLength is the minimum required length for the HMAC secret in bytes. Using 256 bits (32 bytes) provides adequate security for HMAC-SHA256.

View Source
const PKCEChallengeMethodS256 = "S256"

PKCEChallengeMethodS256 is the PKCE challenge method using SHA-256 (RFC 7636).

Variables

This section is empty.

Functions

func ComputePKCEChallenge

func ComputePKCEChallenge(verifier string) string

ComputePKCEChallenge computes the code_challenge from a code_verifier using the S256 method per RFC 7636 Section 4.2. code_challenge = BASE64URL(SHA256(code_verifier))

func DeriveAlgorithm

func DeriveAlgorithm(key crypto.Signer) (string, error)

DeriveAlgorithm determines the appropriate JWT signing algorithm for the given key. Returns the algorithm string (e.g., "RS256", "ES256", "EdDSA") based on key type and parameters.

func DeriveKeyID

func DeriveKeyID(key crypto.Signer) (string, error)

DeriveKeyID computes a key ID from the public key using RFC 7638 JWK Thumbprint. The thumbprint is computed as base64url(SHA-256(JWK canonical form)).

func GeneratePKCEVerifier

func GeneratePKCEVerifier() (string, error)

GeneratePKCEVerifier generates a cryptographically random code_verifier per RFC 7636 Section 4.1. The verifier is 43 characters (32 bytes base64url encoded without padding), using characters from the base64url alphabet: [A-Z], [a-z], [0-9], "-", "_".

func LoadSigningKey

func LoadSigningKey(keyPath string) (crypto.Signer, error)

LoadSigningKey loads a private key from a PEM file. Supports both RSA (PKCS1 and PKCS8) and ECDSA (PKCS8) formats. Returns a crypto.Signer that can be used for JWT signing.

func ValidateAlgorithmForKey

func ValidateAlgorithmForKey(alg string, key crypto.Signer) error

ValidateAlgorithmForKey checks if the provided algorithm is compatible with the key type. Returns an error if the algorithm doesn't match the key type.

Types

type HMACSecrets

type HMACSecrets struct {
	// Current is the active secret used for signing new tokens.
	Current []byte
	// Rotated contains previously-used secrets for verifying existing tokens.
	Rotated [][]byte
}

HMACSecrets holds the current secret and any rotated (previous) secrets. This supports zero-downtime secret rotation for OAuth token signing.

func LoadHMACSecrets

func LoadHMACSecrets(paths []string) (*HMACSecrets, error)

LoadHMACSecrets loads HMAC secrets from file paths for rotation support. paths[0] is the current (signing) secret; paths[1:] are rotated (verification) secrets. Returns nil if paths is empty (caller should generate random secret).

type SigningKeyParams

type SigningKeyParams struct {
	// Key is the private key used for signing.
	Key crypto.Signer
	// KeyID is the key identifier (either derived from thumbprint or configured).
	KeyID string
	// Algorithm is the signing algorithm (either derived from key type or configured).
	Algorithm string
}

SigningKeyParams contains the derived or configured parameters for a signing key.

func DeriveSigningKeyParams

func DeriveSigningKeyParams(key crypto.Signer, keyID, algorithm string) (*SigningKeyParams, error)

DeriveSigningKeyParams derives or validates signing key parameters. If keyID or algorithm are empty, they are derived from the key. If they are provided, they are validated against the key type.

Jump to

Keyboard shortcuts

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