Botan  2.9.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 */
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,
28  const std::string& hash_algo = "SHA-1",
29  size_t digits = 6, size_t time_step = 30) :
30  TOTP(key.begin(), key.size(), hash_algo, digits, time_step) {}
31 
32  /**
33  * @param key the secret key shared between client and server
34  * @param key_len length of key
35  * @param hash_algo the hash algorithm to use, should be SHA-1, SHA-256 or SHA-512
36  * @param digits the number of digits in the OTP (must be 6, 7, or 8)
37  * @param time_step granularity of OTP in seconds
38  */
39  TOTP(const uint8_t key[], size_t key_len,
40  const std::string& hash_algo = "SHA-1",
41  size_t digits = 6,
42  size_t time_step = 30);
43 
44  /**
45  * Convert the provided time_point to a Unix timestamp and call generate_totp
46  */
47  uint32_t generate_totp(std::chrono::system_clock::time_point time_point);
48 
49  /**
50  * Generate the OTP corresponding the the provided "Unix timestamp" (ie
51  * number of seconds since midnight Jan 1, 1970)
52  */
53  uint32_t generate_totp(uint64_t unix_time);
54 
55  bool verify_totp(uint32_t otp,
56  std::chrono::system_clock::time_point time,
57  size_t clock_drift_accepted = 0);
58 
59  bool verify_totp(uint32_t otp, uint64_t unix_time,
60  size_t clock_drift_accepted = 0);
61 
62  private:
63  HOTP m_hotp;
64  size_t m_time_step;
65  std::chrono::system_clock::time_point m_unix_epoch;
66  };
67 
68 }
69 
70 #endif
TOTP(const SymmetricKey &key, const std::string &hash_algo="SHA-1", size_t digits=6, size_t time_step=30)
Definition: totp.h:27
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Definition: alg_id.cpp:13
AlgorithmIdentifier hash_algo
Definition: x509_obj.cpp:23