totp

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

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

Go to latest
Published: Mar 10, 2025 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Overview

Package totp uses, creates or verifies time based one-time-passwords based on the specification in RFC6238¹. Digest hasher function sha512 and 8 digits are used by default by Now().

¹ https://www.rfc-editor.org/rfc/rfc6238.html

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	Verbosity com.Level
	Skew      time.Duration = -1 * time.Second
)

Functions

func Secret

func Secret(size int) (string, error)

Secret creates a random, base32 encoded secret of the given size for the use with TOTP.

Example
package main

import (
	"fmt"
	"log"

	"catinello.eu/totp"
)

func main() {
	secret, err := totp.Secret(32)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(len(secret))
}
Output:

52

Types

type Code

type Code string

Code represents the generated TOTP token.

func Now

func Now(secret string) Code

Now returns a code for the given secret, using sha512 and 8 digits on 30 seconds.

Example
package main

import (
	"fmt"

	"catinello.eu/totp"
)

var (
	MySecret string

	ThisCode totp.Code
)

func main() {
	ThisCode = totp.Now(MySecret)

	fmt.Println(len(ThisCode))
}
Output:

8

func Parse

func Parse(code string, length int) (Code, error)

Parse takes a string and validates the input to return a Code value.

Example
package main

import (
	"fmt"
	"log"

	"catinello.eu/totp"
)

func main() {
	code, err := totp.Parse("1234 5678", 8)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(len(code))
}
Output:

8

func (Code) String

func (c Code) String() string

String returns the token code with spaces to be readable.

type TOTP

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

TOTP contains all attributes and values.

func New

func New(secret string, digits uint8, hasher func() hash.Hash, period uint8) (TOTP, error)

New returns a TOTP with the given attributes.

Example
package main

import (
	"fmt"
	"log"
	"time"

	"catinello.eu/totp"
)

var MySecret string

func main() {
	token, err := totp.New(MySecret, 8, nil, 30)
	if err != nil {
		log.Fatal(err)
	}

	code, err := token.Generate(time.Now())
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(code)
}
Output:

func (*TOTP) Generate

func (t *TOTP) Generate(date time.Time) (Code, error)

Generate returns a code for the given date and TOTP.

Example
package main

import (
	"fmt"
	"log"
	"time"

	"catinello.eu/totp"
)

var Token totp.TOTP

func main() {
	code, err := Token.Generate(time.Now())
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(len(code))
}
Output:

8

func (*TOTP) Verify

func (t *TOTP) Verify(date time.Time, compare Code) (bool, error)

Verify takes a date and code value to compare against a generated token.

Example
package main

import (
	"fmt"
	"log"
	"time"

	"catinello.eu/totp"
)

var (
	Token    totp.TOTP
	ThisCode totp.Code
)

func main() {
	comp, err := Token.Verify(time.Now(), ThisCode)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(comp)
}
Output:

true

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