onetime

package module
Version: v0.0.0-...-7dc4527 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2014 License: BSD-2-Clause Imports: 7 Imported by: 0

README

onetime

An one-time password generation library written in Go, implementing HOTP (RFC-4226) and TOTP (RFC-6238).

Example usage

Simple 6-digit HOTP code:

import "onetime"

var secret = []byte("SOME_SECRET")
var counter = 123456
var otp, _ = onetime.Simple(6)
var code = otp.HOTP(secret, counter)

Google authenticator style 8-digit TOTP code:

import "onetime"

var secret = []byte("SOME_SECRET")
var otp, _ = onetime.Simple(8)
var code = otp.TOTP(secret)

9-digit 5-second-step TOTP starting on midnight 2000-01-01 UTC, using SHA-256:

import (
    "crypto/sha256"
    "onetime"
    "time"
)

var secret = []byte("SOME_SECRET")
const ts = 5 * time.Second
var t = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
var otp = onetime.OneTimePassword{Digit: 9, TimeStep: ts, BaseTime: t, Hash: sha256.New}
var code = otp.TOTP(secret)

Documentation

Package doc can be found at godoc.org.

License

This library is released under a simplified BSD license.

Documentation

Overview

Package onetime provides a library for one-time password generation, implementing the HOTP and TOTP algorithms as specified by IETF RFC-4226 and RFC-6238.

Example (Authenticator)
// Google authenticator style 8-digit TOTP code:
var secret = []byte("SOME_SECRET")
var otp, _ = onetime.Simple(8)
var code = otp.TOTP(secret)
fmt.Println(code)
Output:

Example (Custom)
// 9-digit 5-second-step TOTP starting on midnight 2000-01-01 UTC, using SHA-256:

var secret = []byte("SOME_SECRET")
const ts = 5 * time.Second
var t = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
var otp = onetime.OneTimePassword{Digit: 9, TimeStep: ts, BaseTime: t, Hash: sha256.New}
var code = otp.TOTP(secret)
fmt.Println(code)
Output:

Example (Simple)
// Simple 6-digit HOTP code:
var secret = []byte("SOME_SECRET")
var counter uint64 = 123456
var otp, _ = onetime.Simple(6)
var code = otp.HOTP(secret, counter)
fmt.Println(code)
Output:

260040

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OneTimePassword

type OneTimePassword struct {
	Digit    int              // Length of code generated
	TimeStep time.Duration    // Length of each time step for TOTP
	BaseTime time.Time        // The start time for TOTP step calculation
	Hash     func() hash.Hash // Hash algorithm used with HMAC
}

OneTimePassword stores the configuration values relevant to HOTP/TOTP calculations.

func Simple

func Simple(digit int) (otp OneTimePassword, err error)

Simple returns a new OneTimePassword with the specified HTOP code length, SHA-1 as the HMAC hash algorithm, the Unix epoch as the base time, and 30 seconds as the step length.

func (*OneTimePassword) HOTP

func (otp *OneTimePassword) HOTP(secret []byte, count uint64) uint

HOTP returns a HOTP code with the given secret and counter.

func (*OneTimePassword) TOTP

func (otp *OneTimePassword) TOTP(secret []byte) uint

TOTP returns a TOTP code calculated with the current time and the given secret.

Source Files

Jump to

Keyboard shortcuts

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