@@ -4,6 +4,7 @@ use crate::util::{base32_decode, get_code, hash_generic, MacDigest};
4
4
///
5
5
/// Follows the specification listed in [RFC6238]. Needs a secret,
6
6
/// a digest algorithm, a number of digits and a period on initialization.
7
+ ///
7
8
/// The TOTP can then be generated using [`TOTP::get_otp`] or
8
9
/// [`TOTP::get_otp_with_custom_time_start`].
9
10
///
@@ -19,13 +20,14 @@ use crate::util::{base32_decode, get_code, hash_generic, MacDigest};
19
20
pub struct TOTP {
20
21
/// The secret key used in the HMAC process.
21
22
///
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 .
24
25
secret : Vec < u8 > ,
25
26
26
27
/// The digest to use in the HMAC process.
27
28
///
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.
29
31
mac_digest : MacDigest ,
30
32
31
33
/// The number of digits of the code generated.
@@ -39,10 +41,11 @@ pub struct TOTP {
39
41
period : u64 ,
40
42
}
41
43
42
- // All initializer implementations for the [`TOTP`] struct
44
+ /// All initializer implementations for the [`TOTP`] struct
43
45
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.
46
49
pub fn new ( secret : & [ u8 ] , mac_digest : MacDigest , digits : u32 , period : u64 ) -> Self {
47
50
TOTP {
48
51
secret : secret. to_vec ( ) ,
@@ -52,14 +55,16 @@ impl TOTP {
52
55
}
53
56
}
54
57
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.
57
61
pub fn new_from_utf8 ( secret : & str , mac_digest : MacDigest , digits : u32 , period : u64 ) -> Self {
58
62
TOTP :: new ( secret. as_bytes ( ) , mac_digest, digits, period)
59
63
}
60
64
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.
63
68
///
64
69
/// # Panics
65
70
/// This method panics if the provided string is not correctly base32 encoded.
@@ -68,56 +73,59 @@ impl TOTP {
68
73
TOTP :: new ( & decoded, mac_digest, digits, period)
69
74
}
70
75
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.
72
78
///
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.
75
81
pub fn default_from_secret ( secret : & [ u8 ] ) -> Self {
76
82
TOTP :: default_from_secret_with_digest ( secret, MacDigest :: SHA1 )
77
83
}
78
84
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.
81
87
///
82
- /// Defaults to using 6 digits and a 30 seconds period.
88
+ /// Defaults to a 6-digit OTP output and a 30-second period.
83
89
pub fn default_from_secret_with_digest ( secret : & [ u8 ] , mac_digest : MacDigest ) -> Self {
84
90
TOTP :: new ( secret, mac_digest, 6 , 30 )
85
91
}
86
92
87
93
/// Creates a new TOTP instance with an utf8 representation of the secret.
88
94
///
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.
91
97
pub fn default_from_utf8 ( secret : & str ) -> Self {
92
98
TOTP :: default_from_utf8_with_digest ( secret, MacDigest :: SHA1 )
93
99
}
94
100
95
101
/// Creates a new TOTP instance with an utf8 representation of the secret and
96
102
/// a digest algorithm.
97
103
///
98
- /// Defaults to using 6 digits and a 30 seconds period.
104
+ /// Defaults to a 6-digit OTP output and a 30-second period.
99
105
pub fn default_from_utf8_with_digest ( secret : & str , mac_digest : MacDigest ) -> Self {
100
106
TOTP :: new_from_utf8 ( secret, mac_digest, 6 , 30 )
101
107
}
102
108
103
109
/// Creates a new TOTP instance with a base32 representation of the secret.
104
110
///
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.
107
113
///
108
114
/// # 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.
110
117
pub fn default_from_base32 ( secret : & str ) -> Self {
111
118
TOTP :: default_from_base32_with_digest ( secret, MacDigest :: SHA1 )
112
119
}
113
120
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.
116
123
///
117
- /// Defaults to using 6 digits and a 30 seconds period.
124
+ /// Defaults to a 6-digit OTP output and a 30-second period.
118
125
///
119
126
/// # 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.
121
129
pub fn default_from_base32_with_digest ( secret : & str , mac_digest : MacDigest ) -> Self {
122
130
TOTP :: new_from_base32 ( secret, mac_digest, 6 , 30 )
123
131
}
@@ -135,7 +143,7 @@ impl TOTP {
135
143
self . digits
136
144
}
137
145
138
- /// Gets the period between different generated code.
146
+ /// Gets the period between code changes .
139
147
pub fn get_period ( & self ) -> u64 {
140
148
self . period
141
149
}
@@ -145,18 +153,20 @@ impl TOTP {
145
153
impl TOTP {
146
154
/// Generates and returns the TOTP value for the specified time.
147
155
///
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.
149
158
///
150
159
/// # 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.
153
162
pub fn get_otp ( & self , time : u64 ) -> u32 {
154
163
self . get_otp_with_custom_time_start ( time, 0 )
155
164
}
156
165
157
166
/// Generates and returns the TOTP value for the specified time.
158
167
///
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.
160
170
///
161
171
/// This method allows a custom start time to be provided.
162
172
///
0 commit comments