otp

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

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

Go to latest
Published: Oct 20, 2024 License: MIT Imports: 6 Imported by: 0

README

OTP Package

This Go package provides implementations for One-Time Password (OTP) systems, including TOTP (Time-Based One-Time Password), HOTP (HMAC-Based One-Time Password), and OCRA (OATH Challenge-Response Algorithm). It offers a secure, flexible, and easy-to-use solution for enhancing user authentication.

Features

  • TOTP: Generates time-based one-time passwords.
  • HOTP: Generates one-time passwords based on a counter value.
  • OCRA: Implements the OATH Challenge-Response algorithm for user verification.
  • Ease of Use: Simple API for quick and easy integration.

Installation

go get github.com/hasan-kilici/otp

Usage

1. OTP Configuration
package main

import (
	"fmt"
	"log"
	"github.com/hasan-kilici/otp"
	"github.com/hasan-kilici/otp/utils"
)

func main() {
	otpConfig := otp.Config{
		Secret:      "JBSWY3DPEHPK3PXP",
		TimeInterval: 30,
		Digits:      6,
		Algorithm:   utils.AlgorithmSHA1,
	}

	otpOptions := otp.Options{
		Issuer:      utils.DefaultIssuer,
		AccountName: utils.DefaultAccount,
	}

	otpInstance := otp.New(otpConfig)

	otpCode, err := otpInstance.Generate(otpOptions)
	if err != nil {
		log.Fatalf("OTP Error: %v", err)
	}
	fmt.Println("OTP Code:", otpCode)
}

Description

  • Secret: The shared secret used for generating OTP.
  • TimeInterval: Duration for which the OTP is valid (in seconds).
  • Digits: Number of digits in the OTP.
  • Algorithm: Hashing algorithm used (e.g., SHA1).
2. TOTP Configuration
package main

import (
	"fmt"
	"log"
	"github.com/hasan-kilici/otp/totp"
	"github.com/hasan-kilici/otp/utils"
)

func main() {
	totpConfig := totp.Config{
		Secret:    "JBSWY3DPEHPK3PXP",
		TimeStep:  30,
		Digits:    6,
		Algorithm: utils.AlgorithmSHA1,
	}

	totpOptions := totp.Options{
		Issuer:      utils.DefaultIssuer,
		AccountName: utils.DefaultAccount,
	}

	totpInstance := totp.New(totpConfig)
	totpCode, err := totpInstance.Generate(totpOptions)
	if err != nil {
		log.Fatalf("TOTP Error: %v", err)
	}
	fmt.Println("TOTP Code:", totpCode)
}

Description

  • Secret: Shared secret for TOTP generation.
  • TimeStep: Time interval (in seconds) for which TOTP is valid.
  • Digits: Number of digits in the generated TOTP.
  • Algorithm: Hashing algorithm used (e.g., SHA1).
3. HOTP Configuration
package main

import (
	"fmt"
	"log"
	"github.com/hasan-kilici/otp/hotp"
	"github.com/hasan-kilici/otp/utils"
)

func main() {
	hotpConfig := hotp.Config{
		Secret:    "JBSWY3DPEHPK3PXP",
		Counter:   1,
		Digits:    6,
		Algorithm: utils.AlgorithmSHA256,
	}
	hotpOptions := hotp.Options{
		Issuer:      utils.DefaultIssuer,
		AccountName: utils.DefaultAccount,
	}
	hotpInstance := hotp.New(hotpConfig)
	hotpCode, err := hotpInstance.Generate(hotpOptions)
	if err != nil {
		log.Fatalf("HOTP Error: %v", err)
	}
	fmt.Println("HOTP Code:", hotpCode)
}

Description

  • Secret: Shared secret for generating HOTP.
  • Counter: Counter value for HOTP generation.
  • Digits: Number of digits in the generated HOTP.
  • Algorithm: Hashing algorithm used (e.g., SHA256).
4. OCRA Configuration

package main

import (
	"fmt"
	"log"
	"github.com/hasan-kilici/otp/ocra"
	"github.com/hasan-kilici/otp/utils"
)

func main() {
	ocraConfig := ocra.Config{
		SecretKey:   "JBSWY3DPEHPK3PXP",
		Suite:       "OCRA-1",
		Counter:     "1",
		Algorithm:   utils.AlgorithmSHA512,
	}
	ocraOptions := ocra.Options{
		Issuer:      utils.DefaultIssuer,
		AccountName: utils.DefaultAccount,
	}
	ocraInstance := ocra.New(ocraConfig)
	ocraCode, err := ocraInstance.Generate(ocraOptions)
	if err != nil {
		log.Fatalf("OCRA Error: %v", err)
	}
	fmt.Println("OCRA Code:", ocraCode)
}

