Documentation
¶
Overview ¶
Package totp implements Time-based One-Time Password (TOTP) generation and validation.
TOTP is an algorithm that computes a one-time password from a shared secret key and the current time. This package provides functions to generate a TOTP secret, generate TOTP codes, and validate them.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateSecret ¶
GenerateSecret generates a random TOTP secret key in base32 encoding. The length of the generated secret is fixed to 10 bytes. It returns the base32-encoded secret and an error if the generation fails.
func TOTP ¶
TOTP generates a 6-digit TOTP code using the given base32-encoded secret and a time step in seconds (default is 30 seconds). It returns the generated TOTP code and an error if the generation fails.
Example ¶
Example of how to use the package
// Generate a secret
secret, err := GenerateSecret()
if err != nil {
panic(err)
}
// Generate a TOTP code
code, err := TOTP(secret, 30)
if err != nil {
panic(err)
}
// Validate the code
isValid := Validate(secret, 30, code)
println("Code is valid:", isValid)
Types ¶
This section is empty.