Struct Totp

Source
pub struct Totp<D>{ /* private fields */ }
Expand description

TOTP

Implementation of TOTP in RFC 6238.

§Examples

§Common Usage

use minotp::*;
use sha1::Sha1;

let secret = b"test";

let totp = Totp::<Sha1>::from_bytes(secret, COMMON_INTERVAL).unwrap();

// Get remaining seconds
let _remaining_seconds = totp.remaining_sec();

// Get token
let _token = totp.gen_6_str();

// -- snip -- //

§Base32 secret

Use a third-party crate (e.g., data_encoding) to decode your secret if your secret is Base32 encoded.

use data_encoding::BASE32;
use minotp::*;
use sha1::Sha1;

let secret_base32_str = "ORSXG5A=";

let secret = BASE32.decode(secret_base32_str.as_bytes()).unwrap();

let totp = Totp::<Sha1>::from_bytes(&secret, COMMON_INTERVAL).unwrap();

let _token = totp.gen_6_str();

// -- snip -- //

Implementations§

Source§

impl<D> Totp<D>

Source

pub fn from_bytes(secret: &[u8], interval: u32) -> Result<Self, InvalidLength>
where Self: Sized,

Instantiate a TOTP instance

Source

pub fn new( secret: &[u8], interval: u32, timestamp: u64, ) -> Result<Self, InvalidLength>

Instantiate a TOTP instance with custom timestamp

§Examples
use minotp::*;
use sha1::Sha1;

let secret = b"test";
let interval = 75;
let unix_timestamp = 112345u64;

let totp = Totp::<Sha1>::new(secret, interval, unix_timestamp).unwrap();

assert_eq!(totp.gen_6_str(), "677062");
Source

pub fn interval(&self) -> u32

Interval of current TOTP instance in seconds

Source

pub fn remaining_sec(&self) -> u32

Remaining seconds of current token since instantiated

Trait Implementations§

Source§

impl<D> GenerateOtp for Totp<D>

Source§

fn gen(self, digits: u8) -> u32

Generate an OTP token as an unsigned number Read more
Source§

fn gen_str(self, digits: u8) -> String
where Self: Sized,

Generate an OTP token as a string Read more

Auto Trait Implementations§

§

impl<D> Freeze for Totp<D>

§

impl<D> RefUnwindSafe for Totp<D>

§

impl<D> Send for Totp<D>
where <D as CoreProxy>::Core: Sized + Send,

§

impl<D> Sync for Totp<D>
where <D as CoreProxy>::Core: Sized + Sync,

§

impl<D> Unpin for Totp<D>

§

impl<D> UnwindSafe for Totp<D>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> GenerateOtpDefault for T
where T: GenerateOtp,

Source§

fn gen_4(self) -> u16

Generate 4-digit OTP as an unsigned number Read more
Source§

fn gen_6(self) -> u32

Generate 6-digit OTP as an unsigned number Read more
Source§

fn gen_8(self) -> u32

Generate 8-digit OTP as an unsigned number Read more
Source§

fn gen_4_str(self) -> String

Generate 4-digit OTP as a string Read more
Source§

fn gen_6_str(self) -> String

Generate 6-digit OTP as a string Read more
Source§

fn gen_8_str(self) -> String

Generate 8-digit OTP as a string Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Verify for T
where T: GenerateOtp,

Source§

fn verify(self, input: u32, digits: u8) -> bool

Verify the input token as a u32 Read more
Source§

fn verify_str(self, input: &str, digits: u8) -> bool

Verify a token as a string slice Read more