3个版本 (破坏性更新)
0.4.0 | 2022年1月29日 |
---|---|
0.3.0 | 2022年1月27日 |
0.2.0 |
|
0.1.0 | 2022年1月16日 |
#944 in 加密学
28KB
285 行
xotp
使用方法
使用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);
}
使用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);
}
变更日志
此crate的变更日志可以在CHANGELOG.md中找到
功能和错误
请通过问题跟踪器提交任何功能请求或错误报告
许可
- xotp遵循MIT许可证
依赖
~2MB
~59K SLoC