totp

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2015 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package totp contains an RFC6238 implementation of time-based one time passwords.

To be able to generate codes, you need to create a TOTP object using a secret:

import (
	"gopkg.in/zhevron/go-twofactor.v1"
	"gopkg.in/zhevron/go-twofactor.v1/totp"
)
secret := twofactor.NewSecret(0)
otp := totp.NewTOTP(secret, 0, 0)

After creating the TOTP object, you can easily generate the current code by calling the Code method:

code, err := otp.Code()
if err != nil {
	// Failed to generate the code
}

A convenience method is also provided for checking if a code is valid using time period offset:

code := 123456
if otp.Validate(code, 0) {
	// Code is valid
} else {
	// Code is not valid
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TOTP

type TOTP struct {
	// contains filtered or unexported fields
}

TOTP implements time-based one time passwords as specified in RFC6238 (http://tools.ietf.org/html/rfc6238).

func NewTOTP

func NewTOTP(s twofactor.Secret, length int, duration int) *TOTP

NewTOTP makes a new TOTP object for generating OTP codes from a given Secret. If length is 0 or less, it will default to a length of 6. If duration is 0 or less, it will default to a duration of 30 seconds.

func (TOTP) Code

func (t TOTP) Code() (int32, error)

Code returns the current OTP code.

func (TOTP) ForPeriod

func (t TOTP) ForPeriod(n int64) (int32, error)

ForPeriod returns the code for the given period.

func (TOTP) ForPeriodOffset

func (t TOTP) ForPeriodOffset(n int64) (int32, error)

ForPeriodOffset returns the code from the time period of current+offset.

func (TOTP) Secret

func (t TOTP) Secret() twofactor.Secret

Secret returns the secret for this TOTP object.

func (TOTP) Validate

func (t TOTP) Validate(code int32, offset int) bool

Validate checks the the provided code is valid.

Jump to

Keyboard shortcuts

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