@@ -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};
1920pub 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
4345impl 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 {
145153impl 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 ///
0 commit comments