Skip to content

Commit cc65fda

Browse files
committed
update the documentation slightly
1 parent 41d6fd9 commit cc65fda

File tree

3 files changed

+78
-47
lines changed

3 files changed

+78
-47
lines changed

src/hotp.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use crate::util::{base32_decode, get_code, hash_generic, MacDigest};
44

55
/// A HOTP Generator
66
///
7-
/// Follows the specification listed in [RFC4226]. Needs a secret and a number of digits on initialization.
7+
/// Follows the specification listed in [RFC4226]. Needs a secret and
8+
/// digit count on initialization.
9+
///
810
/// The HOTP can then be generated using [`HOTP::get_otp`].
911
///
1012
/// # Example
@@ -20,8 +22,8 @@ use crate::util::{base32_decode, get_code, hash_generic, MacDigest};
2022
pub struct HOTP {
2123
/// The secret key used in the HMAC process.
2224
///
23-
/// Often given as a Base32 key, which can be conveniently initialize using
24-
/// the [`HOTP::from_base32`] constructors.
25+
/// Often given as a Base32 key, which can be conveniently initialized
26+
/// using the [`HOTP::default_from_base32`] constructor.
2527
secret: Vec<u8>,
2628

2729
/// The number of digits of the code generated.
@@ -33,7 +35,7 @@ pub struct HOTP {
3335
/// All initializer implementations for the [`HOTP`] struct.
3436
impl HOTP {
3537
/// Creates a new HOTP instance with a byte-array representation
36-
/// of the secret and the number of digits.
38+
/// of the secret and specified digit count.
3739
///
3840
/// Since only SHA1 was specified in the reference implementation and
3941
/// RFC specification, there's no need to initialize with a digest object.
@@ -44,40 +46,47 @@ impl HOTP {
4446
}
4547
}
4648

47-
/// Creates a new HOTP instance from an utf8-encoded string secret and the number of digits.
49+
/// Creates a new HOTP instance from a utf8-encoded string secret
50+
/// and specified digit count.
4851
pub fn new_from_utf8(secret: &str, digits: u32) -> Self {
4952
HOTP::new(secret.as_bytes(), digits)
5053
}
5154

52-
/// Creates a new HOTP instance from a base32-encoded string secret and the number of digits.
55+
/// Creates a new HOTP instance from a base32-encoded string secret
56+
/// and specified digit count.
5357
///
5458
/// # Panics
55-
/// This method panics if the provided string is not correctly base32 encoded.
59+
/// This method panics if the provided string is not correctly
60+
/// base32-encoded.
5661
pub fn new_from_base32(secret: &str, digits: u32) -> Self {
5762
let decoded = base32_decode(secret).expect("Failed to decode base32 string");
5863
HOTP::new(&decoded, digits)
5964
}
6065

61-
/// Creates a new HOTP instance from a byte-array representation of the secret and
62-
/// a default number of 6 digits.
66+
/// Creates a new HOTP instance from a byte-array representation of
67+
/// the secret and a default digit count of 6.
6368
pub fn default_from_secret(secret: &[u8]) -> Self {
6469
HOTP::new(secret, 6)
6570
}
6671

67-
/// Creates a new HOTP instance from an utf8-encoded string secret and a default number of 6 digits.
72+
/// Creates a new HOTP instance from an utf8-encoded string secret
73+
/// and a default digit count of 6..
6874
pub fn default_from_utf8(secret: &str) -> Self {
6975
HOTP::new_from_utf8(secret, 6)
7076
}
7177

72-
/// Creates a new HOTP instance from a base32-encoded string secret and a default number of 6 digits.
78+
/// Creates a new HOTP instance from a base32-encoded string secret
79+
/// and a default digit count of 6..
7380
///
7481
/// # Panics
75-
/// This method panics if the provided string is not correctly base32 encoded.
82+
/// This method panics if the provided string is not correctly
83+
/// base32-encoded.
7684
pub fn default_from_base32(secret: &str) -> Self {
7785
HOTP::new_from_base32(secret, 6)
7886
}
7987
}
8088

89+
/// All getters for the ['HOTP'] struct
8190
impl HOTP {
8291
/// Gets the number of digits of the code.
8392
pub fn get_digits(&self) -> u32 {

src/totp.rs

+41-31
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::util::{base32_decode, get_code, hash_generic, MacDigest};
44
///
55
/// Follows the specification listed in [RFC6238]. Needs a secret,
66
/// a digest algorithm, a number of digits and a period on initialization.
7+
///
78
/// The TOTP can then be generated using [`TOTP::get_otp`] or
89
/// [`TOTP::get_otp_with_custom_time_start`].
910
///
@@ -19,13 +20,14 @@ use crate::util::{base32_decode, get_code, hash_generic, MacDigest};
1920
pub struct TOTP {
2021
/// The secret key used in the HMAC process.
2122
///
22-
/// Often given as a Base32 key, which can be conveniently initialized using
23-
/// [`TOTP::default_from_base32`] constructors.
23+
/// Often given as a Base32 key, which can be conveniently initialized
24+
/// using the [`TOTP::default_from_base32`] constructor.
2425
secret: Vec<u8>,
2526

2627
/// The digest to use in the HMAC process.
2728
///
28-
/// This value defaults to [`MacDigest::SHA1`] if not specified in a constructor.
29+
/// This value defaults to [`MacDigest::SHA1`] if not specified in a
30+
/// constructor.
2931
mac_digest: MacDigest,
3032

3133
/// The number of digits of the code generated.
@@ -39,10 +41,11 @@ pub struct TOTP {
3941
period: u64,
4042
}
4143

42-
// All initializer implementations for the [`TOTP`] struct
44+
/// All initializer implementations for the [`TOTP`] struct
4345
impl TOTP {
44-
/// Generates a new TOTP instance from a byte array representation of the secret,
45-
/// a digest algorithm, a number of digits and a period in seconds.
46+
/// Generates a new TOTP instance from a byte array representation of the
47+
/// secret, a digest algorithm, a number of digits,
48+
/// and a period in seconds.
4649
pub fn new(secret: &[u8], mac_digest: MacDigest, digits: u32, period: u64) -> Self {
4750
TOTP {
4851
secret: secret.to_vec(),
@@ -52,14 +55,16 @@ impl TOTP {
5255
}
5356
}
5457

55-
/// Generates a new TOTP instance from an utf8 representation of the secret,
56-
/// a digest algorithm, a number of digits and a period in seconds.
58+
/// Generates a new TOTP instance from an utf8 representation of the
59+
/// secret, a digest algorithm, a number of digits,
60+
/// and a period in seconds.
5761
pub fn new_from_utf8(secret: &str, mac_digest: MacDigest, digits: u32, period: u64) -> Self {
5862
TOTP::new(secret.as_bytes(), mac_digest, digits, period)
5963
}
6064

61-
/// Generates a new TOTP instance from a base32 representation of the secret,
62-
/// a digest algorithm, a number of digits and a period in seconds.
65+
/// Generates a new TOTP instance from a base32-encoded representation of
66+
/// the secret, a digest algorithm, a number of digits,
67+
/// and a period in seconds.
6368
///
6469
/// # Panics
6570
/// This method panics if the provided string is not correctly base32 encoded.
@@ -68,56 +73,59 @@ impl TOTP {
6873
TOTP::new(&decoded, mac_digest, digits, period)
6974
}
7075

71-
/// Creates a new TOTP instance with a byte-array representation of the secret.
76+
/// Creates a new TOTP instance with a byte-array representation of the
77+
/// secret.
7278
///
73-
/// Defaults to using [`MacDigest::SHA1`] as the digest for HMAC operations,
74-
/// 6 digits and a 30 seconds period.
79+
/// Defaults to using [`MacDigest::SHA1`] as the digest for HMAC
80+
/// operations, with a 6-digit OTP output and a 30-second period.
7581
pub fn default_from_secret(secret: &[u8]) -> Self {
7682
TOTP::default_from_secret_with_digest(secret, MacDigest::SHA1)
7783
}
7884

79-
/// Creates a new TOTP instance with a byte-array representation of the secret and
80-
/// a digest algorithm.
85+
/// Creates a new TOTP instance with a byte-array representation of the
86+
/// secret and a digest algorithm.
8187
///
82-
/// Defaults to using 6 digits and a 30 seconds period.
88+
/// Defaults to a 6-digit OTP output and a 30-second period.
8389
pub fn default_from_secret_with_digest(secret: &[u8], mac_digest: MacDigest) -> Self {
8490
TOTP::new(secret, mac_digest, 6, 30)
8591
}
8692

8793
/// Creates a new TOTP instance with an utf8 representation of the secret.
8894
///
89-
/// Defaults to using [`MacDigest::SHA1`] as the digest for HMAC operations,
90-
/// 6 digits and a 30 seconds period.
95+
/// Defaults to using [`MacDigest::SHA1`] as the digest for HMAC
96+
/// operations, with a 6-digit OTP output and a 30-second period.
9197
pub fn default_from_utf8(secret: &str) -> Self {
9298
TOTP::default_from_utf8_with_digest(secret, MacDigest::SHA1)
9399
}
94100

95101
/// Creates a new TOTP instance with an utf8 representation of the secret and
96102
/// a digest algorithm.
97103
///
98-
/// Defaults to using 6 digits and a 30 seconds period.
104+
/// Defaults to a 6-digit OTP output and a 30-second period.
99105
pub fn default_from_utf8_with_digest(secret: &str, mac_digest: MacDigest) -> Self {
100106
TOTP::new_from_utf8(secret, mac_digest, 6, 30)
101107
}
102108

103109
/// Creates a new TOTP instance with a base32 representation of the secret.
104110
///
105-
/// Defaults to using [`MacDigest::SHA1`] as the digest for HMAC operations,
106-
/// 6 digits and a 30 seconds period.
111+
/// Defaults to using [`MacDigest::SHA1`] as the digest for HMAC
112+
/// operations, with a 6-digit OTP output and a 30-second period.
107113
///
108114
/// # Panics
109-
/// This method panics if the provided string is not correctly base32 encoded.
115+
/// This method panics if the provided string is not correctly
116+
/// base32-encoded.
110117
pub fn default_from_base32(secret: &str) -> Self {
111118
TOTP::default_from_base32_with_digest(secret, MacDigest::SHA1)
112119
}
113120

114-
/// Creates a new TOTP instance with a base32 representation of the secret and
115-
/// a digest algorithm.
121+
/// Creates a new TOTP instance with a base32 representation of the secret
122+
/// and a digest algorithm.
116123
///
117-
/// Defaults to using 6 digits and a 30 seconds period.
124+
/// Defaults to a 6-digit OTP output and a 30-second period.
118125
///
119126
/// # Panics
120-
/// This method panics if the provided string is not correctly base32 encoded.
127+
/// This method panics if the provided string is not correctly
128+
/// base32-encoded.
121129
pub fn default_from_base32_with_digest(secret: &str, mac_digest: MacDigest) -> Self {
122130
TOTP::new_from_base32(secret, mac_digest, 6, 30)
123131
}
@@ -135,7 +143,7 @@ impl TOTP {
135143
self.digits
136144
}
137145

138-
/// Gets the period between different generated code.
146+
/// Gets the period between code changes.
139147
pub fn get_period(&self) -> u64 {
140148
self.period
141149
}
@@ -145,18 +153,20 @@ impl TOTP {
145153
impl TOTP {
146154
/// Generates and returns the TOTP value for the specified time.
147155
///
148-
/// The time must be specified in seconds to calculate the correct one-time password.
156+
/// The time must be specified in seconds to calculate the correct
157+
/// one-time password.
149158
///
150159
/// # Panics
151-
/// This method panics if the called [`TOTP::get_otp_with_custom_time_start`] method
152-
/// does, which would happen if the hash's secret is incorrectly given.
160+
/// This method panics if the [`TOTP::get_otp_with_custom_time_start`]
161+
/// method does, which happens if the hash's secret is incorrectly given.
153162
pub fn get_otp(&self, time: u64) -> u32 {
154163
self.get_otp_with_custom_time_start(time, 0)
155164
}
156165

157166
/// Generates and returns the TOTP value for the specified time.
158167
///
159-
/// The time must be specified in seconds to calculate the correct one-time password.
168+
/// The time must be specified in seconds to calculate the correct
169+
/// one-time password.
160170
///
161171
/// This method allows a custom start time to be provided.
162172
///

src/util.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use crate::totp::TOTP;
1919
/// may not support other digest algorithms.
2020
///
2121
/// [RFC6238]: https://datatracker.ietf.org/doc/html/rfc6238
22-
2322
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
2423
#[cfg_attr(feature = "ffi", repr(C))]
2524
pub enum MacDigest {
@@ -77,7 +76,11 @@ pub(crate) fn base32_decode(data: &str) -> Option<Vec<u8>> {
7776

7877
/// Result of an otpauth URI parsing.
7978
///
80-
/// It's either a TOTP or a HOTP with its current counter.
79+
/// As the URI can return either an [HOTP] or [TOTP] instance,
80+
/// this enum is returned as a wrapper around both types.
81+
///
82+
/// If an [HOTP] instance is returned, a second value is returned
83+
/// signifying the counter's value.
8184
#[derive(Debug)]
8285
#[cfg_attr(feature = "ffi", repr(C))]
8386
pub enum ParseResult {
@@ -86,6 +89,10 @@ pub enum ParseResult {
8689
}
8790

8891
/// Different error types of the optauth URI parsing.
92+
///
93+
/// Represents each error that could occur while parsing the otpauth URI
94+
/// in an enum. The returned error may have an associated message or
95+
/// [url::ParseError] with more information
8996
#[derive(Debug)]
9097
#[cfg_attr(feature = "ffi", repr(C))]
9198
pub enum ParseError {
@@ -102,8 +109,13 @@ pub enum ParseError {
102109
InvalidPeriod(String),
103110
}
104111

105-
/// Parses an otpauth URI, which is the string format of the QR codes usually given by platforms for TOTP.
106-
/// This method is safe and shouldn't panic.
112+
/// Parses an otpauth URI.
113+
///
114+
/// This is generally the string format of QR codes provided by
115+
/// authentication services
116+
///
117+
/// This method is safe and shouldn't panic. It will return an error if the
118+
/// provided uri is invalid.
107119
pub fn parse_otpauth_uri(uri: &str) -> Result<ParseResult, ParseError> {
108120
use ParseError::*;
109121

0 commit comments

Comments
 (0)