The Battle for Wesnoth  1.19.2+dev
hash.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
3  by Thomas Baumhauer <thomas.baumhauer@NOSPAMgmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include <array>
19 #include <cstdint>
20 #include <string>
21 
22 #include "exceptions.hpp"
23 
24 // Not configurable. The size (64) is both hard-, and implicitly-coded in crypt_blowfish
25 static const int BCRYPT_HASHSIZE = 64;
26 
27 namespace utils {
28 
29 struct hash_error : public game::error
30 {
31  hash_error(const std::string& message) : game::error(message) {}
32 };
33 
34 class hash_base
35 {
36 public:
37  virtual std::string base64_digest() const = 0;
38  virtual std::string hex_digest() const = 0;
39  virtual ~hash_base() {}
40 };
41 
42 template<std::size_t sz, typename T = uint8_t>
43 class hash_digest : public hash_base
44 {
45 protected:
46  std::array<T, sz> hash;
47 public:
48  static const unsigned int DIGEST_SIZE = sz;
49  std::array<T, sz> raw_digest() const {return hash;}
50 };
51 
52 class md5 : public hash_digest<16>
53 {
54 public:
55  static int get_iteration_count(const std::string& hash);
56  static std::string get_salt(const std::string& hash);
57  static bool is_valid_prefix(const std::string& hash);
58  static bool is_valid_hash(const std::string& hash);
59  explicit md5(const std::string& input);
60  md5(const std::string& input, const std::string& salt, int iteration_count = 10);
61  virtual std::string base64_digest() const override;
62  virtual std::string hex_digest() const override;
63 };
64 
65 class bcrypt : public hash_digest<BCRYPT_HASHSIZE, char>
66 {
67  bcrypt() {}
68  bcrypt(const std::string& input);
69 
70 public:
71  static bcrypt from_salted_salt(const std::string& input);
72  static bcrypt from_hash_string(const std::string& input);
73  static bcrypt hash_pw(const std::string& password, bcrypt& salt);
74 
76 
77  static bool is_valid_prefix(const std::string& hash);
78  std::string get_salt() const;
79  virtual std::string hex_digest() const override;
80  virtual std::string base64_digest() const override;
81 };
82 
83 } // namespace utils
std::size_t iteration_count_delim_pos
Definition: hash.hpp:75
virtual std::string base64_digest() const override
Definition: hash.cpp:189
static bool is_valid_prefix(const std::string &hash)
Definition: hash.cpp:169
std::string get_salt() const
Definition: hash.cpp:176
static bcrypt from_hash_string(const std::string &input)
Definition: hash.cpp:150
virtual std::string hex_digest() const override
Definition: hash.cpp:184
static bcrypt from_salted_salt(const std::string &input)
Definition: hash.cpp:139
static bcrypt hash_pw(const std::string &password, bcrypt &salt)
Definition: hash.cpp:160
virtual std::string hex_digest() const =0
virtual std::string base64_digest() const =0
virtual ~hash_base()
Definition: hash.hpp:39
static const unsigned int DIGEST_SIZE
Definition: hash.hpp:48
std::array< T, sz > raw_digest() const
Definition: hash.hpp:49
std::array< T, sz > hash
Definition: hash.hpp:46
static bool is_valid_prefix(const std::string &hash)
Definition: hash.cpp:95
virtual std::string base64_digest() const override
Definition: hash.cpp:125
static std::string get_salt(const std::string &hash)
Definition: hash.cpp:91
virtual std::string hex_digest() const override
Definition: hash.cpp:120
static int get_iteration_count(const std::string &hash)
Definition: hash.cpp:87
static bool is_valid_hash(const std::string &hash)
Definition: hash.cpp:100
md5(const std::string &input)
Definition: hash.cpp:65
static const int BCRYPT_HASHSIZE
Definition: hash.hpp:25
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29
std::string message
Definition: exceptions.hpp:30
hash_error(const std::string &message)
Definition: hash.hpp:31