Crate rfc_4226

Source
Expand description

Implementation of IETF RFC 4226, “HOTP: An HMAC-Based One-Time Password Algorithm.”

§Examples

The workhorse hotp function returns a Token of the specified length:

let key = b"ferris23!@#$%^&*()";
let counter = 9001_u64;
let token: Token<6> = hotp(key, counter).unwrap();
assert_eq!(token, Token(852888));

The crate makes extensive use of “const generics” to encode token lengths in all operations, forcing consumers to specify exactly what, for instance, “is the token equal to this number?” means. This explicitness also enables some nice features, such as automatic zero-padding of tokens to the correct length for display to a user:

let key = b"ferris23!@#$%^&*()";
let counter = 292167_u64;
let token: Token<6> = hotp(key, counter).unwrap();
// Equivalent:
let token = hotp::<_, _, 6>(key, counter).unwrap();
assert_eq!(token.to_string(), "000000");

This type-level encoding is also used to ensure that the HOTP spec is followed closely at compile time.

let key = b"ferris23!@#$%^&*()";
let counter = 9001_u64;
// The HOTP spec only allows tokens of length 6–9
let pin: Token<4> = hotp(key, counter).unwrap();

Modules§

  • HMAC digest types and traits.
  • Type-level encoding of HOTP token length restrictions.

Structs§

Enums§

Functions§

  • Main HOTP function.