Documentation
¶
Index ¶
- Constants
- type HashAlgorithm
- type TinyMfa
- func (tinymfa *TinyMfa) BuildPayload(issuer, username string, secret *string, digits uint8, algorithm HashAlgorithm, ...) string
- func (tinymfa *TinyMfa) CalculateHMAC(message []byte, key *[]byte, algorithm HashAlgorithm) ([]byte, error)
- func (tinymfa *TinyMfa) ConvertColorSetting(setting structs.ColorSetting) color.Color
- func (tinymfa *TinyMfa) GenerateExtendedSecretKey() (*[]byte, error)
- func (tinymfa *TinyMfa) GenerateMessage(timestamp int64, offsetType uint8, timeStep int64, t0 int64) (int64, error)
- func (tinymfa *TinyMfa) GenerateMessageBytes(message int64) ([]byte, error)
- func (tinymfa *TinyMfa) GenerateQrCode(issuer, user string, secret *string, digits uint8, algorithm HashAlgorithm, ...) ([]byte, error)
- func (tinymfa *TinyMfa) GenerateSecretKey(size int8) (*[]byte, error)
- func (tinymfa *TinyMfa) GenerateSecretKeyForAlgorithm(algorithm HashAlgorithm) (*[]byte, error)
- func (tinymfa *TinyMfa) GenerateStandardSecretKey() (*[]byte, error)
- func (tinymfa *TinyMfa) GenerateSuperbSecretKey() (*[]byte, error)
- func (tinymfa *TinyMfa) GenerateToken(unixTimestamp int64, key *[]byte, offsetType uint8, tokenlength uint8, ...) (int, error)
- func (tinymfa *TinyMfa) GetQRCodeConfig() structs.QrCodeConfig
- func (tinymfa *TinyMfa) SetQRCodeConfig(qrcodeConfig structs.QrCodeConfig)
- func (tinymfa *TinyMfa) ValidateToken(token int, key *[]byte, unixTimestamp int64, tokenlength uint8, ...) (bool, error)
- func (tinymfa *TinyMfa) ValidateTokenCurrentTimestamp(token int, key *[]byte, tokenlength uint8, algorithm HashAlgorithm, ...) Validation
- func (tinymfa *TinyMfa) ValidateTokenWithTimestamp(token int, key *[]byte, timestamp int64, tokenlength uint8, ...) Validation
- func (tinymfa *TinyMfa) WriteQrCodeImage(issuer, user string, secret *string, digits uint8, algorithm HashAlgorithm, ...) error
- type TinyMfaInterface
- type Validation
Constants ¶
const ( // Present can be used as an Offset Type Present uint8 = iota // Future can be used as an Offset Type Future // Past can be used as an Offset Type Past )
const ( // KeySizeSHA1 is the recommended secret key size for SHA-1 (160 bits / 20 bytes). // RFC 6238 Section 4 recommends keys be at least as long as the HMAC output. KeySizeSHA1 int8 = 20 // KeySizeSHA256 is the recommended secret key size for SHA-256 (256 bits / 32 bytes). // RFC 6238 Section 4 recommends keys be at least as long as the HMAC output. KeySizeSHA256 int8 = 32 // KeySizeSHA512 is the recommended secret key size for SHA-512 (512 bits / 64 bytes). // RFC 6238 Section 4 recommends keys be at least as long as the HMAC output. KeySizeSHA512 int8 = 64 )
const ( // DefaultTimeStep is the default time step size in seconds (RFC 6238 Section 4.1). DefaultTimeStep int64 = 30 // DefaultT0 is the default Unix epoch offset in seconds (RFC 6238 Section 4.1). DefaultT0 int64 = 0 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HashAlgorithm ¶ added in v0.4.0
type HashAlgorithm uint8
HashAlgorithm represents the hash algorithm used for HMAC computation. RFC 6238 Section 1.2 defines SHA-1, SHA-256, and SHA-512 as valid algorithms.
const ( // SHA1 selects HMAC-SHA-1 for TOTP computation (RFC 6238 Section 1.2). SHA1 HashAlgorithm = iota // SHA256 selects HMAC-SHA-256 for TOTP computation (RFC 6238 Section 1.2). SHA256 // SHA512 selects HMAC-SHA-512 for TOTP computation (RFC 6238 Section 1.2). SHA512 )
type TinyMfa ¶ added in v0.3.0
type TinyMfa struct {
QRCodeConfig structs.QrCodeConfig
}
func (*TinyMfa) BuildPayload ¶ added in v0.3.0
func (tinymfa *TinyMfa) BuildPayload(issuer, username string, secret *string, digits uint8, algorithm HashAlgorithm, timeStep int64) string
BuildPayload builds the otpauth:// URL payload for QR code generation with specified algorithm and timeStep.
func (*TinyMfa) CalculateHMAC ¶ added in v0.3.0
func (tinymfa *TinyMfa) CalculateHMAC(message []byte, key *[]byte, algorithm HashAlgorithm) ([]byte, error)
CalculateHMAC calculates the HMAC value for a given message and key using the specified hash algorithm. Supported algorithms are SHA-1, SHA-256, and SHA-512. RFC 2104 defines the HMAC construction. RFC 6238 Section 1.2 specifies the supported hash functions for TOTP.
func (*TinyMfa) ConvertColorSetting ¶ added in v0.3.0
func (tinymfa *TinyMfa) ConvertColorSetting(setting structs.ColorSetting) color.Color
func (*TinyMfa) GenerateExtendedSecretKey ¶ added in v0.3.0
GenerateExtendedSecretKey returns a 32-byte secret key (SHA-256 recommended size).
func (*TinyMfa) GenerateMessage ¶ added in v0.3.0
func (tinymfa *TinyMfa) GenerateMessage(timestamp int64, offsetType uint8, timeStep int64, t0 int64) (int64, error)
GenerateMessage computes the time counter T for TOTP using configurable time step and epoch offset parameters. The counter is calculated as:
T = floor((unixTime + offset - t0) / timeStep)
where offset is determined by offsetType: Present=0, Future=+timeStep, Past=-timeStep. RFC 6238 Section 4.2 defines the time counter computation. RFC 6238 Section 5.2 defines the time step size X (default 30s) and epoch T0 (default 0).
func (*TinyMfa) GenerateMessageBytes ¶ added in v0.3.0
GenerateMessageBytes takes in a int64 number and turns it to a BigEndian byte array
func (*TinyMfa) GenerateQrCode ¶ added in v0.3.0
func (tinymfa *TinyMfa) GenerateQrCode(issuer, user string, secret *string, digits uint8, algorithm HashAlgorithm, timeStep int64) ([]byte, error)
GenerateQrCode Generates a QRCode of the totp url with specified algorithm and timeStep
func (*TinyMfa) GenerateSecretKey ¶ added in v0.3.0
GenerateSecretKey returns a secret key of the specified size. Valid sizes are KeySizeSHA1 (20), KeySizeSHA256 (32), and KeySizeSHA512 (64).
func (*TinyMfa) GenerateSecretKeyForAlgorithm ¶ added in v0.4.0
func (tinymfa *TinyMfa) GenerateSecretKeyForAlgorithm(algorithm HashAlgorithm) (*[]byte, error)
GenerateSecretKeyForAlgorithm generates a cryptographically random secret key with the recommended size for the specified hash algorithm. Key sizes follow the recommendation in RFC 6238 Section 4, which states that keys SHOULD be of the length of the HMAC output to facilitate interoperability.
- SHA-1: 20 bytes (160 bits)
- SHA-256: 32 bytes (256 bits)
- SHA-512: 64 bytes (512 bits)
func (*TinyMfa) GenerateStandardSecretKey ¶ added in v0.3.0
GenerateStandardSecretKey returns a 20-byte secret key (SHA-1 recommended size).
func (*TinyMfa) GenerateSuperbSecretKey ¶ added in v0.4.0
GenerateSuperbSecretKey returns a 64-byte secret key (SHA-512 recommended size).
func (*TinyMfa) GenerateToken ¶ added in v0.4.0
func (tinymfa *TinyMfa) GenerateToken(unixTimestamp int64, key *[]byte, offsetType uint8, tokenlength uint8, algorithm HashAlgorithm, timeStep int64, t0 int64) (int, error)
GenerateToken generates a TOTP token per RFC 6238 with configurable hash algorithm, time step, and epoch offset. This function implements the full TOTP generation pipeline:
- Compute time counter T (RFC 6238 Section 4.2)
- Convert T to 8-byte big-endian representation
- Compute HMAC using the selected algorithm (RFC 2104)
- Apply dynamic truncation (RFC 4226 Section 5.3)
- Reduce to the requested number of digits (RFC 4226 Section 5.4)
Supported token lengths are 5-8 digits. Supported algorithms are SHA1, SHA256, SHA512. RFC 6238 Section 4.2 recommends SHA-256 or SHA-512 for new deployments.
func (*TinyMfa) GetQRCodeConfig ¶ added in v0.3.0
func (tinymfa *TinyMfa) GetQRCodeConfig() structs.QrCodeConfig
GetQRCodeConfig returns the current QRCodeConfig for the QRCode.
func (*TinyMfa) SetQRCodeConfig ¶ added in v0.3.0
func (tinymfa *TinyMfa) SetQRCodeConfig(qrcodeConfig structs.QrCodeConfig)
SetQRCodeConfig sets the QRCodeConfig for the QRCode.
func (*TinyMfa) ValidateToken ¶ added in v0.3.0
func (tinymfa *TinyMfa) ValidateToken(token int, key *[]byte, unixTimestamp int64, tokenlength uint8, algorithm HashAlgorithm, timeStep int64, t0 int64) (bool, error)
ValidateToken validates a submitted TOTP token against present, past, and future time windows using the specified hash algorithm and time parameters. The validation checks three consecutive time steps to account for clock drift between client and server. RFC 6238 Section 5.2 recommends validation across a window of time steps.
func (*TinyMfa) ValidateTokenCurrentTimestamp ¶ added in v0.3.0
func (tinymfa *TinyMfa) ValidateTokenCurrentTimestamp(token int, key *[]byte, tokenlength uint8, algorithm HashAlgorithm, timeStep int64, t0 int64) Validation
ValidateTokenCurrentTimestamp validates a submitted TOTP token against the current Unix timestamp using the specified algorithm and time parameters. This is a convenience wrapper around ValidateToken that captures the current system time. RFC 6238 Section 5.2 defines the validation procedure.
func (*TinyMfa) ValidateTokenWithTimestamp ¶ added in v0.3.0
func (tinymfa *TinyMfa) ValidateTokenWithTimestamp(token int, key *[]byte, timestamp int64, tokenlength uint8, algorithm HashAlgorithm, timeStep int64, t0 int64) Validation
ValidateTokenWithTimestamp validates a submitted TOTP token against a provided Unix timestamp using the specified algorithm and time parameters. This is a convenience wrapper around ValidateToken that returns a Validation struct. RFC 6238 Section 5.2 defines the validation procedure.
func (*TinyMfa) WriteQrCodeImage ¶ added in v0.3.0
func (tinymfa *TinyMfa) WriteQrCodeImage(issuer, user string, secret *string, digits uint8, algorithm HashAlgorithm, timeStep int64, filePath string) error
WriteQrCodeImage writes a png to the filesystem with specified algorithm and timeStep
type TinyMfaInterface ¶ added in v0.3.0
type TinyMfaInterface interface {
// GenerateStandardSecretKey returns a 20-byte secret key (SHA-1 recommended size).
GenerateStandardSecretKey() (*[]byte, error)
// GenerateExtendedSecretKey returns a 32-byte secret key (SHA-256 recommended size).
GenerateExtendedSecretKey() (*[]byte, error)
// GenerateSuperbSecretKey returns a 64-byte secret key (SHA-512 recommended size).
GenerateSuperbSecretKey() (*[]byte, error)
// GenerateSecretKey returns a secret key of the specified size.
// Valid sizes are KeySizeSHA1 (20), KeySizeSHA256 (32), and KeySizeSHA512 (64).
GenerateSecretKey(size int8) (*[]byte, error)
// GenerateSecretKeyForAlgorithm generates a secret key with the recommended size
// for the specified hash algorithm per RFC 6238 Section 4.
GenerateSecretKeyForAlgorithm(algorithm HashAlgorithm) (*[]byte, error)
// GenerateMessageBytes takes in a int64 number and turns it to a BigEndian byte array.
GenerateMessageBytes(message int64) ([]byte, error)
// CalculateHMAC calculates the HMAC value for a given message and key
// using the specified hash algorithm (RFC 2104, RFC 6238 Section 1.2).
CalculateHMAC(message []byte, key *[]byte, algorithm HashAlgorithm) ([]byte, error)
// GenerateMessage computes the time counter T for TOTP using configurable
// parameters per RFC 6238 Section 4.2.
GenerateMessage(timestamp int64, offsetType uint8, timeStep int64, t0 int64) (int64, error)
// GenerateToken generates a TOTP token per RFC 6238 with configurable hash algorithm,
// time step, and epoch offset (RFC 6238 Section 4.2).
GenerateToken(unixTimestamp int64, key *[]byte, offsetType uint8, tokenlength uint8, algorithm HashAlgorithm, timeStep int64, t0 int64) (int, error)
// ValidateToken validates a submitted TOTP token with configurable algorithm
// and time parameters per RFC 6238 Section 5.2.
ValidateToken(token int, key *[]byte, unixTimestamp int64, tokenlength uint8, algorithm HashAlgorithm, timeStep int64, t0 int64) (bool, error)
// ValidateTokenCurrentTimestamp validates a TOTP token against the current
// Unix timestamp with configurable parameters (RFC 6238 Section 5.2).
ValidateTokenCurrentTimestamp(token int, key *[]byte, tokenlength uint8, algorithm HashAlgorithm, timeStep int64, t0 int64) Validation
// ValidateTokenWithTimestamp validates a TOTP token against a provided
// Unix timestamp with configurable parameters (RFC 6238 Section 5.2).
ValidateTokenWithTimestamp(token int, key *[]byte, timestamp int64, tokenlength uint8, algorithm HashAlgorithm, timeStep int64, t0 int64) Validation
// GenerateQrCode generates a QRCode for the provided issuer, user and secret with specified algorithm and timeStep.
GenerateQrCode(issuer, user string, secret *string, digits uint8, algorithm HashAlgorithm, timeStep int64) ([]byte, error)
// ConvertColorSetting converts the ColorSetting struct into a color.Color object.
ConvertColorSetting(setting structs.ColorSetting) color.Color
// WriteQrCodeImage writes a QR code PNG to the filesystem with specified algorithm and timeStep.
WriteQrCodeImage(issuer, user string, secret *string, digits uint8, algorithm HashAlgorithm, timeStep int64, filepath string) error
// BuildPayload builds the otpauth:// URL payload for QR code generation with specified algorithm and timeStep.
BuildPayload(issuer, username string, secret *string, digits uint8, algorithm HashAlgorithm, timeStep int64) string
// SetQRCodeConfig sets the QRCodeConfig for the QRCode.
SetQRCodeConfig(qrcodeConfig structs.QrCodeConfig)
// GetQRCodeConfig returns the current QRCodeConfig for the QRCode.
GetQRCodeConfig() structs.QrCodeConfig
}
func NewTinyMfa ¶ added in v0.3.0
func NewTinyMfa() TinyMfaInterface
type Validation ¶ added in v0.3.0
Validation is a struct used to return the result of a token validation