totp

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2023 License: MIT Imports: 7 Imported by: 1

README

totp

Simple library with no dependencies to:

Installation

go get -u github.com/lucasepe/totp

Usage

Generation
package main

import (
    "fmt"

    "github.com/lucasepe/totp"
)

func main() {
    code, err := totp.New(totp.Options{
        Secret:   "JBSWY3DPEHPK3PXP",
        Digits:   8,
        Period:   15,
        UnixTime: 32158800000,
    })
    if err != nil {
        panic(err)
    }

    fmt.Println(code)
}
URI Parsing
package main

import (
    "fmt"

    "github.com/lucasepe/totp"
)

func main() {
    opts, err := totp.ParseURI("otpauth://totp/Acme?secret=IRXW4J3UEBKGK3DMEBAW46KPNZSSC")
    if err != nil {
        panic(err)
    }

    code, err := totp.New(opts)
    if err != nil {
        panic(err)
    }

    fmt.Println(code)
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts Options) (string, error)

New generate a TOTP code.

Example
package main

import (
	"fmt"

	"github.com/lucasepe/totp"
)

func main() {
	// Verify with: https://totp.danhersam.com/
	code, err := totp.New(totp.Options{
		Secret:   "JBSWY3DPEHPK3PXP",
		Digits:   8,
		Period:   15,
		UnixTime: 32158800000,
	})
	if err != nil {
		fmt.Println(err.Error())
	}

	fmt.Println(code)

}
Output:

45451783

func ParseKey

func ParseKey(s string) ([]byte, error)

ParseKey parses a key encoded as base32, the format used by common two-factor authentication setup tools. Whitespace is ignored, case is normalized, and padding is added if required.

Types

type Options

type Options struct {
	Secret    string // Secret key (required)
	Digits    int    // OTP digit count (default: 6)
	Algorithm string // OTP Algorithm ("SHA1" or "SHA256" or "SHA512") (default: SHA1)
	Period    int64  // Period for which OTP is valid (seconds) (default: 30)
	UnixTime  int64  // (Optional) Unix Timestamp (default: Current unix timestamp)
}

Options represents Time-based OTP. See https://datatracker.ietf.org/doc/html/rfc6238

func ParseURI

func ParseURI(u string) (Options, error)
Example
package main

import (
	"fmt"

	"github.com/lucasepe/totp"
)

func main() {
	opts, err := totp.ParseURI("otpauth://totp/Acme?secret=IRXW4J3UEBKGK3DMEBAW46KPNZSSC")
	if err != nil {
		panic(err)
	}

	// This only to be sure to generate always the same code.
	opts.UnixTime = int64(32158800)

	code, err := totp.New(opts)
	if err != nil {
		panic(err)
	}

	fmt.Println(code)

}
Output:

331676

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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