#totp #hmac #2fa #otp #generate #read-write

totp_rfc6238

根据RFC 6238定义生成基于时间的单次密码(TOTP)代码/令牌的库

9个版本

0.6.1 2024年8月15日
0.5.3 2024年4月4日
0.5.1 2022年10月15日
0.5.0 2021年8月13日
0.4.2 2021年5月13日

身份验证 中排名 61

Download history 25/week @ 2024-05-20 6/week @ 2024-06-03 10/week @ 2024-06-10 5/week @ 2024-06-17 15/week @ 2024-06-24 29/week @ 2024-07-01 129/week @ 2024-07-29 221/week @ 2024-08-12

每月下载量 350

MIT/Apache

56KB
815

totp_rfc6238

Rust用于生成RFC 6238定义的TOTP代码(令牌)的crate。

crates.io docs.rs Rust-test

此crate的特点

  • 提供低级和高级API。
  • 代码长度、初始计数器时间(T0)、更新时间间隔(周期)和哈希算法可配置。
  • 通过RustCryptoring实现HMAC算法。
  • 读取或写入"密钥URI格式"(URI以otpauth://totp/开头)(oathuri功能门)。
  • 从base32编码的字符串中读取或写入keyoathuri功能门)。

选择SHA实现

  • 使用RustCrypto的实现(默认)
    [dependencies]
    totp_rfc6238 = "0.6"
    
  • 使用Ring的实现
    [dependencies]
    totp_rfc6238 = { version = "0.6", default-features = false, features = ["ring"] }
    

注意

此实现不考虑早于Unix纪元1970-01-01T00:00:00Z)的时间。

示例

use totp_rfc6238::{HashAlgorithm, TotpGenerator};
fn main() {
    // Create a standard TOTP code generator: 6-digit, updating every
    // 30 seconds, starting at "Jan 01 1970 00:00:00 UTC", using HMAC-SHA1.
    let mut totp_generator = TotpGenerator::new().build();

    // Assuming you have read the key from somewhere secure
    let key = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890+/";

    let output1 = totp_generator.get_code(key);
    println!("Your TOTP code for current time is: {}", output1);

    let output2 = totp_generator.get_next_update_time().unwrap();
    println!("Next update will be at the unix timestamp of {}", output2);

    let output3 = totp_generator.get_code_window(key, -4..=4).unwrap();
    println!("Codes for 2 minutes earlier or later are:");
    for i in output3 {
        println!("  {}", i);
    }

    // You can also create a non-standard TOTP code generator: 8-digit,
    // updating every 90 seconds, starting at "Jan 01 1970 00:16:42 UTC",
    // using HMAC-SHA512.
    let mut another_totp_generator = TotpGenerator::new()
        .set_digit(8).unwrap()
        .set_step(90).unwrap()
        .set_t0(16 * 60 + 42)
        .set_hash_algorithm(HashAlgorithm::SHA512)
        .build();

    let output4 = another_totp_generator.get_code(key);
    println!("Your non-standard TOTP code for current time is: {}", output4);
}

变更日志

请参阅此处

不兼容的API破坏性更改

版本号低于1.0.0应视为API的不稳定版本。因此,一些版本更新可能包含不兼容的API更改。在更改依赖版本时,请参阅以下内容。

  • v0.5.2(未发布)-> v0.5.3:在版本0.5.2中,oath_uri::TotpUrioath_uri::KeyInfo实现了Debug特质,但在0.5.3中删除了它。(仅影响oathuri功能)
  • v0.4.2 -> v0.5.0: 错误的数据类型已更改。(仅影响 oathuri 功能)
  • v0.3.1 -> v0.4.0: 错误和函数名称的数据类型已更改。(仅影响 oathuri 功能)
  • v0.2.0 -> v0.3.0: 在百分比编码中,需要转义的字符已更改。(仅影响 oathuri 功能)

警告

此crate的代码尚未经过审计。

  • 读取或写入QR码。
  • 基于HMAC的一次性密码(HOTP)算法,如RFC 4226中定义。

许可协议

此工具主要在MIT许可协议和Apache License(版本2.0)的条款下分发,部分受各种BSD-like许可协议的保护。
有关详细信息,请参阅LICENSE-APACHELICENSE-MIT

贡献

  1. 您有意提交以包含在 totp_rfc6238 中的任何贡献,根据Apache-2.0许可协议,应按上述方式双重许可,不附加任何额外条款或条件。
  2. 欢迎提交拉取请求。

依赖项

~0–9MB
~93K SLoC