Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Update the fix example in docs and include them in tests #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ git = "https://github.com/TimDumol/rust-otp"

```rust
// first argument is the secret, second argument is the counter
assert_eq!(make_hotp("base32secret3232".to_ascii(), 0), Some(260182));
assert_eq!(make_hotp("base32secret3232".to_ascii_uppercase().ok(), 0), Some(260182));

// first argument is the secret, followed by the time step in seconds (Google
// Authenticator uses a time step of 30), and then the skew in seconds
// (often used when calculating HOTPs for a sequence of consecutive
// time intervals, to deal with potential latency and desynchronization).
assert_eq!(make_totp("base32secret3232".to_ascii(), 30, 0), Some(260182)); // true on Unix epoch
assert_eq!(make_totp("base32secret3232".to_ascii_uppercase(), 30, 0).ok(), Some(260182)); // true on Unix epoch
```


Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub fn make_totp(secret: &str, time_step: u64, skew: i64) -> Result<u32, Error>

#[cfg(test)]
mod tests {
use super::{make_hotp, make_totp_helper};
use super::{make_hotp, make_totp, make_totp_helper, Error};

#[test]
fn hotp() {
Expand All @@ -86,5 +86,11 @@ mod tests {
assert_eq!(make_totp_helper("BASE32SECRET3232", 3600, 0, 7).unwrap(), 260182);
assert_eq!(make_totp_helper("BASE32SECRET3232", 30, 0, 35).unwrap(), 55283);
assert_eq!(make_totp_helper("BASE32SECRET3232", 1, -2, 1403).unwrap(), 316439);
assert!(
matches!(
make_totp("base32secret3232", 30, 0).unwrap_err(),
Error::InvalidSecret{..}
)
);
}
}