otp

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

Go (Golang) One Time Password (OTP) Library

This library helps to implementing Time-Based One Time Password (TOTP) and HMAC-Based One Time Password (HOTP) algorithms with SHA-1. It produces 6 digits code and 16 digits secret and validates it . Also give you an URL to use it in any authenticator applications like Google Authenticator. The Library supports synchronization (TOTP = 1 min, HOTP = 3 turn)

Example

Generate Code

package main

import (
	"fmt"
	"github.com/batuhanay97/otp"
)

func main() {
	code, secret, url, err := otp.Generate(otp.TOTP, <issuer>, <username>)
	fmt.Println("TOTP: ", code, secret, url, err)
    	// TOTP:  <code> <secret> otpauth://totp/<issuer>:<username>?algorithm=SHA1&digits=6&issuer=<issuer>&period=30&secret=<secret> <err>

	code, secret, url, err = otp.Generate(otp.HOTP, <issuer>, <username>, <counter>)
	fmt.Println("HOTP: ", code, secret, url, err)
    	// HOTP:  <code> <secret> otpauth://hotp/<issuer>:<username>?algorithm=SHA1&digits=6&issuer=<issuer>&secret=<secret> <err>
}

Validate Code

package main

import (
	"fmt"
	"github.com/batuhanay97/otp"
)

func main() {
	valid, err := otp.Validate(otp.TOTP, <code>, <secret>)
	fmt.Println("TOTP: ", valid, err)
    	// TOTP:  true <nil>

	valid, err = otp.Validate(otp.HOTP, <code>, <secret>, <counter>)
	fmt.Println("HOTP: ", valid, err)
    	// HOTP:  true <nil>
}

License

otp is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(provider SourceType, issuer, username string, counter ...uint64) (string, string, string, error)

func Validate

func Validate(provider SourceType, code, secret string, counter ...uint64) (bool, error)

Types

type SourceType

type SourceType string
const (
	TOTP SourceType = "totp"
	HOTP SourceType = "hotp"
)

Directories

Path Synopsis
service
otp

Jump to

Keyboard shortcuts

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