Documentation
¶
Index ¶
- Constants
- Variables
- func CodeChallenge(verifier string) string
- func Decrypt(key, ciphertext []byte) ([]byte, error)
- func Encrypt(key, plaintext []byte) ([]byte, error)
- func HashPassword(plain string) (string, error)
- func HashToken(token string) []byte
- func NewCodeVerifier() (string, error)
- func NewToken() (string, error)
- func VerifyPassword(plain, phc string) (bool, error)
Constants ¶
const AESKeyLen = 32
AESKeyLen is the required key length in bytes for Encrypt/Decrypt (AES-256).
Variables ¶
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.
var ErrInvalidKeyLength = errors.New("theauth/crypto: encryption key must be 32 bytes")
ErrInvalidKeyLength is returned when the supplied key is not exactly AESKeyLen.
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
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
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
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
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 NewCodeVerifier ¶ added in v0.3.0
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 VerifyPassword ¶ added in v0.2.0
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.