I am trying to build a simple tool that takes an initial TOTP-seed and outputs the 6-digit code for the current timestamp.
I used IT Tools - Handy online tools for developers to generate a seed/secret and test my program, but I get an invalid token. This is my code so far:
use totp_rs::{Algorithm, TOTP};
fn main() {
// just some random seed, hardcoded for testing
let seed = "HFNTDOQHB3OAGL6V".to_string();
let token_generator = TOTP::new(
Algorithm::SHA1,
6,
0,
30,
totp_rs::Secret::Raw(seed.as_bytes().to_vec())
.to_bytes()
.unwrap(),
)
.unwrap();
print!("token: {}\n", token_generator.generate_current().unwrap());
}
and
[dependencies]
totp-rs = "5.6.0"
I also tried to use Secret::Encoded instead of Secret::Raw, but that only results in a panic from an unwrap.
I would greatly appreciate any assistance you can provide regarding this issue. Thank you in advance for your support!
totp-rs uses base32::decode(base32::Alphabet::Rfc4648 { padding: false }, value.as_ref()) to decode a secret from a otpauth:// url. You probably need to do the same.
The totp-rs crate enforces the secret size >= 128 since version 3.0. If you use 2.0.1 it will work with the shorter secret. (You'll have to tweak the TOTP::new() call, which has extra arguments, and convert the secret to raw bytes beforehand.)
The minimum secret size constraint is unfortunate, despite being RFC compliant, since all those authenticator apps use 80-bit secrets to this day.
Thanks for the insights. As I said, it was fixed by just entering a larger value.
But my main problem still remains: the tokens that my program outputs do not match the tokens of the website.