Skip to content

Instantly share code, notes, and snippets.

@vpetrigo
Last active February 1, 2016 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vpetrigo/bc355a5adb2392dbc788 to your computer and use it in GitHub Desktop.
Save vpetrigo/bc355a5adb2392dbc788 to your computer and use it in GitHub Desktop.
Random 32 hex-character string
#include <iostream>
#include <chrono>
#include <fstream>
#include <array>
#include <random>
#include <cstddef>
void key_gen(std::ofstream& fout);
int main() {
std::string key_file_name {"key.txt"};
std::ofstream file{key_file_name};
if (!file) {
std::cerr << "Cannot open file <" << key_file_name << ">" << std::endl;
return 1;
}
key_gen(file);
std::cout << "Key is generated" << std::endl;
return 0;
}
void key_gen(std::ofstream& fout) {
std::random_device rd;
std::mt19937 gen(rd());
auto curr_clock = std::chrono::high_resolution_clock::now();
std::seed_seq sseq {
std::chrono::duration_cast<std::chrono::hours> (curr_clock.time_since_epoch()).count(),
std::chrono::duration_cast<std::chrono::seconds> (curr_clock.time_since_epoch()).count()
};
gen.seed(sseq);
// generator for hex numbers from 0 to F
std::uniform_int_distribution<int> dis(0x0, 0xF);
constexpr int key_size = 32;
fout << std::hex;
for (std::size_t i = 0; i < key_size; ++i) {
fout << dis(gen);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment