crypto

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package crypto provides the low-level primitives authit's service packages build on: password hashing, opaque token generation/hashing, TOTP enrollment/validation, and secret-at-rest encryption. Nothing here touches storage or transport.

Index

Constants

View Source
const DefaultBcryptCost = bcrypt.DefaultCost

DefaultBcryptCost matches bcrypt's own default and is what HashPassword uses unless overridden via HashPasswordWithCost.

Variables

View Source
var ErrInvalidKeySize = errors.New("authit/crypto: key must be 32 bytes")

ErrInvalidKeySize is returned by EncryptSecret/DecryptSecret when the key is not 32 bytes (AES-256).

Functions

func CheckPassword

func CheckPassword(password, hash string) bool

CheckPassword reports whether password matches the given bcrypt hash.

func DecryptSecret

func DecryptSecret(key []byte, ciphertext []byte) (string, error)

DecryptSecret reverses EncryptSecret.

func EncryptSecret

func EncryptSecret(key []byte, plaintext string) ([]byte, error)

EncryptSecret encrypts plaintext (e.g. a TOTP secret) with AES-256-GCM under key, so it can be stored at rest without exposing it to anyone with read access to the database alone.

func GenerateBackupCodes

func GenerateBackupCodes(n int) ([]string, error)

GenerateBackupCodes returns n single-use recovery codes in plaintext. The caller is responsible for showing them to the user exactly once and persisting only their hashes via HashBackupCode.

func GenerateOpaqueToken

func GenerateOpaqueToken() (raw string, hash string, err error)

GenerateOpaqueToken returns a fresh cryptographically random token (used for refresh tokens, password reset links, email verification links, and pending-2FA sessions) together with its hash. Only the hash should ever be persisted; the raw value is returned to the caller exactly once.

func GenerateTOTPSecret

func GenerateTOTPSecret(issuer, accountName string) (*otp.Key, error)

GenerateTOTPSecret creates a new base32 TOTP secret for the given account (typically the user's email) under issuer (typically the host application's name).

func GenerateUserCode

func GenerateUserCode() (string, error)

GenerateUserCode returns an 8-character device-flow user code formatted as "XXXX-XXXX" (RFC 8628 §6.1's recommended shape), e.g. "WDJB-MJHT". At ~34.5 bits of entropy it is deliberately low relative to the device_code — the security property comes from rate-limiting guesses at the verification endpoint, not from the code's entropy alone.

func HashBackupCode

func HashBackupCode(code string) string

HashBackupCode hashes a backup code for storage/comparison, using the same opaque-token hash as everywhere else in authit.

func HashPassword

func HashPassword(password string) (string, error)

HashPassword hashes a plaintext password for storage.

func HashPasswordWithCost

func HashPasswordWithCost(password string, cost int) (string, error)

HashPasswordWithCost hashes a plaintext password using an explicit bcrypt cost, e.g. a lower cost in tests to keep them fast.

func HashToken

func HashToken(raw string) string

HashToken hashes a raw opaque token for lookup/storage.

func NewID

func NewID() (string, error)

NewID returns a random UUIDv4 string. authit's service packages use this to assign IDs before handing records to a Store, so stores never need to generate identifiers themselves.

func ValidateTOTPCode

func ValidateTOTPCode(secret, code string) bool

ValidateTOTPCode reports whether code is a currently-valid TOTP code for secret.

Types

This section is empty.

Jump to

Keyboard shortcuts

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