Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Config = config{ Digits: DigitsSix, SecretSize: 16, Period: 30, Algorithm: AlgorithmSHA1, ValidChars: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", }
Config is the configuration for the package. It can be changed to customize the package. The default configuration is:
SecretSize: 16, Period: 30, Algorithm: AlgorithmSHA1, Digits: DigitsSix, ValidChars: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
Functions ¶
func CreateSecret ¶
func CreateSecret() string
CreateSecret returns a random secret key. The secret key is a string of characters from the set A-Z, 2-7. The default length of the secret key is 16 characters. This can be changed by setting Config.SecretSize. The default set of characters can be changed by setting Config.ValidChars.
func GetCode ¶
GetCode returns the current code for the secret key. The code is valid for the current period. The period is 30 seconds by default. This can be changed by setting Config.Period.
func GetQRCode ¶
GetQRCode returns the URL of the QR code for the secret key. The QR code can be used to add the secret key to an authenticator app. The QR code is a Google Chart. The QR code is a PNG image.
func GetTime ¶
func GetTime() int64
GetTime returns the current time in Unix time divided by the period. This is used to generate the counter for the HOTP algorithm. The period is 30 seconds by default. This can be changed by setting Config.Period. The Unix time is divided by the period to make it easier to test. This way, the counter can be set to any value.
func Verify ¶
func Verify(config VerifyConfig) bool
Verify verifies the code against the secret key. It returns true if the code is valid, false otherwise.
Types ¶
type Digits ¶
type Digits int
Digits is the number of digits in the code. It can be 6 or 8. The default is 6.
type QrConfig ¶
type QrConfig struct { // Secret is the secret key. Secret string // Issuer is the name of the issuer. This is optional. If set, it will be displayed on the authenticator app. For example, "Google". Issuer string // AccountName is the name of the account. This is required. For example, "John Doe". // This will be displayed on the authenticator app. // The final display will be "Issuer (AccountName)" or "AccountName" if Issuer is not set. AccountName string }
QrConfig is the configuration for the GetQRCode function.
type VerifyConfig ¶
type VerifyConfig struct { // Secret is the secret key. // It must be a string of valid characters. Secret string // Code is the code to verify. // It must be a string of digits. Code string // Window is the number of codes to check before and after the current code. // If Window is 0, only the current code is checked. Window int }
VerifyConfig is the configuration for the Verify function.