]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_bcrypt.cpp
Fix being able to see the modes of private/secret channels.
[user/henk/code/inspircd.git] / src / modules / m_bcrypt.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2017-2018 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                 std::string ret = Generate(input, hash);
62                 if (ret.empty())
63                         return false;
64
65                 if (ret == hash)
66                         return true;
67                 return false;
68         }
69
70         std::string ToPrintable(const std::string& raw) CXX11_OVERRIDE
71         {
72                 return raw;
73         }
74
75         BCryptProvider(Module* parent)
76                 : HashProvider(parent, "bcrypt", 60)
77                 , rounds(10)
78         {
79         }
80 };
81
82 class ModuleBCrypt : public Module
83 {
84         BCryptProvider bcrypt;
85
86  public:
87         ModuleBCrypt() : bcrypt(this)
88         {
89         }
90
91         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
92         {
93                 ConfigTag* conf = ServerInstance->Config->ConfValue("bcrypt");
94                 bcrypt.rounds = conf->getUInt("rounds", 10, 1);
95         }
96
97         Version GetVersion() CXX11_OVERRIDE
98         {
99                 return Version("Implements bcrypt hashing", VF_VENDOR);
100         }
101 };
102
103 MODULE_INIT(ModuleBCrypt)