@@ -40,6 +40,8 @@ pub struct TOTP {
40
40
41
41
// All initializer implementations for the [`TOTP`] struct
42
42
impl TOTP {
43
+ /// Generates a new TOTP instance from a byte array representation of the secret,
44
+ /// a digest algorithm, a number of digits and a period in seconds.
43
45
pub fn new ( secret : & [ u8 ] , mac_digest : MacDigest , digits : u32 , period : u64 ) -> Self {
44
46
TOTP {
45
47
secret : secret. to_vec ( ) ,
@@ -49,64 +51,69 @@ impl TOTP {
49
51
}
50
52
}
51
53
54
+ /// Generates a new TOTP instance from an utf8 representation of the secret,
55
+ /// a digest algorithm, a number of digits and a period in seconds.
52
56
pub fn new_from_utf8 ( secret : & str , mac_digest : MacDigest , digits : u32 , period : u64 ) -> Self {
53
57
TOTP :: new ( secret. as_bytes ( ) , mac_digest, digits, period)
54
58
}
55
59
60
+ /// Generates a new TOTP instance from a base32 representation of the secret,
61
+ /// a digest algorithm, a number of digits and a period in seconds.
62
+ ///
63
+ /// # Panics
64
+ /// This method panics if the provided string is not correctly base32 encoded.
56
65
pub fn new_from_base32 ( secret : & str , mac_digest : MacDigest , digits : u32 , period : u64 ) -> Self {
57
66
let decoded = base32_decode ( secret) . expect ( "Failed to decode base32 string" ) ;
58
67
TOTP :: new ( & decoded, mac_digest, digits, period)
59
68
}
60
69
61
- /// Creates a new TOTP instance with a byte-array representation
62
- /// of the secret
70
+ /// Creates a new TOTP instance with a byte-array representation of the secret.
63
71
///
64
- /// Defaults to using [`MacDigest::SHA1`] as the digest for HMAC
65
- /// operations .
72
+ /// Defaults to using [`MacDigest::SHA1`] as the digest for HMAC operations,
73
+ /// 6 digits and a 30 seconds period .
66
74
pub fn from_secret ( secret : & [ u8 ] ) -> Self {
67
75
TOTP :: from_secret_with_digest ( secret, MacDigest :: SHA1 )
68
76
}
69
77
70
- /// Creates a new TOTP instance with a byte-array representation
71
- /// of the secret and a specific digest for HMAC operations
78
+ /// Creates a new TOTP instance with a byte-array representation of the secret and
79
+ /// a digest algorithm.
72
80
///
73
- /// Allows for non-SHA1 algorithms to be used with TOTP generation
81
+ /// Defaults to using 6 digits and a 30 seconds period.
74
82
pub fn from_secret_with_digest ( secret : & [ u8 ] , mac_digest : MacDigest ) -> Self {
75
83
TOTP :: new ( secret, mac_digest, 6 , 30 )
76
84
}
77
85
78
- /// Creates a new TOTP instance from a utf8-encoded string secret
86
+ /// Creates a new TOTP instance with an utf8 representation of the secret.
79
87
///
80
- /// Like [`TOTP::new`], this method also defaults to using [`MacDigest::SHA1`]
81
- /// for HMAC operations .
88
+ /// Defaults to using [`MacDigest::SHA1`] as the digest for HMAC operations,
89
+ /// 6 digits and a 30 seconds period .
82
90
pub fn from_utf8 ( secret : & str ) -> Self {
83
91
TOTP :: from_utf8_with_digest ( secret, MacDigest :: SHA1 )
84
92
}
85
93
86
- /// Creates a new TOTP instance from a utf8-encoded string secret
94
+ /// Creates a new TOTP instance with an utf8 representation of the secret and
95
+ /// a digest algorithm.
87
96
///
88
- /// Like [`TOTP::new_with_digest`], this method allows a digest to be specified
89
- /// instead of the default SHA1 being used.
97
+ /// Defaults to using 6 digits and a 30 seconds period.
90
98
pub fn from_utf8_with_digest ( secret : & str , mac_digest : MacDigest ) -> Self {
91
99
TOTP :: new_from_utf8 ( secret, mac_digest, 6 , 30 )
92
100
}
93
101
94
- /// Creates a new TOTP instance from a base32-encoded string secret
102
+ /// Creates a new TOTP instance with a base32 representation of the secret.
95
103
///
96
- /// Like [`TOTP::new`] and [`TOTP::from_utf8 `] this method also defaults
97
- /// to using [`MacDigest::SHA1`] for HMAC operations .
104
+ /// Defaults to using [`MacDigest::SHA1 `] as the digest for HMAC operations,
105
+ /// 6 digits and a 30 seconds period .
98
106
///
99
107
/// # Panics
100
- /// This method panics if the [`TOTP::from_base32_with_digest`] does,
101
- /// which happens when the provided string is not correctly base32 encoded.
108
+ /// This method panics if the provided string is not correctly base32 encoded.
102
109
pub fn from_base32 ( secret : & str ) -> Self {
103
110
TOTP :: from_base32_with_digest ( secret, MacDigest :: SHA1 )
104
111
}
105
112
106
- /// Creates a new TOTP instance from a base32-encoded string secret
113
+ /// Creates a new TOTP instance with a base32 representation of the secret and
114
+ /// a digest algorithm.
107
115
///
108
- /// Like [`TOTP::new_with_digest`] and [`TOTP::from_utf8_with_digest`] this
109
- /// method allows a digest to be specified instead of the default SHA1.
116
+ /// Defaults to using 6 digits and a 30 seconds period.
110
117
///
111
118
/// # Panics
112
119
/// This method panics if the provided string is not correctly base32 encoded.
@@ -135,30 +142,22 @@ impl TOTP {
135
142
136
143
// All otp generation methods for the [`TOTP`] struct.
137
144
impl TOTP {
138
- /// Generates and returns the TOTP value for the time with the
139
- /// specified digits.
140
- ///
141
- /// The time must be specified in seconds to calculate the correct
142
- /// one-time password.
145
+ /// Generates and returns the TOTP value for the specified time.
143
146
///
144
- /// As this method doesn't specify time steps or a starting time,
145
- /// the starting time is assumed to be 0 and the time step is set
146
- /// to the default of 30 seconds.
147
+ /// The time must be specified in seconds to calculate the correct one-time password.
147
148
///
148
149
/// # Panics
149
- /// This method panics if the called [`TOTP::get_otp_with_custom `] method
150
+ /// This method panics if the called [`TOTP::get_otp_with_custom_time_start `] method
150
151
/// does, which would happen if the hash's secret is incorrectly given.
151
152
pub fn get_otp ( & self , time : u64 ) -> u32 {
152
153
self . get_otp_with_custom_time_start ( time, 0 )
153
154
}
154
155
155
- /// Generates and returns the TOTP value for the time with a provided step,
156
- /// start time, and digit count
156
+ /// Generates and returns the TOTP value for the specified time.
157
157
///
158
- /// Like with the [`TOTP::get_otp`] method, the time should be provided in
159
- /// seconds for proper calculation
158
+ /// The time must be specified in seconds to calculate the correct one-time password.
160
159
///
161
- /// This method allows custom start times and time steps to be provided.
160
+ /// This method allows a custom start time to be provided.
162
161
///
163
162
/// # Panics
164
163
/// This method panics if the hash's secret is incorrectly given.
0 commit comments