GoTOTP

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: MIT Imports: 10 Imported by: 0

README

Go

A simple Time Based One-Time Password library

  • This library is created with the focus on simplicity and stable code.
  • This library will not change much once it will reach maturity.
  • While other implementations already exists, this project is mostly created because I was bored of building only websites :)
  • This library will try as much possible not to reinvent the wheel. If there is an implementation in the standard library, that implementation will be used. (I've seen in some other libraries this weird behavior of reinventing the wheel)

This implementation is based on RFC 6238

Project Goals & Roadmap

  1. Keep it simple
  2. Create a way of generating random secret keys (will be made soon)
  3. Implementing a method of generating the QR code for Google Authenticator and other similar apps like this.
  4. Keep it rock solid for years to come :)

Other things to keep in mind

  • The default behavior is to generate a code that will expire in 30 seconds
  • The generated code will have 6 numbers.

Maybe in the future I'm going to make it in a way to control this yourself, however this is not a priority.

Code Examples

First install the package


go get github.com/MrTuNNe/GoTOTP

  
// To generate a random secret you can use this
secret, err := totp.GenerateRandomSecret(32) // the length of the secret key
if err != nil {
	// handle the error
}
// The secret key must be base32 encoded and without padding

totp := totp.TOTP{Key: "OK6ZZOALZY6RNZBPM4QKD2ZFO5F3PTP56VIAXLDJLEHBPLJJIZNQ"}

// generate the TOTP with the timestamp

totp.GenerateTOTP(time.Now().Unix())

if totp.Verify("149425") { // check the input code from the user

// do something if the code is good

}

// you can also check a code with a timestamp

totp.VerifyWithTimestamp(1723719527, "611626") // this will return true

totp.VerifyWithTimestamp(1723719580, "611626") // this will return false

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateRandomSecret added in v0.1.2

func GenerateRandomSecret(length int) (string, error)

This will generate random bytes and encode them to Base32 (without padding)

Types

type TOTP

type TOTP struct {
	Key string
}

func (*TOTP) GenerateTOTP

func (totp *TOTP) GenerateTOTP(timestamp int64) (string, error)

Based from RFC 6238

func (*TOTP) Verify

func (totp *TOTP) Verify(inputCode string) bool

Verify if the given input code is valid for the current timestamp.

func (*TOTP) VerifyWithTimestamp

func (totp *TOTP) VerifyWithTimestamp(timestamp int64, inputCode string) bool

Verify if the input code is valid for a given timestamp. Use this just for testing

Jump to

Keyboard shortcuts

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