Categorygithub.com/FuLygon/go-totp/v2
modulepackage
2.4.0
Repository: https://github.com/fulygon/go-totp.git
Documentation: pkg.go.dev

# README

go-totp

Github tag GoReportCard

Package go-totp library implements functionalities to create and validate Time-Based One-Time Password (TOTP) for Two-Factor Authentication (2FA) applications.

TOTP generates temporary codes based on a shared secret key, enhancing security.

Installation

Use go get

go get -u github.com/FuLygon/go-totp/v2

Import package

import "github.com/FuLygon/go-totp/v2"

Documentation

GoDoc

Example

See Example

Usage

Create TOTP

Generate or define a TOTP instance

t, err := totp.New(totp.TOTP{
    AccountName: "your_account_name",
    Issuer:      "your_issuer_name",
})
if err != nil {
    // handle error
    log.Println("error generating QR code:", err)
    return
}

// optionally, define TOTP details:
t := totp.TOTP{
    AccountName: "your_account_name",
    Issuer:      "your_issuer_name",
    Algorithm:   totp.AlgorithmSHA1,
    Digits:      6,
    Period:      30,
    Secret:      "your_shared_secret",
}

Generate TOTP URL and QR code

// generate TOTP URL
url, err := t.GetURL()
if err != nil {
    // handle error
    log.Println("error generating TOTP URL:", err)
    return
}
fmt.Println("TOTP URL:", url)

// generate QR code
qr, err := t.GetQR(256)
if err != nil {
    // handle error
    log.Println("error generating QR code:", err)
    return
}
fmt.Println("QR Code Base64:", qr.Base64)

Validating TOTP code

Create a validator instance

v := totp.Validator{
  Algorithm: totp.AlgorithmSHA1,
  Digits:    6,
  Period:    30,
  Secret:    "your_shared_secret",
}

Validate TOTP code

code := "123456" // user-provided TOTP code

valid, err := v.Validate(code)
if err != nil {
    // handle error
    log.Println("error validating TOTP code:", err)
    return
}

if valid {
    fmt.Println("TOTP code is valid!")
} else {
    fmt.Println("TOTP code is invalid.")
}

# Packages

No description provided by the author

# Functions

New creates a new TOTP with a randomly generated shared secret, default value will be used if null.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author

# Variables

ErrEmptyAccountName value for account name is required.
ErrEmptyIssuer value for issuer is required.
ErrInvalidAlgorithm invalid or unsupported algorithm.
ErrInvalidDigits invalid or unsupported digits, supported values are from 1 to 10.
ErrInvalidPeriod invalid period value.
ErrInvalidSecret invalid secret value.

# Structs

QR represents a QR code.
TOTP represents parameters of a Time-based One-Time Password.
Validator defines the structure used for TOTP validation.

# Type aliases

Algorithm represents hashing functions for generating OTP.