The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
hash.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2008 - 2025
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
25static const int BCRYPT_HASHSIZE = 64;
26
27namespace utils {
28
29struct hash_error : public game::error
30{
31 hash_error(const std::string& message) : game::error(message) {}
32};
33
35{
36public:
37 virtual std::string base64_digest() const = 0;
38 virtual std::string hex_digest() const = 0;
39 virtual ~hash_base() {}
40};
41
42template<std::size_t sz, typename T = uint8_t>
43class hash_digest : public hash_base
44{
45protected:
46 std::array<T, sz> hash;
47public:
48 static const unsigned int DIGEST_SIZE = sz;
49 std::array<T, sz> raw_digest() const {return hash;}
50};
51
52class md5 : public hash_digest<16>
53{
54public:
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
65class bcrypt : public hash_digest<BCRYPT_HASHSIZE, char>
66{
67 bcrypt() {}
68 bcrypt(const std::string& input);
69
70public:
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:188
static bool is_valid_prefix(const std::string &hash)
Definition hash.cpp:168
std::string get_salt() const
Definition hash.cpp:175
static bcrypt from_hash_string(const std::string &input)
Definition hash.cpp:149
virtual std::string hex_digest() const override
Definition hash.cpp:183
static bcrypt from_salted_salt(const std::string &input)
Definition hash.cpp:138
static bcrypt hash_pw(const std::string &password, bcrypt &salt)
Definition hash.cpp:159
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:94
virtual std::string base64_digest() const override
Definition hash.cpp:124
static std::string get_salt(const std::string &hash)
Definition hash.cpp:90
virtual std::string hex_digest() const override
Definition hash.cpp:119
static int get_iteration_count(const std::string &hash)
Definition hash.cpp:86
static bool is_valid_hash(const std::string &hash)
Definition hash.cpp:99
static const int BCRYPT_HASHSIZE
Definition hash.hpp:25
Base class for all the errors encountered by the engine.
std::string message
hash_error(const std::string &message)
Definition hash.hpp:31