Skip to content

Commit 56bd72e

Browse files
committed
make HOTP and TOTP return the OTPResult type (Will break tests)
1 parent ef759fe commit 56bd72e

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

src/hotp.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Implementation of the HOTP standard according to RFC4226 by Tejas Mehta
22

3+
use crate::otp_result::OTPResult;
34
use crate::util::{base32_decode, get_code, hash_generic, MacDigest};
45

56
/// A HOTP Generator
@@ -101,13 +102,14 @@ impl HOTP {
101102
///
102103
/// # Panics
103104
/// This method panics if the hash's secret is incorrectly given.
104-
pub fn get_otp(&self, counter: u64) -> u32 {
105+
pub fn get_otp(&self, counter: u64) -> OTPResult {
105106
let hash = hash_generic(&counter.to_be_bytes(), &self.secret, &MacDigest::SHA1);
106107
let offset = (hash[hash.len() - 1] & 0xf) as usize;
107108
let bytes: [u8; 4] = hash[offset..offset + 4]
108109
.try_into()
109110
.expect("Failed byte get");
110111

111-
get_code(bytes, self.digits)
112+
let code = get_code(bytes, self.digits);
113+
OTPResult::new(self.digits, code)
112114
}
113115
}

src/otp_result.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::time::{SystemTime, UNIX_EPOCH};
22

3-
struct OTPResult {
3+
pub struct OTPResult {
44
digits: u32,
55
code: u32,
66
}

src/totp.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::otp_result::OTPResult;
12
use crate::util::{base32_decode, get_code, hash_generic, MacDigest};
23

34
/// A TOTP generator
@@ -158,7 +159,7 @@ impl TOTP {
158159
/// # Panics
159160
/// This method panics if the [`TOTP::get_otp_with_custom_time_start`]
160161
/// method does, which happens if the hash's secret is incorrectly given.
161-
pub fn get_otp(&self, time: u64) -> u32 {
162+
pub fn get_otp(&self, time: u64) -> OTPResult {
162163
self.get_otp_with_custom_time_start(time, 0)
163164
}
164165

@@ -171,7 +172,7 @@ impl TOTP {
171172
///
172173
/// # Panics
173174
/// This method panics if the hash's secret is incorrectly given.
174-
pub fn get_otp_with_custom_time_start(&self, time: u64, time_start: u64) -> u32 {
175+
pub fn get_otp_with_custom_time_start(&self, time: u64, time_start: u64) -> OTPResult {
175176
let time_count = (time - time_start) / self.period;
176177

177178
let hash = hash_generic(&time_count.to_be_bytes(), &self.secret, &self.mac_digest);
@@ -180,6 +181,8 @@ impl TOTP {
180181
.try_into()
181182
.expect("Failed byte get");
182183

183-
get_code(bytes, self.digits)
184+
185+
let code = get_code(bytes, self.digits);
186+
OTPResult::new(self.digits, code)
184187
}
185188
}

0 commit comments

Comments
 (0)