git.fiddlerwoaroof.com
Browse code

Cleanup totp generator

Ed Langley authored on 02/06/2017 18:00:00
Showing 3 changed files
... ...
@@ -66,14 +66,6 @@ private:
66 66
     const std::string key;
67 67
 
68 68
 private:
69
-    unsigned long truncate (const std::string &mac) const
70
-    {
71
-        uint8_t offset = static_cast<uint8_t > (mac[19]) & static_cast<uint8_t>
72
-                         (0x0f);
73
-        std::string  offsetBytes = mac.substr (offset, 4);
74
-        return bytesToInt (offsetBytes) & 0x7fffffff;
75
-    }
76
-
77 69
     std::string zero_fill (unsigned long result, int digits) const
78 70
     {
79 71
         std::ostringstream result_stream;
... ...
@@ -81,13 +73,23 @@ private:
81 73
         return result_stream.str();
82 74
     }
83 75
 
76
+    unsigned long truncate (const std::string &mac) const
77
+    {
78
+        uint8_t offset = static_cast<uint8_t > (mac[19]) & static_cast<uint8_t> (0x0f);
79
+
80
+        std::string  offsetBytes = mac.substr (offset, 4);
81
+
82
+        return bytesToInt (offsetBytes) & 0x7fffffff;
83
+    }
84
+
84 85
     std::string hotp (const std::string &key, const unsigned char *data,
85 86
                       size_t data_size, const int digits=6) const
86 87
     {
87
-        unsigned char *digest = HMAC (EVP_sha1(), key.c_str(), key.size(), data,
88
-                                      data_size, NULL, NULL);
89
-        std::string digest_s = std::string (reinterpret_cast<const char *> (digest),
90
-                                            20);
88
+        // TODO: see if I can use sha256/etc. with google auth...
89
+        unsigned char *digest = HMAC (EVP_sha1(), key.c_str(), key.size(), data, data_size, NULL, NULL);
90
+
91
+        std::string digest_s = std::string (reinterpret_cast<const char *> (digest), 20); //TODO: use vectors
92
+
91 93
         unsigned long result = truncate (digest_s) % ipow (10,digits);
92 94
 
93 95
         return zero_fill (result, digits);
... ...
@@ -29,7 +29,6 @@ class token_generator_ifc
29 29
 {
30 30
 public:
31 31
     virtual std::string generate_token () const = 0;
32
-
33 32
 };
34 33
 
35 34
 class totp_generator
... ...
@@ -49,6 +48,7 @@ public:
49 48
     totp_generator (delegate delegate_) :
50 49
         delegate_ (delegate_)
51 50
     {}
51
+
52 52
     totp_generator (const sys_time &clock,
53 53
                     const std::string &key_c,
54 54
                     const int code_digits);
... ...
@@ -70,7 +70,7 @@ public:
70 70
     }
71 71
 };
72 72
 
73
-int six_digits()
73
+int given_digits()
74 74
 {
75 75
     // given
76 76
     std::initializer_list<time_t> samples { 1 };
... ...
@@ -162,7 +162,6 @@ int int_precomputed()
162 162
     // given
163 163
     // The token for key 76I6WTYEUTNCJUREMGKVM45PMA and time '2017/01/01 00:00:00' is 258675
164 164
     time_t theTime = 1483257600;
165
-    /// TODO: int -> time_t
166 165
     std::initializer_list<time_t> samples { theTime }; //
167 166
     auto test_stdtime = std::make_shared<fake_time> (samples);
168 167
 
... ...
@@ -184,7 +183,7 @@ int int_precomputed()
184 183
 
185 184
 int run_tests()
186 185
 {
187
-    test (six_digits);
186
+    test (given_digits);
188 187
     test (modulated_source_modulates_tokens);
189 188
     test (int_max);
190 189
     test (int_min);
... ...
@@ -196,4 +195,3 @@ int main (int argc, char *argv[])
196 195
 {
197 196
     return !run_tests();
198 197
 }
199
-