gototp

package module
v0.0.0-...-bbfd598 Latest Latest
Warning

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

Go to latest
Published: May 10, 2013 License: MIT Imports: 9 Imported by: 3

README

gototp

Go Time-Based One Time Password implemention of RFC6238 (http://tools.ietf.org/html/rfc6238)

Based on pyotp: https://github.com/nathforge/pyotp

USAGE

gototp is easy to use:

  1. Generate a random secret to store for a user:
    // Do this somewhere early, and only once
    rnd := rand.New(rand.NewSource(time.Now().Unix())
    
    // A secret length of 10 gives a 16 character secret key
    secret := gototp.RandomSecret(10, rand.New(rand.NewSource(time.Now().Unix())))
  1. Create the OTP object:
    // Create the OTP
    otp, err := gototp.New(secret)
    if nil!=err {
      panic(err)
  	}
  1. Find the current TOTP code:
    code := otp.Now()

    // Or find the previous code and the next code
    previousCode := otp.FromNow(-1)
    nextCode := otp.FromNow(1)
  1. Generate a Google Charts URL for a QR Code of the Secret, with a label
    // Google Charts URL to display a QR Code, of width (and height) 300px
    url := otp.QRCodeGoogleChartsUrl("My Own TOTP", 300)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RandomSecret

func RandomSecret(length int, rnd *rand.Rand) string

Generate a Random secret encoded as a b32 string If the length is <= 0, a default length of 10 bytes will be used, which will generate a secret of length 16.

Types

type TOTP

type TOTP struct {
	Digits int // Number of digits for code. Defaults to 6.
	Period int // Number of seconds for each code. Defaults to 30.
	// contains filtered or unexported fields
}

Time-based One Time Password

func New

func New(secretB32 string) (*TOTP, error)

Generate a new Time Based One Time Password with the given secret (a b32 encoded key)

func (*TOTP) ForPeriod

func (totp *TOTP) ForPeriod(period int64) int32

Return the time-based OTP for the given period.

func (*TOTP) FromNow

func (totp *TOTP) FromNow(periods int64) int32

Return the Time Based One Time Password for the time-period that is Now + the given periods. This is useful if you want to provide some flexibility around the acceptance of codes. For instance, you might want to accept a code that is valid in the current period (FromNow(0)), or that was valid in the previous period (FromNow(-1)) or that will be valid in the next period (FromNow(1)). This means that every code is therefore valid for 3 * totp.Period.

func (*TOTP) Now

func (totp *TOTP) Now() int32

Return the Time Based One Time Password for right now

func (*TOTP) QRCodeData

func (totp *TOTP) QRCodeData(label string) string

Return the data to be contained in a QR Code for this TOTP with the given label.

func (*TOTP) QRCodeGoogleChartsUrl

func (totp *TOTP) QRCodeGoogleChartsUrl(label string, width int) string

Return a URL to generate a QRCode on Google Charts for the TOTP, with the given label and width (and height equal to width).

func (*TOTP) Secret

func (totp *TOTP) Secret() string

Return the TOTP Secret base32 encoded

Jump to

Keyboard shortcuts

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