GoTOTP

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2024 License: MIT Imports: 13 Imported by: 0

README

GoTOTP: A Simple Time-Based One-Time Password (TOTP) Library

Go

Overview

GoTOTP is a simple, stable, and efficient Time-Based One-Time Password (TOTP) library written in Go, built with a focus on simplicity and stability. It aims to be a long-term solution for generating and verifying TOTPs, without reinventing the wheel.

This implementation is based on RFC 6238, ensuring compatibility with Google Authenticator and similar apps.

Why GoTOTP?
  • Simplicity: Prioritizes straightforward code and minimal changes once the library reaches maturity.
  • Stability: Aims for long-term stability.
  • Standard-compliant: Utilizes existing Go standard libraries wherever possible.
  • Personal Project: Initially built out of curiosity and as a break from web development.

Features

  • Generates a 6-digit code, valid for 30 seconds (default).
  • Built-in methods for secret generation, code verification, and URI generation for QR codes.
  • QR code support for Google Authenticator and other similar apps (future enhancement).

Project Goals & Roadmap

  1. Simplicity: Maintain ease of use and clarity in design.
  2. QR Code Generation: Implement a method to generate QR codes for TOTP setup.
  3. Long-Term Stability: Keep the library rock-solid for years to come.

Future Enhancements

  • Potentially allow users to configure code length and expiration period.
    • Note: This is not a current priority.

Installation

To get started, install the package:

go get github.com/MrTuNNe/GoTOTP

Usage Example

Here is a basic example of how to generate and verify TOTPs using GoTOTP:

package main

import (
    "fmt"
    "time"
    "github.com/MrTuNNe/GoTOTP"
)

func main() {
    // Generate a random secret (base32 encoded, without padding)
    secret, err := GoTOTP.GenerateRandomSecret(32) // 32 bytes length
    if err != nil {
        // Handle error
        fmt.Println("Error generating secret:", err)
        return
    }

    // Create a new TOTP instance
    totp := GoTOTP.TOTP{
        Key:      "OK6ZZOALZY6RNZBPM4QKD2ZFO5F3PTP56VIAXLDJLEHBPLJJIZNQ",
        Issuer:   "mrtunne.info",
        UserName: "[email protected]",
    }

    // Generate a TOTP based on the current timestamp
    code := totp.GenerateTOTP(time.Now().Unix())
    fmt.Println("Generated TOTP:", code)

    // Verify user input
    if totp.Verify("149425") {
        fmt.Println("Code verified successfully!")
    } else {
        fmt.Println("Invalid code.")
    }

    // Check code with a specific timestamp
    if totp.VerifyWithTimestamp(1723719527, "611626") {
        fmt.Println("Code valid for timestamp.")
    } else {
        fmt.Println("Code invalid for timestamp.")
    }

    // Generate a URI for QR code generation
    uri := totp.GenerateURI()
    fmt.Println("TOTP URI:", uri)
    // Example output:
    // otpauth://totp/mrtunne.info:%[email protected]?algorithm=SHA256&digits=6&issuer=mrtunne.info&period=30&secret=OK6ZZOALZY6RNZBPM4QKD2ZFO5F3PTP56VIAXLDJLEHBPLJJIZNQ
}
Notes:
  • Default Expiration: Generated codes expire after 30 seconds.
  • Default Length: Codes are 6 digits long.

Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request if you have any improvements or suggestions.

License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateRandomSecret added in v0.1.2

func GenerateRandomSecret(length int) (string, error)

This will generate random bytes and encode them to Base32 (without padding)

Types

type TOTP

type TOTP struct {
	Key      string
	Issuer   string
	UserName string
}

func (*TOTP) GenerateTOTP

func (totp *TOTP) GenerateTOTP(timestamp int64) (string, error)

Based from RFC 6238

func (*TOTP) GenerateURI added in v0.2.0

func (totp *TOTP) GenerateURI() (string, error)

func (*TOTP) Verify

func (totp *TOTP) Verify(inputCode string) bool

Verify if the given input code is valid for the current timestamp.

func (*TOTP) VerifyWithTimestamp

func (totp *TOTP) VerifyWithTimestamp(timestamp int64, inputCode string) bool

Verify if the input code is valid for a given timestamp. Use this just for testing

Jump to

Keyboard shortcuts

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