2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2017-2018 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2014 Daniel Vassdal <shutter@canternet.org>
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.
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
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/>.
20 /// $CompilerFlags: -Ivendor_directory("bcrypt")
24 #include "modules/hash.h"
26 #include <crypt_blowfish.c>
28 class BCryptProvider : public HashProvider
34 for (unsigned int i = 0; i < sizeof(entropy); ++i)
35 entropy[i] = ServerInstance->GenRandomInt(0xFF);
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");
47 std::string Generate(const std::string& data, const std::string& salt)
50 _crypt_blowfish_rn(data.c_str(), salt.c_str(), hash, sizeof(hash));
54 std::string GenerateRaw(const std::string& data) CXX11_OVERRIDE
56 return Generate(data, Salt());
59 bool Compare(const std::string& input, const std::string& hash) CXX11_OVERRIDE
61 std::string ret = Generate(input, hash);
70 std::string ToPrintable(const std::string& raw) CXX11_OVERRIDE
75 BCryptProvider(Module* parent)
76 : HashProvider(parent, "bcrypt", 60)
82 class ModuleBCrypt : public Module
84 BCryptProvider bcrypt;
87 ModuleBCrypt() : bcrypt(this)
91 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
93 ConfigTag* conf = ServerInstance->Config->ConfValue("bcrypt");
94 bcrypt.rounds = conf->getUInt("rounds", 10, 1);
97 Version GetVersion() CXX11_OVERRIDE
99 return Version("Allows other modules to generate bcrypt hashes.", VF_VENDOR);
103 MODULE_INIT(ModuleBCrypt)