pub struct Totp<D>where
D: CoreProxy,
D::Core: HashMarker + UpdateCore + FixedOutputCore + BufferKindUser<BufferKind = Eager> + Default + Clone,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,{ /* 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>where
D: CoreProxy,
D::Core: HashMarker + UpdateCore + FixedOutputCore + BufferKindUser<BufferKind = Eager> + Default + Clone,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
impl<D> Totp<D>where
D: CoreProxy,
D::Core: HashMarker + UpdateCore + FixedOutputCore + BufferKindUser<BufferKind = Eager> + Default + Clone,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
Sourcepub fn from_bytes(secret: &[u8], interval: u32) -> Result<Self, InvalidLength>where
Self: Sized,
pub fn from_bytes(secret: &[u8], interval: u32) -> Result<Self, InvalidLength>where
Self: Sized,
Instantiate a TOTP instance
Sourcepub fn new(
secret: &[u8],
interval: u32,
timestamp: u64,
) -> Result<Self, InvalidLength>
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");
Sourcepub fn remaining_sec(&self) -> u32
pub fn remaining_sec(&self) -> u32
Remaining seconds of current token since instantiated
Trait Implementations§
Source§impl<D> GenerateOtp for Totp<D>where
D: CoreProxy,
D::Core: HashMarker + UpdateCore + FixedOutputCore + BufferKindUser<BufferKind = Eager> + Default + Clone,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
impl<D> GenerateOtp for Totp<D>where
D: CoreProxy,
D::Core: HashMarker + UpdateCore + FixedOutputCore + BufferKindUser<BufferKind = Eager> + Default + Clone,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
Auto Trait Implementations§
impl<D> Freeze for Totp<D>
impl<D> RefUnwindSafe for Totp<D>where
<D as CoreProxy>::Core: Sized + RefUnwindSafe,
<<<D as CoreProxy>::Core as BlockSizeUser>::BlockSize as ArrayLength<u8>>::ArrayType: RefUnwindSafe,
impl<D> Send for Totp<D>
impl<D> Sync for Totp<D>
impl<D> Unpin for Totp<D>
impl<D> UnwindSafe for Totp<D>where
<D as CoreProxy>::Core: Sized + UnwindSafe,
<<<D as CoreProxy>::Core as BlockSizeUser>::BlockSize as ArrayLength<u8>>::ArrayType: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more