Documentation
¶
Overview ¶
Package crypto contains the primitives theauth-go uses for password hashing (Argon2id), opaque session and reset tokens (crypto/rand plus SHA-256), AES-256-GCM symmetric encryption for OAuth tokens at rest, PKCE code verifier and S256 challenge generation, and salted SHA-256 recovery-code hashing.
The functions in this package are safe to call from multiple goroutines. They have no internal state. AES key inputs must be exactly 32 bytes (see AESKeyLen); pass shorter or longer slices and Encrypt and Decrypt return ErrInvalidKeyLength.
Index ¶
- Constants
- Variables
- func CodeChallenge(verifier string) string
- func Decrypt(key, ciphertext []byte) ([]byte, error)
- func Encrypt(key, plaintext []byte) ([]byte, error)
- func GenerateRecoveryCode() (string, error)
- func HashPassword(plain string) (string, error)
- func HashRecoveryCode(code string) ([]byte, error)
- func HashToken(token string) []byte
- func NewCodeVerifier() (string, error)
- func NewToken() (string, error)
- func VerifyPassword(plain, phc string) (bool, error)
- func VerifyRecoveryCode(stored []byte, code string) bool
Examples ¶
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.
Example ¶
ExampleEncrypt shows a round trip with a 32 byte key. The output of Encrypt is the random 12 byte nonce concatenated with the ciphertext and the GCM auth tag.
package main
import (
"bytes"
"fmt"
"github.com/glincker/theauth-go/crypto"
)
func main() {
key := bytes.Repeat([]byte{0x42}, crypto.AESKeyLen)
plaintext := []byte("hello, theauth")
ciphertext, err := crypto.Encrypt(key, plaintext)
if err != nil {
panic(err)
}
got, err := crypto.Decrypt(key, ciphertext)
if err != nil {
panic(err)
}
fmt.Println(string(got))
}
Output: hello, theauth
func GenerateRecoveryCode ¶ added in v0.5.0
GenerateRecoveryCode draws recoveryCodeBytes from crypto/rand and returns the lowercase hex encoding (10 chars). Returned errors propagate any RNG failure; callers should bail on the whole enrollment.
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 HashRecoveryCode ¶ added in v0.5.0
HashRecoveryCode produces a 48-byte storage value laid out as salt(16) || sha256(salt || code)(32). The salt is per-code random, so two rows holding the same plaintext code produce different hashes. Pre-computed rainbow tables are defeated by the salt alone; the sha256 digest then sits behind a 2^40 brute-force barrier per row.
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.
func VerifyRecoveryCode ¶ added in v0.5.0
VerifyRecoveryCode re-hashes the supplied plaintext using the salt extracted from the stored value and compares in constant time. Returns false on any malformed input (including a stored value shorter than the salt prefix).
Types ¶
This section is empty.