otp

package module
v0.0.0-...-71391b8 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2019 License: MIT Imports: 13 Imported by: 0

README

otp

Go package for HOTP (RFC4226) and TOTP (RFC6238)

GoDoc

Usage
Generate secret of default size (20 bytes)
secret, _ := otp.NewSecret()
Generate secret of specified size (bytes)
secret, _ := otp.NewSecretWithSize(otp.SECERT_SIZE_32)
Generate URI for HOTP (Counter value must be non-zero)
hotpURI := otp.URI(secret, &otp.Options{
	User:    "testuser",
	Issuer:  "testissuer",
	Counter: 1,
})
Generate URI for HOTP (Counter value is always zero for TOTP)
totpURI := otp.URI(secret, &otp.Options{
	User:   "testuser",
	Issuer: "testissuer",
})
Generate QR code for HOTP/TOTP
qr, uri, qrErr := otp.QR(secret, &otp.Options{
	User:   "testuser",
	Issuer: "testissuer",
})
Generate HOTP of length 4 and next counter value of 20
hotp := otp.HOTP(sha1.New, secret, 20, otp.DIGITS_4)
Generate TOTP with recycle rate of 45 sec and length 8
totp := otp.TOTP(sha1.New, secret, time.Now(), otp.PERIOD_45, otp.DIGITS_8)
Generate Google Authenticator compatible HOTP
ghotp := otp.GOOGLE_HOTP(secret, 1)
Generate Google Authenticator compatible TOTP
gtotp := otp.GOOGLE_TOTP(secret)

Documentation

Overview

otp.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GOOGLE_HOTP

func GOOGLE_HOTP(secret string, counter uint64) (otp string)

Generate a Hmac based OTP that is compatible with Google Authenticator

func GOOGLE_TOTP

func GOOGLE_TOTP(secret string) (otp string)

Generate a Time based OTP that is compatible with Google Authenticator

func HOTP

func HOTP(hasher func() hash.Hash, secret string, counter uint64, length Digit) (otp string)

Generate a Hmac based OTP

func HOTP_SHA1

func HOTP_SHA1(secret string, counter uint64, length Digit) (otp string)

Generate a Hmac based OTP using Sha1 as the Hashing Algorithm for HMac

func NewSecret

func NewSecret() (secret string, err error)

Generate a new secret of default size (20 bytes)

func NewSecretWithSize

func NewSecretWithSize(size SecretSize) (secret string, err error)

Generate a new secret of specified size. Mininum size is 16 bytes

func QR

func QR(secret string, options *Options) (qrCode string, uri string, err error)

Generate OATH URI and QR code compatible with Authenticator Apps.

func TOTP

func TOTP(hasher func() hash.Hash, secret string, timeStamp time.Time, period Period, length Digit) (otp string)

Generate a Time based OTP

func TOTP_SHA1

func TOTP_SHA1(secret string, timeStamp time.Time, period Period, length Digit) (otp string)

Generate a Time based OTP using Sha1 as the Hashing Algorithm for HMac

func URI

func URI(secret string, options *Options) string

Generate OATH URI compatible with Authenticator Apps. The format is documented at https://github.com/google/google-authenticator/wiki/Key-Uri-Format

Types

type Algorithm

type Algorithm string
const (
	SHA1   Algorithm = "sha1"
	SHA256 Algorithm = "sha256"
	SHA512 Algorithm = "sha512"
)

Standard Hashing Algorithms

type Digit

type Digit uint8
const (
	DIGITS_4   Digit = 4
	DIGITS_6   Digit = 6
	DIGITS_8   Digit = 8
	DIGITS_MIN Digit = DIGITS_4
	DIGITS_MAX Digit = 9
)

Standard OTP Lengths

type Options

type Options struct {
	User      string
	Issuer    string
	Counter   uint64
	Period    Period
	Digits    Digit
	Algorithm Algorithm
}

type Period

type Period uint8
const (
	PERIOD_30 Period = 30
	PERIOD_45 Period = 45
	PERIOD_60 Period = 60
)

Standard TOTP Periods

type SecretSize

type SecretSize uint8
const (
	SECRET_SIZE_16      SecretSize = 16
	SECRET_SIZE_20      SecretSize = 20
	SECERT_SIZE_32      SecretSize = 32
	SECRET_SIZE_64      SecretSize = 64
	SECRET_SIZE_MIN     SecretSize = SECRET_SIZE_16
	SECRET_SIZE_DEFAULT SecretSize = SECRET_SIZE_20
)

Standard Secret sizes

Jump to

Keyboard shortcuts

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