totp

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2024 License: MIT Imports: 8 Imported by: 0

README

TOTP

GitHub release (latest by date) GitHub Workflow Status GitHub GitHub last commit GitHub forks GitHub top language

TOTP is a Time Based One Time Password Algorithm, which can be used in client side or server side applications.

Features

  • Generate TOTP secret.
  • Generate TOTP code.
  • Verify TOTP code.

How to use it?

  • Install dependencies
go get github.com/tech-thinker/[email protected]
  • Write your code
package main    You, 05/11/24 09:52 • initial commit

import (
    "fmt"

    "github.com/tech-thinker/totp"
)

func main() {
    secret, err := totp.GenerateSecret()
    if err != nil {
        fmt.Println("Error generating secret:", err)
    }
    duration := 30
    otp, err := totp.TOTP(secret, duration)
    if err != nil {
        fmt.Println("Error generating TOTP:", err)
        return
    }
    fmt.Println("Generated TOTP Code:", otp)
    fmt.Println("Verify:", totp.Validate(secret, duration, otp))
}

Contributors

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

func GenerateSecret() (string, error)

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

func TOTP(secret string, duration int) (string, error)

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)

func Validate

func Validate(secret string, duration int, code string) bool

Validate checks if the provided TOTP code matches the generated TOTP code for the given secret within a +/- 30-second time window. It returns true if the code is valid, otherwise false.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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