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