GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - tmthecoder/xotp: An HOTP & TOTP implementation in Rust

BranchesTags
Open more actions menu

Latest commit

 

History

58 Commits
58 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
.idea
.idea
 
 
src
src
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
CHANGELOG.md
CHANGELOG.md
 
 
Cargo.toml
Cargo.toml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 

Repository files navigation

xotp

crate xotp License: MIT

A Rust implementation the HOTP and TOTP Algorithms.

  • HOTP was implemented in accordance with RFC4226
  • TOTP was implemented in accordance with RFC6238

Usage

To use HOTP:

use xotp::hotp::HOTP;

fn get_otp_with_hotp() {
    let secret = "secret";
    let counter = 0;
    // Get a HOTP instance with a '&str' secret
    let hotp_str = HOTP::default_from_utf8(secret);
    // Get an otp with the given counter
    let otp_from_str = hotp_str.get_otp(counter);
    println!("The otp from hotp_str: {}", otp_from_str);

    // Alternatively, get a HOTP instance with a '&[u8]' secret
    let hotp_bytes = HOTP::new(secret.as_bytes(), 6);
    // Get an otp with the given counter
    let otp_from_bytes = hotp_bytes.get_otp(counter);
    println!("The otp from hotp_bytes: {}", otp_from_bytes);
}

To use TOTP:

use xotp::totp::TOTP;
use xotp::util::MacDigest; // Only needed if using a non-SHA1 hash function
use std::time::{Duration, SystemTime, UNIX_EPOCH};

fn get_otp_with_totp() {
    let secret = "secret";
    let elapsed_seconds = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("Error getting time")
        .as_secs();
    // Get a TOTP instance with an '&str' secret and default SHA1 Digest
    let totp_sha1_str = TOTP::default_from_utf8(secret);
    // Get an otp with the given counter and elapsed seconds
    let otp_sha1 = totp_sha1_str.get_otp(elapsed_seconds);
    println!("The otp from totp_sha1_str: {}", otp_sha1);

    // Alternatively get a TOTP instance with an '&[u8]' secret
    // and different digest (Sha256 or Sha512)
    let totp_sha256_bytes = TOTP::new(
        secret.as_bytes(),
        MacDigest::SHA256, // SHA256 algorithm
        8,  // 8 digits
        60  // 60-second interval
    );
    // Get an otp with the given counter, time and other custom params
    let otp_sha256 = totp_sha256_bytes.get_otp_with_custom_time_start(
        elapsed_seconds,
        0, // Start time at unix epoch
    );
    println!("The otp from totp_sha256_bytes: {}", otp_sha256);
}

Changelog

The changelog for this crate can be found at CHANGELOG.md

Features and Bugs

Please file any featre requests or bug reports through the issue tracker

Licensing

  • xotp is licensed under the MIT License

About

An HOTP & TOTP implementation in Rust

Resources

Readme
MIT license
Activity

Stars

5 stars

Watchers

1 watching

Forks

3 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.