1 stable release
1.0.5 | May 17, 2022 |
---|
#1041 in Authentication
11KB
127 lines
totp_embed
This is a fork of original totp-lite with some changes on API and added support for no_std
.
fn totp1_tests() {
let secret: &[u8] = b"12345678901234567890";
assert_eq!(20, secret.len());
let pairs = vec![
(94287082, 59),
(07081804, 1111111109),
(14050471, 1111111111),
(89005924, 1234567890),
(69279037, 2000000000),
(65353130, 20000000000),
];
pairs.into_iter().for_each(|(expected, time)| {
assert_eq!(expected, totp::<Sha1>(secret, time));
});
}
See totp-lite for more details.
License: MIT
lib.rs
:
A simple, correct TOTP library.
Time-based One-time Passwords are a useful way to authenticate a client, since a valid password expires long before it could ever be guessed by an attacker. This library provides an implementation of TOTP that matches its specification RFC6238, along with a simple interface.
Usage
The totp
function is likely what you need. It uses the default time step
of 30 seconds and produces 8 digits of output:
use std::time::{SystemTime, UNIX_EPOCH};
use totp_embed::{totp, Sha512};
// Negotiated between you and the authenticating service.
let password: &[u8] = b"secret";
// The number of seconds since the Unix Epoch.
let seconds: u64 = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
// Specify the desired Hash algorithm via a type parameter.
// `Sha1` and `Sha256` are also available.
let result: u64 = totp::<Sha512>(password, seconds);
For full control over how the algorithm is configured, consider
totp_custom
.
Resources
Dependencies
~540KB
~11K SLoC