Skip to main content

gen_totp_sha1

Function gen_totp_sha1 

Source
pub fn gen_totp_sha1(
    base32_key: &str,
    unix_ts: i64,
    period_sec: u32,
) -> Option<String>
Expand description

生成 6 位 TOTP auth_token。对齐 C++ GenGoogleOTPCode_SHA1 (OMBase_API_OTP.cpp:12):

  • 计数器 = (unix_time / period) 作大端 u64 [u8; 8]
  • mac = HMAC-SHA1(base32_decode(key), counter_bytes)
  • offset = mac[19] & 0x0f
  • truncated = u32::from_be_bytes(mac[offset..offset+4]) & 0x7fff_ffff
  • code = truncated % 1_000_000(6 位补零)