Description

  • SecretKey: Shared secret key for OCRA.
  • Suite: The OCRA suite being used (e.g., OCRA-1).
  • Counter: Counter value for OCRA generation.
  • Algorithm: Hashing algorithm used (e.g., SHA512).

References

Documentation

Overview

Package otp provides an implementation of one-time password (OTP) generation using TOTP, HOTP, and OCRA algorithms.

The OTP generation is based on the following RFCs:

- RFC 4226: HOTP: An HMAC-Based One-Time Password Algorithm - RFC 6238: TOTP: Time-Based One-Time Password Algorithm - RFC 6287: OCRA: OATH Challenge-Response Algorithm

## Overview

This package contains structures and methods for generating OTPs using different algorithms:

1. **TOTP** (Time-based One-Time Password) 2. **HOTP** (HMAC-based One-Time Password) 3. **OCRA** (OATH Challenge-Response Algorithm)

## Usage

To use the package, create a configuration for the desired OTP type, set the necessary parameters, and call the Generate method to obtain the OTP.

### Example

package main

import (

"fmt"
"log"
"github.com/hasan-kilici/otp/totp"
"github.com/hasan-kilici/otp/ocra"
"github.com/hasan-kilici/otp/hotp"
"github.com/hasan-kilici/otp/utils"

)

func main() {
	// TOTP Example
	totpConfig := totp.Config{
		Secret:    "JBSWY3DPEHPK3PXP",
		TimeStep:  30,
		Digits:    6,
		Algorithm: utils.AlgorithmSHA1,
	}
	totpOptions := totp.Options{
		Issuer:      utils.DefaultIssuer,
		AccountName: utils.DefaultAccount,
	}
	totpInstance := totp.New(totpConfig)
	totpCode, err := totpInstance.Generate(totpOptions)
	if err != nil {
		log.Fatalf("TOTP Error: %v", err)
	}
	fmt.Println("TOTP Code:", totpCode)

	// HOTP Example
	hotpConfig := hotp.Config{
		Secret:    "JBSWY3DPEHPK3PXP",
		Counter:   1,
		Digits:    6,
		Algorithm: utils.AlgorithmSHA256,
	}
	hotpOptions := hotp.Options{
		Issuer:      utils.DefaultIssuer,
		AccountName: utils.DefaultAccount,
	}
	hotpInstance := hotp.New(hotpConfig)
	hotpCode, err := hotpInstance.Generate(hotpOptions)
	if err != nil {
		log.Fatalf("HOTP Error: %v", err)
	}
	fmt.Println("HOTP Code:", hotpCode)

	// OCRA Example
	ocraConfig := ocra.Config{
		SecretKey:   "JBSWY3DPEHPK3PXP",
		Suite:       "OCRA-1",
		Counter:     "1",
		Algorithm:   utils.AlgorithmSHA512,
	}
	ocraOptions := ocra.Options{
		Issuer:      utils.DefaultIssuer,
		AccountName: utils.DefaultAccount,
	}
	ocraInstance := ocra.New(ocraConfig)
	ocraCode, err := ocraInstance.Generate(ocraOptions)
	if err != nil {
		log.Fatalf("OCRA Error: %v", err)
	}
	fmt.Println("OCRA Code:", ocraCode)
}

## Structures

### Config

The Config structure contains the settings necessary for OTP generation:

- Secret: The shared secret used for generating OTPs. - TimeStep: The time interval for TOTP (in seconds). - Counter: The counter value for HOTP. - Digits: The number of digits in the generated OTP. - Algorithm: The hash algorithm used (SHA1, SHA256, SHA512).

### Options

The Options structure contains optional parameters for OTP generation:

- Issuer: The name of the application issuing the OTP. - AccountName: The account name for which the OTP is generated.

## License

This project is licensed under the MIT License. See the LICENSE file for more details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Secret       string
	TimeInterval int64
	Digits       int
	Algorithm    string
}

func New

func New(config Config) *Config

func (*Config) Generate

func (otp *Config) Generate(options Options) (string, error)

type Options

type Options struct {
	Issuer      string
	AccountName string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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