Browse code
Prototype with key generation
Ed Langley authored on 13/06/2017 23:07:28
Showing 4 changed files
Showing 4 changed files
... | ... |
@@ -37,17 +37,6 @@ class system init_system() |
37 | 37 |
installer init_installer() |
38 | 38 |
{ |
39 | 39 |
fstreams fstreams (fstreams::create()); |
40 |
- |
|
41 |
- random_source foo (random_source::create (fstreams)); |
|
42 |
- auto bytes = foo.get_random_bytes (16); |
|
43 |
- std::cout << "I'm random: "; |
|
44 |
- |
|
45 |
- for (auto byte: bytes) { |
|
46 |
- std::cout << static_cast<unsigned int> (byte) << ", "; |
|
47 |
- } |
|
48 |
- |
|
49 |
- std::cout << std::endl; |
|
50 |
- |
|
51 | 40 |
pwd pwd (pwd::create()); |
52 | 41 |
unistd unistd (unistd::create()); |
53 | 42 |
directory directory (directory::create (unistd, pwd)); |
... | ... |
@@ -68,7 +57,10 @@ int main (int argc, char *argv[]) |
68 | 57 |
class system system (init_system()); |
69 | 58 |
installer tool (init_installer()); |
70 | 59 |
auto generated_key_and_sample_token = tool.install_key(); |
71 |
- std::cout << generated_key_and_sample_token.first << " " << |
|
72 |
- generated_key_and_sample_token.second << std::endl; |
|
60 |
+ std::cout << " Key: " << generated_key_and_sample_token.first |
|
61 |
+ << std::endl |
|
62 |
+ << "Sample Token: " << generated_key_and_sample_token.second |
|
63 |
+ << std::endl |
|
64 |
+ << "Run again to get another token from the same key." |
|
65 |
+ << std::endl; |
|
73 | 66 |
} |
74 |
- |
... | ... |
@@ -22,7 +22,7 @@ class impl : public random_source_ifc |
22 | 22 |
private: |
23 | 23 |
fstreams fstreams_; |
24 | 24 |
public: |
25 |
- impl (fstreams fstreams) |
|
25 |
+ impl (const fstreams fstreams) |
|
26 | 26 |
: fstreams_ (fstreams) |
27 | 27 |
{} |
28 | 28 |
std::vector<uint8_t> get_random_bytes (int length) const override |
... | ... |
@@ -43,8 +43,7 @@ public: |
43 | 43 |
|
44 | 44 |
const std::string random_source_ifc::file_path = "/dev/urandom"; |
45 | 45 |
|
46 |
-random_source random_source::create (fstreams &fstreams) |
|
46 |
+random_source random_source::create (const fstreams &fstreams) |
|
47 | 47 |
{ |
48 | 48 |
return random_source (random_source::delegate (new impl (fstreams))); |
49 | 49 |
}; |
50 |
- |
... | ... |
@@ -20,6 +20,7 @@ |
20 | 20 |
#include "user.h" |
21 | 21 |
#include "base32.h" |
22 | 22 |
#include "sys_fstream.h" |
23 |
+#include "random_source.h" |
|
23 | 24 |
|
24 | 25 |
namespace |
25 | 26 |
{ |
... | ... |
@@ -64,11 +65,13 @@ private: |
64 | 65 |
std::string generate_key() const |
65 | 66 |
{ |
66 | 67 |
base32 codec; |
68 |
+ random_source rand = random_source::create(fstreams_); |
|
67 | 69 |
// get randomness |
68 |
- std::vector<unsigned char> random_bytes (16); |
|
70 |
+ int length = 10; |
|
71 |
+ std::vector<uint8_t> random_bytes (rand.get_random_bytes(length)); |
|
69 | 72 |
// base32encode it |
70 | 73 |
std::string key = codec.encode (random_bytes); |
71 |
- return "QWERQWERQWERQWER"; |
|
74 |
+ return key; |
|
72 | 75 |
} |
73 | 76 |
|
74 | 77 |
std::string read_key (const user &user) const |
... | ... |
@@ -115,4 +118,3 @@ tokens tokens::create (const fstreams &fstreams, |
115 | 118 |
return tokens (tokens::delegate |
116 | 119 |
(new tokens_impl (fstreams, generator))); |
117 | 120 |
} |
118 |
- |