Skip to content

Commit e10a2b1

Browse files
committed
cargo formatted code
1 parent d4a8a05 commit e10a2b1

File tree

6 files changed

+56
-40
lines changed

6 files changed

+56
-40
lines changed

src/hotp.rs

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

3-
use base32::Alphabet;
43
use crate::util::{get_code, hash_generic, MacDigest};
4+
use base32::Alphabet;
55

66
/// A HOTP Generator
77
///
@@ -35,9 +35,9 @@ impl HOTP {
3535
/// Since only SHA1 was specified in the reference implementation and
3636
/// RFC specification, there's no need to initialize with a digest object
3737
pub fn new(secret: &[u8]) -> Self {
38-
HOTP {
39-
secret: secret.to_vec()
40-
}
38+
HOTP {
39+
secret: secret.to_vec(),
40+
}
4141
}
4242

4343
/// Creates a new HOTP instance from a utf8-encoded string secret
@@ -55,9 +55,8 @@ impl HOTP {
5555
/// This method panics if the provided string is not correctly base32
5656
/// encoded.
5757
pub fn from_base32(secret: &str) -> Self {
58-
let decoded = base32::decode(
59-
Alphabet::RFC4648 { padding: false }, secret
60-
).expect("Failed to decode base32 string");
58+
let decoded = base32::decode(Alphabet::RFC4648 { padding: false }, secret)
59+
.expect("Failed to decode base32 string");
6160
HOTP::new(&decoded)
6261
}
6362
}
@@ -72,12 +71,12 @@ impl HOTP {
7271
/// # Panics
7372
/// This method panics if the hash's secret is incorrectly given.
7473
pub fn get_otp(&self, counter: u64, digits: u32) -> u32 {
75-
7674
let hash = hash_generic(&counter.to_be_bytes(), &self.secret, &MacDigest::SHA1);
77-
let offset = (hash[hash.len()-1] & 0xf) as usize;
78-
let bytes: [u8; 4] = hash[offset..offset + 4].try_into().expect("Failed byte get");
75+
let offset = (hash[hash.len() - 1] & 0xf) as usize;
76+
let bytes: [u8; 4] = hash[offset..offset + 4]
77+
.try_into()
78+
.expect("Failed byte get");
7979

8080
get_code(bytes, digits)
8181
}
82-
83-
}
82+
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@
7575
7676
pub mod hotp;
7777
pub mod totp;
78-
pub mod util;
78+
pub mod util;

src/totp.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use base32::Alphabet;
21
use crate::util::{get_code, hash_generic, MacDigest};
2+
use base32::Alphabet;
33

44
/// A TOTP generator
55
///
@@ -29,7 +29,7 @@ pub struct TOTP {
2929
///
3030
/// Unless an initializer ending in 'with_digest' is used, this value
3131
/// defaults to [`MacDigest::SHA1`]
32-
mac_digest: MacDigest
32+
mac_digest: MacDigest,
3333
}
3434

3535
/// All initializer implementations for the [`TOTP`] struct
@@ -50,7 +50,7 @@ impl TOTP {
5050
pub fn new_with_digest(secret: &[u8], mac_digest: MacDigest) -> Self {
5151
TOTP {
5252
secret: secret.to_vec(),
53-
mac_digest
53+
mac_digest,
5454
}
5555
}
5656

@@ -59,7 +59,7 @@ impl TOTP {
5959
/// Like [`TOTP::new`], this method also defaults to using [`MacDigest::SHA1`]
6060
/// for HMAC operations.
6161
pub fn from_utf8(secret: &str) -> Self {
62-
TOTP::from_utf8_with_digest(secret, MacDigest::SHA1)
62+
TOTP::from_utf8_with_digest(secret, MacDigest::SHA1)
6363
}
6464

6565
/// Creates a new TOTP instance from a utf8-encoded string secret
@@ -90,9 +90,8 @@ impl TOTP {
9090
/// # Panics
9191
/// This method panics if the provided string is not correctly base32 encoded.
9292
pub fn from_base32_with_digest(secret: &str, mac_digest: MacDigest) -> Self {
93-
let decoded = base32::decode(
94-
Alphabet::RFC4648 { padding: false }, secret
95-
).expect("Failed to decode base32 string");
93+
let decoded = base32::decode(Alphabet::RFC4648 { padding: false }, secret)
94+
.expect("Failed to decode base32 string");
9695
TOTP::new_with_digest(&decoded, mac_digest)
9796
}
9897
}
@@ -113,7 +112,7 @@ impl TOTP {
113112
/// This method panics if the called [`TOTP::get_otp_with_custom`] method
114113
/// does, which would happen if the hash's secret is incorrectly given.
115114
pub fn get_otp(&self, time: u64, digits: u32) -> u32 {
116-
self.get_otp_with_custom(time, 30, 0, digits)
115+
self.get_otp_with_custom(time, 30, 0, digits)
117116
}
118117

119118
/// Generates and returns the TOTP value for the time with a provided step,
@@ -126,13 +125,21 @@ impl TOTP {
126125
///
127126
/// # Panics
128127
/// This method panics if the hash's secret is incorrectly given.
129-
pub fn get_otp_with_custom(&self, time: u64, time_step: u64, time_start: u64, digits: u32) -> u32 {
128+
pub fn get_otp_with_custom(
129+
&self,
130+
time: u64,
131+
time_step: u64,
132+
time_start: u64,
133+
digits: u32,
134+
) -> u32 {
130135
let time_count = (time - time_start) / time_step;
131136

132137
let hash = hash_generic(&time_count.to_be_bytes(), &self.secret, &self.mac_digest);
133-
let offset = (hash[hash.len()-1] & 0xf) as usize;
134-
let bytes: [u8; 4] = hash[offset..offset + 4].try_into().expect("Failed byte get");
138+
let offset = (hash[hash.len() - 1] & 0xf) as usize;
139+
let bytes: [u8; 4] = hash[offset..offset + 4]
140+
.try_into()
141+
.expect("Failed byte get");
135142

136143
get_code(bytes, digits)
137144
}
138-
}
145+
}

src/util.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use sha2::{Sha256, Sha512};
1616
pub enum MacDigest {
1717
SHA1,
1818
SHA256,
19-
SHA512
19+
SHA512,
2020
}
2121

2222
/// A generic method to convert the [H/T]OTP byte-array into the
@@ -44,7 +44,7 @@ pub(crate) fn hash_generic(msg: &[u8], secret: &[u8], digest: &MacDigest) -> Vec
4444
match *digest {
4545
MacDigest::SHA1 => hash_internal::<Hmac<Sha1>>(msg, secret),
4646
MacDigest::SHA256 => hash_internal::<Hmac<Sha256>>(msg, secret),
47-
MacDigest::SHA512 => hash_internal::<Hmac<Sha512>>(msg, secret)
47+
MacDigest::SHA512 => hash_internal::<Hmac<Sha512>>(msg, secret),
4848
}
4949
}
5050

@@ -58,9 +58,8 @@ pub(crate) fn hash_generic(msg: &[u8], secret: &[u8], digest: &MacDigest) -> Vec
5858
/// The method will panic if the provided secret is invalid and a hash
5959
/// cannot be generated
6060
61-
fn hash_internal<D: Mac> (msg: &[u8], secret: &[u8]) -> Vec<u8> {
62-
let mut hmac = <D>::new_from_slice(secret)
63-
.expect("Failed to initialize HMAC");
61+
fn hash_internal<D: Mac>(msg: &[u8], secret: &[u8]) -> Vec<u8> {
62+
let mut hmac = <D>::new_from_slice(secret).expect("Failed to initialize HMAC");
6463
hmac.update(msg);
6564
hmac.finalize().into_bytes()[..].into()
66-
}
65+
}

tests/hotp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,4 @@ fn rfc_test_case_9() {
7878
#[test]
7979
fn rfc_test_case_10() {
8080
assert_eq!(run_rfc_test_base32(9), 520489)
81-
}
81+
}

tests/totp.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use xotp::util::MacDigest;
21
use xotp::totp::TOTP;
2+
use xotp::util::MacDigest;
33

44
// RFC6238 SHA1 Secret
55
static SECRET_UTF8_SHA1: &str = "12345678901234567890";
@@ -31,7 +31,11 @@ fn run_rfc_test_bytes(time: u64) -> u32 {
3131
/// Generic test method to get the TOTP code with
3232
/// the given digest's Secret Key as a byte array
3333
fn run_rfc_test_bytes_with_digest(time: u64, digest: MacDigest) -> u32 {
34-
let secret = if let MacDigest::SHA256 = digest { SECRET_BYTES_SHA256 } else { SECRET_BYTES_SHA512 };
34+
let secret = if let MacDigest::SHA256 = digest {
35+
SECRET_BYTES_SHA256
36+
} else {
37+
SECRET_BYTES_SHA512
38+
};
3539
let totp = TOTP::new_with_digest(secret, digest);
3640
totp.get_otp(time, 8)
3741
}
@@ -46,7 +50,11 @@ fn run_rfc_test_utf8(time: u64) -> u32 {
4650
/// Generic test method to get the TOTP code with
4751
/// the given digest's Secret Key as a string literal
4852
fn run_rfc_test_utf8_with_digest(time: u64, digest: MacDigest) -> u32 {
49-
let secret = if let MacDigest::SHA256 = digest { SECRET_UTF8_SHA256 } else { SECRET_UTF8_SHA512 };
53+
let secret = if let MacDigest::SHA256 = digest {
54+
SECRET_UTF8_SHA256
55+
} else {
56+
SECRET_UTF8_SHA512
57+
};
5058
let totp = TOTP::from_utf8_with_digest(secret, digest);
5159
totp.get_otp(time, 8)
5260
}
@@ -61,13 +69,15 @@ fn run_rfc_test_base32(time: u64) -> u32 {
6169
/// Generic test method to get the TOTP code with
6270
/// the given digest's Secret Key as a base32-encoded string
6371
fn run_rfc_test_base32_with_digest(time: u64, digest: MacDigest) -> u32 {
64-
let secret = if let MacDigest::SHA256 = digest { SECRET_BASE32_SHA256 } else { SECRET_BASE32_SHA512 };
72+
let secret = if let MacDigest::SHA256 = digest {
73+
SECRET_BASE32_SHA256
74+
} else {
75+
SECRET_BASE32_SHA512
76+
};
6577
let totp = TOTP::from_base32_with_digest(secret, digest);
6678
totp.get_otp_with_custom(time, 30, 0, 8)
6779
}
6880

69-
70-
7181
// All SHA-1 Tests for TOTP from RTC6238
7282
// Tests 1-2 ran with 'SECRET_BYTES_SHA1'
7383
#[test]
@@ -168,7 +178,8 @@ fn rtc_test_1_sha512() {
168178
fn rtc_test_2_sha512() {
169179
assert_eq!(
170180
run_rfc_test_bytes_with_digest(1111111109, MacDigest::SHA512),
171-
25091201 )
181+
25091201
182+
)
172183
}
173184

174185
// Tests 3-4 ran with 'SECRET_UTF8_SHA512'
@@ -203,4 +214,4 @@ fn rtc_test_6_sha512() {
203214
run_rfc_test_base32_with_digest(20000000000, MacDigest::SHA512),
204215
47863826
205216
)
206-
}
217+
}

0 commit comments

Comments
 (0)