Botan  2.4.0
Crypto and TLS for C++11
totp.h
Go to the documentation of this file.
1 /*
2 * (C) 2017 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6 
7 #ifndef BOTAN_TOTP_H_
8 #define BOTAN_TOTP_H_
9 
10 #include <botan/hotp.h>
11 #include <chrono>
12 
13 namespace Botan {
14 
15 /**
16 * TOTP (time based) one time passwords (RFC 6238)
17 */
18 class BOTAN_PUBLIC_API(2,2) TOTP final
19  {
20  public:
21  /**
22  * @param key the secret key shared between client and server
23  * @param hash_algo the hash algorithm to use, should be SHA-1, SHA-256 or SHA-512
24  * @param digits the number of digits in the OTP (must be 6, 7, or 8)
25  * @param time_step granularity of OTP in seconds
26  */
27  TOTP(const SymmetricKey& key, const std::string& hash_algo = "SHA-1",
28  size_t digits = 6, size_t time_step = 30);
29 
30  /**
31  * Convert the provided time_point to a Unix timestamp and call generate_totp
32  */
33  uint32_t generate_totp(std::chrono::system_clock::time_point time_point);
34 
35  /**
36  * Generate the OTP cooresponding the the provided "Unix timestamp" (ie
37  * number of seconds since midnight Jan 1, 1970)
38  */
39  uint32_t generate_totp(uint64_t unix_time);
40 
41  bool verify_totp(uint32_t otp,
42  std::chrono::system_clock::time_point time,
43  size_t clock_drift_accepted = 0);
44 
45  bool verify_totp(uint32_t otp, uint64_t unix_time,
46  size_t clock_drift_accepted = 0);
47 
48  private:
49  HOTP m_hotp;
50  size_t m_time_step;
51  std::chrono::system_clock::time_point m_unix_epoch;
52  };
53 
54 }
55 
56 #endif
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
Definition: alg_id.cpp:13
AlgorithmIdentifier hash_algo
Definition: x509_obj.cpp:22