entica

package module
v0.0.0-...-0c9ffac Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2020 License: MIT Imports: 14 Imported by: 0

README

Entica

Dead simple Golang OTP (One Time Password) library for both HOTP and TOTP without any dependency following both official RFC6238 and RFC4226 rules on the implementation.

TOTP

A time-based OTP, using the time for the message validation.

secret := entica.RandSecret() // base32 string (A-Z and 2-7) with length of 32 chars
totp := entica.NewTOTP(secret) // new default totp (sha1) with 6 digit result
totp.Get() // 918399

// or with more specific

secret := "IDI5FG3XTZE26AONPVRIVQP4DN2DV54J"
totp := entica.NewTOTPSHA(7, sha512.New, secret)
totp.Get() // 9183993

totp.Compare("codeToCheck") // return valid status
totp.At(time.Now().Sub(time.Hour * 3)) // return code at specific time, 3 hours earlier
totp.CodeAtUnix(1646373) // return code at specific unix time

HOTP

HMAC-Based OTP, using the counter value for the message validation.

secret := entica.RandSecret("SomeSalt")
hotp := entica.HOTP{
    Hash: entica.Sha1,
    Digits: 6,
    Secret: secret,  // 32bit string secret
    Counter: 1,
}

// or

hotp := entica.NewDefaultHOTP(secret, counter)

hotp.Check("codeToCheck") // return code at current time
hotp.CodeAtCounter(10) // return code at specific counter value 
hotp.CurrCounter() // return counter value 

LICENSE

MIT License

Copyright (c) 2020 Aditya Kresna

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RandSecret

func RandSecret() string

Types

type HOTP

type HOTP struct {
	OTP
	Counter int
	// contains filtered or unexported fields
}

func NewHOTP

func NewHOTP(s string) *HOTP

func NewHOTP256

func NewHOTP256(s string) *HOTP

func NewHOTP512

func NewHOTP512(s string) *HOTP

func NewHOTPSHA

func NewHOTPSHA(digits int, hash func() hash.Hash, secret string) *HOTP

func (*HOTP) AtCounter

func (h *HOTP) AtCounter(c int) string

func (*HOTP) Compare

func (h *HOTP) Compare(code string) bool

func (*HOTP) Current

func (h *HOTP) Current() string

func (*HOTP) Get

func (h *HOTP) Get() string

func (*HOTP) SetCounter

func (h *HOTP) SetCounter(c int)

type Hash

type Hash int
const (
	Sha1 Hash = iota + 1
	Sha256
	Sha512
)

type OTP

type OTP struct {
	Hash   func() hash.Hash
	Digits int
	Secret string
}

type TOTP

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

func NewTOTP

func NewTOTP(s string) *TOTP

func NewTOTP256

func NewTOTP256(s string) *TOTP

func NewTOTP512

func NewTOTP512(s string) *TOTP

func NewTOTPSHA

func NewTOTPSHA(digits int, hash func() hash.Hash, secret string) *TOTP

func (*TOTP) At

func (t *TOTP) At(at time.Time) string

func (*TOTP) Compare

func (t *TOTP) Compare(code string) bool

func (*TOTP) Get

func (t *TOTP) Get() string

Jump to

Keyboard shortcuts

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