]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_bcrypt.cpp
Update copyright headers.
[user/henk/code/inspircd.git] / src / modules / m_bcrypt.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2017-2018, 2020 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2014 Daniel Vassdal <shutter@canternet.org>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /// $CompilerFlags: -Ivendor_directory("bcrypt")
21
22
23 #include "inspircd.h"
24 #include "modules/hash.h"
25
26 #include <crypt_blowfish.c>
27
28 class BCryptProvider : public HashProvider
29 {
30  private:
31         std::string Salt()
32         {
33                 char entropy[16];
34                 for (unsigned int i = 0; i < sizeof(entropy); ++i)
35                         entropy[i] = ServerInstance->GenRandomInt(0xFF);
36
37                 char salt[32];
38                 if (!_crypt_gensalt_blowfish_rn("$2a$", rounds, entropy, sizeof(entropy), salt, sizeof(salt)))
39                         throw ModuleException("Could not generate salt - this should never happen");
40
41                 return salt;
42         }
43
44  public:
45         unsigned int rounds;
46
47         std::string Generate(const std::string& data, const std::string& salt)
48         {
49                 char hash[64];
50                 _crypt_blowfish_rn(data.c_str(), salt.c_str(), hash, sizeof(hash));
51                 return hash;
52         }
53
54         std::string GenerateRaw(const std::string& data) CXX11_OVERRIDE
55         {
56                 return Generate(data, Salt());
57         }
58
59         bool Compare(const std::string& input, const std::string& hash)  CXX11_OVERRIDE
60         {
61                 return InspIRCd::TimingSafeCompare(Generate(input, hash), hash);
62         }
63
64         std::string ToPrintable(const std::string& raw) CXX11_OVERRIDE
65         {
66                 return raw;
67         }
68
69         BCryptProvider(Module* parent)
70                 : HashProvider(parent, "bcrypt", 60)
71                 , rounds(10)
72         {
73         }
74 };
75
76 class ModuleBCrypt : public Module
77 {
78         BCryptProvider bcrypt;
79
80  public:
81         ModuleBCrypt() : bcrypt(this)
82         {
83         }
84
85         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
86         {
87                 ConfigTag* conf = ServerInstance->Config->ConfValue("bcrypt");
88                 bcrypt.rounds = conf->getUInt("rounds", 10, 1);
89         }
90
91         Version GetVersion() CXX11_OVERRIDE
92         {
93                 return Version("Allows other modules to generate bcrypt hashes.", VF_VENDOR);
94         }
95 };
96
97 MODULE_INIT(ModuleBCrypt)