The Battle for Wesnoth  1.15.1+dev
user_handler.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Thomas Baumhauer <thomas.baumhauer@NOSPAMgmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "server/user_handler.hpp"
16 #include "config.hpp"
17 #include "random.hpp"
18 #include "serialization/base64.hpp"
19 
20 #ifndef __APPLE__
21 #include <openssl/rand.h>
22 #else
23 #include <cstdlib>
24 #endif
25 
26 #include <array>
27 #include <ctime>
28 #include <sstream>
29 
30 std::string user_handler::create_unsecure_nonce(int length) {
31  srand(static_cast<unsigned>(std::time(nullptr)));
32 
33  std::stringstream ss;
34 
35  for(int i = 0; i < length; i++) {
37  }
38 
39  return ss.str();
40 }
41 
42 #ifndef __APPLE__
43 namespace {
44  class RAND_bytes_exception: public std::exception
45  {
46  };
47 }
48 #endif
49 
51 {
52  // Must be full base64 encodings (3 bytes = 4 chars) else we skew the PRNG results
53  std::array<unsigned char, (3 * 32) / 4> buf;
54 
55 #ifndef __APPLE__
56  if(!RAND_bytes(buf.data(), buf.size())) {
57  throw RAND_bytes_exception();
58  }
59 #else
60  arc4random_buf(buf.data(), buf.size());
61 #endif
62 
63  return base64::encode({buf.data(), buf.size()});
64 }
65 
std::string create_unsecure_nonce(int length=8)
Create a random string of digits for password encryption.
std::string encode(utils::byte_string_view bytes)
Definition: base64.cpp:219
Definitions for the interface to Wesnoth Markup Language (WML).
std::size_t i
Definition: function.cpp:933
int get_random_int(int min, int max)
This helper method provides a random int from the underlying generator, using results of next_random...
Definition: random.hpp:51
static rng & default_instance()
Definition: random.cpp:73
std::string create_secure_nonce()