@@ -4,9 +4,8 @@ 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 on
8- /// initialization, with other single generation-specific items being
9- /// provided when [`HOTP::get_otp`] is called.
7+ /// Follows the specification listed in [RFC4226]. Needs a secret and a number of digits on initialization.
8+ /// The HOTP can then be generated using [`HOTP::get_otp`].
109///
1110/// # Example
1211/// See the top-level README for an example of HOTP usage
@@ -21,65 +20,69 @@ pub struct HOTP {
2120 /// The secret key used in the HMAC process.
2221 ///
2322 /// Often given as a Base32 key, which can be conveniently initialize using
24- /// the [`HOTP::from_base32`] initializers
23+ /// the [`HOTP::from_base32`] constructors.
2524 secret : Vec < u8 > ,
2625
26+ /// The number of digits of the code generated.
27+ ///
28+ /// This value defaults to 6 if not specified in a constructor.
2729 digits : u32 ,
2830}
2931
3032/// All initializer implementations for the [`HOTP`] struct.
3133impl HOTP {
3234 /// Creates a new HOTP instance with a byte-array representation
33- /// of the secret
35+ /// of the secret and the number of digits.
3436 ///
3537 /// Since only SHA1 was specified in the reference implementation and
36- /// RFC specification, there's no need to initialize with a digest object
38+ /// RFC specification, there's no need to initialize with a digest object.
3739 pub fn new ( secret : & [ u8 ] , digits : u32 ) -> Self {
3840 HOTP {
3941 secret : secret. to_vec ( ) ,
4042 digits,
4143 }
4244 }
4345
46+ /// Creates a new HOTP instance from a utf8-encoded string secret and the number of digits.
4447 pub fn new_from_utf8 ( secret : & str , digits : u32 ) -> Self {
4548 HOTP :: new ( secret. as_bytes ( ) , digits)
4649 }
4750
51+ /// Creates a new HOTP instance from a base32-encoded string secret and the number of digits.
52+ ///
53+ /// # Panics
54+ /// This method panics if the provided string is not correctly base32 encoded.
4855 pub fn new_from_base32 ( secret : & str , digits : u32 ) -> Self {
4956 let decoded = base32_decode ( secret) . expect ( "Failed to decode base32 string" ) ;
5057 HOTP :: new ( & decoded, digits)
5158 }
5259
53- /// Creates a new HOTP instance from a utf8-encoded string secret
54- ///
55- /// Internally calls [`HOTP::new`] with the string's byte representation
60+ /// Creates a new HOTP instance from a utf8-encoded string secret and a default number of 6 digits.
5661 pub fn from_utf8 ( secret : & str ) -> Self {
5762 HOTP :: new_from_utf8 ( secret, 6 )
5863 }
5964
60- /// Creates a new HOTP instance from a base32-encoded string secret
61- ///
62- /// Internally calls [`HOTP::new`] after decoding the string
65+ /// Creates a new HOTP instance from a base32-encoded string secret and a default number of 6 digits.
6366 ///
6467 /// # Panics
65- /// This method panics if the provided string is not correctly base32
66- /// encoded.
68+ /// This method panics if the provided string is not correctly base32 encoded.
6769 pub fn from_base32 ( secret : & str ) -> Self {
6870 HOTP :: new_from_base32 ( secret, 6 )
6971 }
7072}
7173
7274impl HOTP {
75+ /// Gets the number of digits of the code.
7376 pub fn get_digits ( & self ) -> u32 {
7477 self . digits
7578 }
7679}
7780
7881/// All otp generation methods for the [`HOTP`] struct.
7982impl HOTP {
80- /// Generates and returns the HOTP value
83+ /// Generates and returns the HOTP value.
8184 ///
82- /// Uses the given counter value with the specified digit count
85+ /// Uses the given counter value.
8386 ///
8487 /// # Panics
8588 /// This method panics if the hash's secret is incorrectly given.
0 commit comments