Function rfc_4226::hotp[][src]

pub fn hotp<K, C, const DIGITS: u8>(
    key: K,
    counter: C
) -> Result<Token<DIGITS>, HotpError> where
    C: Into<u64>,
    K: AsRef<[u8]>,
    Length<DIGITS>: TokenLength
Expand description

Main HOTP function.

This function takes an 8-byte counter element and a key (specified as a sequence of bytes) and uses the HMAC-SHA1 digest method followed by truncation to produce an HOTP token of the desired number of DIGITS.

Errors

Currently (and subject to change), the only possible error from this method results from providing an invalid shared secret (key). According to RFC 4226, algorithm requirement 6:

The length of the shared secret MUST be at least 128 bits. This document RECOMMENDS a shared secret length of 160 bits.

As such, this method will return an error if the key is shorter than 16 bytes (128 bits).

Extension to other digest protocols

While RFC 4226 technically only allows for the HMAC-SHA1 protocol, extensions such as RFC 6238 (which describes TOTP) allow the use of other protocols. As such, this crate pragmatically exposes a method for using other digest protocols in the Digest trait. To use other digest functions:

  1. implement Digest for your type, and
  2. invoke Digest::truncate on an instance of the type to generate a Token.

So long as the digest is at least 16 bytes long, this should work without issue.

For those specifically interested in TOTP, see also the companion rfc-6238 crate.