Skip to content
Merged
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
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ categories = ["authentication"]
github = { repository = "fosskers/totp-lite", branch = "master", workflow = "Rust" }

[dependencies]
digest = "0.9"
hmac = "0.11"
sha-1 = "0.9"
sha2 = "0.9"
digest = "0.10"
hmac = "0.12"
sha-1 = "0.10"
sha2 = "0.10"

[dev-dependencies]
version-sync = "0.9"
Expand Down
2 changes: 1 addition & 1 deletion examples/ga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn main() {
// Calculate a 6 digit code.
6,
// Convert the secret into bytes using base32::decode().
&base32::decode(input.trim().to_lowercase().to_string()).unwrap(),
&base32::decode(&input.trim().to_lowercase()).unwrap(),
// Seconds since the Unix Epoch.
seconds,
)
Expand Down
33 changes: 27 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@

#![doc(html_root_url = "https://docs.rs/totp-lite/1.0.3")]

use digest::{BlockInput, FixedOutputDirty, Reset, Update};
use hmac::{Hmac, Mac, NewMac};
use digest::{
block_buffer::Eager,
core_api::{BlockSizeUser, BufferKindUser, CoreProxy, FixedOutputCore, UpdateCore},
generic_array::typenum::{IsLess, Le, NonZero, U256},
FixedOutput, HashMarker, Update,
};
use hmac::{Hmac, Mac};
pub use sha1::Sha1;
pub use sha2::{Sha256, Sha512};

Expand Down Expand Up @@ -68,7 +73,15 @@ pub const DEFAULT_DIGITS: u32 = 8;
/// ```
pub fn totp<H>(secret: &[u8], time: u64) -> String
where
H: Update + BlockInput + Reset + FixedOutputDirty + Clone + Default,
H: Update + FixedOutput + CoreProxy,
H::Core: HashMarker
+ UpdateCore
+ FixedOutputCore
+ BufferKindUser<BufferKind = Eager>
+ Default
+ Clone,
<H::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<H::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
Comment on lines +76 to +84
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain these new constraints?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mainly copy-pasted them from Hmac https://docs.rs/hmac/latest/src/hmac/optim.rs.html#20-36

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks I'll review these.

{
totp_custom::<H>(DEFAULT_STEP, DEFAULT_DIGITS, secret, time)
}
Expand All @@ -91,11 +104,19 @@ where
/// ```
pub fn totp_custom<H>(step: u64, digits: u32, secret: &[u8], time: u64) -> String
where
H: Update + BlockInput + Reset + FixedOutputDirty + Clone + Default,
H: Update + FixedOutput + CoreProxy,
H::Core: HashMarker
+ UpdateCore
+ FixedOutputCore
+ BufferKindUser<BufferKind = Eager>
+ Default
+ Clone,
<H::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<H::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
{
// Hash the secret and the time together.
let mut mac: Hmac<H> = Hmac::new_from_slice(secret).unwrap();
mac.update(&to_bytes(time / step));
let mut mac = <Hmac<H> as Mac>::new_from_slice(secret).unwrap();
<Hmac<H> as Update>::update(&mut mac, &to_bytes(time / step));
Comment on lines +118 to +119
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is update no longer a method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is still a method, but both the Update and the UpdateCore traits implement update, so I had to let the compiler know which of the two implementations to use

let hash: &[u8] = &mac.finalize().into_bytes();

// Magic from the RFC.
Expand Down