crypto

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2026 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const AESKeyLen = 32

AESKeyLen is the required key length in bytes for Encrypt/Decrypt (AES-256).

Variables

View Source
var ErrCiphertextTooShort = errors.New("theauth/crypto: ciphertext too short")

ErrCiphertextTooShort is returned when the supplied ciphertext is shorter than the GCM nonce, meaning it cannot be a valid Encrypt output.

View Source
var ErrInvalidKeyLength = errors.New("theauth/crypto: encryption key must be 32 bytes")

ErrInvalidKeyLength is returned when the supplied key is not exactly AESKeyLen.

View Source
var ErrInvalidPasswordHash = errors.New("crypto: invalid password hash")

ErrInvalidPasswordHash is returned by VerifyPassword when the stored PHC string cannot be parsed. Callers should treat this as a server-side fault, not a credential mismatch.

Functions

func CodeChallenge added in v0.3.0

func CodeChallenge(verifier string) string

CodeChallenge derives the S256 code challenge from a verifier per RFC 7636 section 4.2: challenge = base64url(sha256(ASCII(verifier))), no padding. Callers send this value as code_challenge with code_challenge_method=S256 on the authorize request, then send the original verifier as code_verifier on the token exchange. The provider recomputes and compares.

func Decrypt added in v0.3.0

func Decrypt(key, ciphertext []byte) ([]byte, error)

Decrypt reverses Encrypt: it splits off the 12-byte nonce prefix, then authenticates and decrypts the remainder. Returns an error if the key is the wrong length, the ciphertext is too short, or the authentication tag fails (wrong key or tampered ciphertext).

func Encrypt added in v0.3.0

func Encrypt(key, plaintext []byte) ([]byte, error)

Encrypt seals plaintext with AES-256-GCM and returns nonce || ciphertext. The 12-byte nonce is generated from crypto/rand and prepended so that Decrypt can split it back out without out-of-band metadata. key must be exactly 32 bytes (AES-256); other lengths return ErrInvalidKeyLength.

func HashPassword added in v0.2.0

func HashPassword(plain string) (string, error)

HashPassword returns an argon2id PHC-formatted hash of the plain password. Format:

$argon2id$v=19$m=<KiB>,t=<iters>,p=<threads>$<base64(salt)>$<base64(hash)>

Salt is freshly random per call — two hashes of the same password differ.

func HashToken

func HashToken(token string) []byte

HashToken returns sha256(token) as raw bytes — store this, never the token itself.

func NewCodeVerifier added in v0.3.0

func NewCodeVerifier() (string, error)

NewCodeVerifier returns a fresh PKCE code verifier suitable for an authorization code exchange per RFC 7636. The result is 43 base64url (no-pad) characters of cryptographically random data, which sits at the low end of the [43,128] legal range.

func NewToken

func NewToken() (string, error)

NewToken returns a 32-byte cryptographically random token, URL-safe-base64-encoded.

func VerifyPassword added in v0.2.0

func VerifyPassword(plain, phc string) (bool, error)

VerifyPassword parses the PHC string and constant-time-compares the supplied plain password against it. Returns (true, nil) on match, (false, nil) on mismatch, and (false, ErrInvalidPasswordHash) on a malformed hash.

Types

This section is empty.

Jump to

Keyboard shortcuts

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