The Battle for Wesnoth  1.15.12+dev
hash.hpp
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 #pragma once
16 
17 #include <array>
18 #include <cstdint>
19 #include <string>
20 
21 #include "exceptions.hpp"
22 
23 // Not configurable. The size (64) is both hard-, and implicitly-coded in crypt_blowfish
24 static const int BCRYPT_HASHSIZE = 64;
25 
26 namespace utils {
27 
28 struct hash_error : public game::error
29 {
30  hash_error(const std::string& message) : game::error(message) {}
31 };
32 
33 class hash_base
34 {
35 public:
36  virtual std::string base64_digest() const = 0;
37  virtual std::string hex_digest() const = 0;
38  virtual ~hash_base() {}
39 };
40 
41 template<std::size_t sz, typename T = uint8_t>
42 class hash_digest : public hash_base
43 {
44 protected:
45  std::array<T, sz> hash;
46 public:
47  static const int DIGEST_SIZE = sz;
48  std::array<T, sz> raw_digest() const {return hash;}
49 };
50 
51 class md5 : public hash_digest<16>
52 {
53 public:
54  static int get_iteration_count(const std::string& hash);
55  static std::string get_salt(const std::string& hash);
56  static bool is_valid_prefix(const std::string& hash);
57  static bool is_valid_hash(const std::string& hash);
58  explicit md5(const std::string& input);
59  md5(const std::string& input, const std::string& salt, int iteration_count = 10);
60  virtual std::string base64_digest() const override;
61  virtual std::string hex_digest() const override;
62 };
63 
64 class sha1 : public hash_digest<20>
65 {
66 public:
67  explicit sha1(const std::string& input);
68  virtual std::string base64_digest() const override;
69  virtual std::string hex_digest() const override;
70 };
71 
72 class bcrypt : public hash_digest<BCRYPT_HASHSIZE, char>
73 {
74  bcrypt() {}
75  bcrypt(const std::string& input);
76 
77 public:
78  static bcrypt from_salted_salt(const std::string& input);
79  static bcrypt from_hash_string(const std::string& input);
80  static bcrypt hash_pw(const std::string& password, bcrypt& salt);
81 
83 
84  static bool is_valid_prefix(const std::string& hash);
85  std::string get_salt() const;
86  virtual std::string hex_digest() const override;
87  virtual std::string base64_digest() const override;
88 };
89 
90 } // namespace utils
std::array< T, sz > raw_digest() const
Definition: hash.hpp:48
std::array< T, sz > hash
Definition: hash.hpp:45
hash_error(const std::string &message)
Definition: hash.hpp:30
static const int BCRYPT_HASHSIZE
Definition: hash.hpp:24
std::string password(const std::string &server, const std::string &login)
std::size_t iteration_count_delim_pos
Definition: hash.hpp:82
virtual ~hash_base()
Definition: hash.hpp:38
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:27
std::string message
Definition: exceptions.hpp:29