//! RFC 6238 Appendix B and RFC 4226 Appendix D test vectors, exactly as published, plus otpauth parsing. use custos_totp::{Algorithm, Totp, hotp}; const SEED_SHA1: &[u8] = b"12345678901234567890"; const SEED_SHA256: &[u8] = b"12345678901234567890123456789012"; const SEED_SHA512: &[u8] = b"1234567890123456789012345678901234567890123456789012345678901234"; #[test] fn rfc6238_appendix_b() { // Time (s), SHA1, SHA256, SHA512 — 8 digits, period 30, T0 = 0. let table: [(u64, &str, &str, &str); 6] = [ (59, "94287082", "46119246", "90693936"), (1111111109, "07081804", "68084774", "25091201"), (1111111111, "14050471", "67062674", "99943326"), (1234567890, "89005924", "91819424", "93441116"), (2000000000, "69279037", "90698825", "38618901"), (20000000000, "65353130", "77737706", "47863826"), ]; for (t, sha1, sha256, sha512) in table { for (seed, alg, want) in [ (SEED_SHA1, Algorithm::Sha1, sha1), (SEED_SHA256, Algorithm::Sha256, sha256), (SEED_SHA512, Algorithm::Sha512, sha512), ] { let totp = Totp::new(seed.to_vec(), alg, 8, 30).unwrap(); assert_eq!(totp.code_at(t), want, "T={t} {alg:?}"); } } } #[test] fn rfc4226_appendix_d() { let want = ["755224", "287082", "359152", "969429", "338314", "254676", "287922", "162583", "399871", "520489"]; for (counter, code) in want.iter().enumerate() { assert_eq!(hotp(SEED_SHA1, counter as u64, 6, Algorithm::Sha1), *code, "counter {counter}"); } } #[test] fn otpauth_uri_and_base32() { // "12345678901234567890" in base32. let b32 = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"; let t = Totp::parse(&format!( "otpauth://totp/Example%20Co:alice%40example.com?secret={b32}&issuer=Example%20Co&algorithm=SHA1&digits=8&period=30" )) .unwrap(); assert_eq!(t.code_at(59), "94287082"); assert_eq!(t.issuer(), Some("Example Co")); assert_eq!(t.account(), Some("alice@example.com")); assert_eq!((t.digits(), t.period(), t.algorithm()), (8, 30, Algorithm::Sha1)); // Defaults: SHA1, 6 digits, 30 s. A bare secret (lower case, spaces, padding) works too. let bare = Totp::parse("gezd gnbv gy3t qojq gezd gnbv gy3t qojq").unwrap(); assert_eq!(bare.code_at(59), "287082"); assert_eq!((bare.digits(), bare.period()), (6, 30)); assert_eq!(Totp::parse(&format!("{b32}====")).unwrap().code_at(59), "287082"); let sha256 = Totp::parse( "otpauth://totp/x?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZA&algorithm=SHA256&digits=8", ) .unwrap(); assert_eq!(sha256.code_at(59), "46119246"); for bad in [ "otpauth://hotp/x?secret=GEZDGNBV&counter=1", "otpauth://totp/x?digits=6", "otpauth://totp/x?secret=not*base32", "otpauth://totp/x?secret=GEZDGNBV&digits=9", "otpauth://totp/x?secret=GEZDGNBV&period=0", "otpauth://totp/x?secret=GEZDGNBV&algorithm=MD5", "", ] { assert!(Totp::parse(bad).is_err(), "{bad} was accepted"); } } #[test] fn round_trip_through_uri_and_countdown() { let t = Totp::parse("otpauth://totp/GitHub:mark?secret=JBSWY3DPEHPK3PXP&issuer=GitHub").unwrap(); let back = Totp::parse(&t.to_uri()).unwrap(); assert_eq!(back.code_at(1_700_000_000), t.code_at(1_700_000_000)); assert_eq!(back.issuer(), Some("GitHub")); assert_eq!(t.seconds_left(1_700_000_000), 30 - 1_700_000_000 % 30); assert_eq!(t.seconds_left(60), 30); } #[test] fn secret_is_redacted_in_debug() { let t = Totp::parse("otpauth://totp/x?secret=JBSWY3DPEHPK3PXP").unwrap(); let out = format!("{t:?}"); assert!(!out.contains("JBSWY3DPEHPK3PXP") && !out.contains("72, 101") && out.contains("[redacted]"), "{out}"); }