Skip to content

Commit cb5384d

Browse files
committed
Fix TOTP tests
1 parent a2a9683 commit cb5384d

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ hmac = "0.12.0"
1515
sha-1 = "0.10.0"
1616
sha2 = "0.10.1"
1717
base32 = "0.4.0"
18-
url = "2.2.2"
18+
url = "2.2.2"

src/hotp.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,20 @@ impl HOTP {
4141
}
4242
}
4343

44+
pub fn new_from_utf8(secret: &str, digits: u32) -> Self {
45+
HOTP::new(secret.as_bytes(), digits)
46+
}
47+
48+
pub fn new_from_base32(secret: &str, digits: u32) -> Self {
49+
let decoded = base32_decode(secret).expect("Failed to decode base32 string");
50+
HOTP::new(&decoded, digits)
51+
}
52+
4453
/// Creates a new HOTP instance from a utf8-encoded string secret
4554
///
4655
/// Internally calls [`HOTP::new`] with the string's byte representation
4756
pub fn from_utf8(secret: &str) -> Self {
48-
HOTP::new(secret.as_bytes(), 6)
57+
HOTP::new_from_utf8(secret, 6)
4958
}
5059

5160
/// Creates a new HOTP instance from a base32-encoded string secret
@@ -56,8 +65,7 @@ impl HOTP {
5665
/// This method panics if the provided string is not correctly base32
5766
/// encoded.
5867
pub fn from_base32(secret: &str) -> Self {
59-
let decoded = base32_decode(secret).expect("Failed to decode base32 string");
60-
HOTP::new(&decoded, 6)
68+
HOTP::new_from_base32(secret, 6)
6169
}
6270
}
6371

src/totp.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ impl TOTP {
4646
}
4747
}
4848

49+
pub fn new_from_utf8(secret: &str, mac_digest: MacDigest, digits: u32, period: u64) -> Self {
50+
TOTP::new(secret.as_bytes(), mac_digest, digits, period)
51+
}
52+
53+
pub fn new_from_base32(secret: &str, mac_digest: MacDigest, digits: u32, period: u64) -> Self {
54+
let decoded = base32_decode(secret).expect("Failed to decode base32 string");
55+
TOTP::new(&decoded, mac_digest, digits, period)
56+
}
57+
4958
/// Creates a new TOTP instance with a byte-array representation
5059
/// of the secret
5160
///
@@ -76,7 +85,7 @@ impl TOTP {
7685
/// Like [`TOTP::new_with_digest`], this method allows a digest to be specified
7786
/// instead of the default SHA1 being used.
7887
pub fn from_utf8_with_digest(secret: &str, mac_digest: MacDigest) -> Self {
79-
TOTP::from_secret_with_digest(secret.as_bytes(), mac_digest)
88+
TOTP::new_from_utf8(secret, mac_digest, 6, 30)
8089
}
8190

8291
/// Creates a new TOTP instance from a base32-encoded string secret
@@ -99,8 +108,7 @@ impl TOTP {
99108
/// # Panics
100109
/// This method panics if the provided string is not correctly base32 encoded.
101110
pub fn from_base32_with_digest(secret: &str, mac_digest: MacDigest) -> Self {
102-
let decoded = base32_decode(secret).expect("Failed to decode base32 string");
103-
TOTP::from_secret_with_digest(&decoded, mac_digest)
111+
TOTP::new_from_base32(secret, mac_digest, 6, 30)
104112
}
105113
}
106114

tests/totp.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static SECRET_BASE32_SHA512: &str = "GEZDGNBVGY3TQOJQGEZ\
2424
/// Generic test method to get the TOTP code with
2525
/// the SHA1 Secret Key as a byte array
2626
fn run_rfc_test_bytes(time: u64) -> u32 {
27-
let totp = TOTP::from_secret(SECRET_BYTES_SHA1);
27+
let totp = TOTP::new(SECRET_BYTES_SHA1, MacDigest::SHA1, 8, 30);
2828
totp.get_otp(time)
2929
}
3030

@@ -36,14 +36,14 @@ fn run_rfc_test_bytes_with_digest(time: u64, digest: MacDigest) -> u32 {
3636
} else {
3737
SECRET_BYTES_SHA512
3838
};
39-
let totp = TOTP::from_secret_with_digest(secret, digest);
39+
let totp = TOTP::new(secret, digest, 8, 30);
4040
totp.get_otp(time)
4141
}
4242

4343
/// Generic test method to get the TOTP code with
4444
/// the SHA1 Secret Key as a string literal
4545
fn run_rfc_test_utf8(time: u64) -> u32 {
46-
let totp = TOTP::from_utf8(SECRET_UTF8_SHA1);
46+
let totp = TOTP::new_from_utf8(SECRET_UTF8_SHA1, MacDigest::SHA1, 8, 30);
4747
totp.get_otp(time)
4848
}
4949

@@ -55,14 +55,14 @@ fn run_rfc_test_utf8_with_digest(time: u64, digest: MacDigest) -> u32 {
5555
} else {
5656
SECRET_UTF8_SHA512
5757
};
58-
let totp = TOTP::from_utf8_with_digest(secret, digest);
58+
let totp = TOTP::new_from_utf8(secret, digest, 8, 30);
5959
totp.get_otp(time)
6060
}
6161

6262
/// Generic test method to get the TOTP code with
6363
/// the SHA1 Secret Key as a base32-encoded string
6464
fn run_rfc_test_base32(time: u64) -> u32 {
65-
let totp = TOTP::from_base32(SECRET_BASE32_SHA1);
65+
let totp = TOTP::new_from_base32(SECRET_BASE32_SHA1, MacDigest::SHA1, 8, 30);
6666
totp.get_otp(time)
6767
}
6868

@@ -74,7 +74,7 @@ fn run_rfc_test_base32_with_digest(time: u64, digest: MacDigest) -> u32 {
7474
} else {
7575
SECRET_BASE32_SHA512
7676
};
77-
let totp = TOTP::from_base32_with_digest(secret, digest);
77+
let totp = TOTP::new_from_base32(secret, digest, 8, 30);
7878
totp.get_otp(time)
7979
}
8080

0 commit comments

Comments
 (0)