2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
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.
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
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/>.
19 /// $CompilerFlags: -Ivendor_directory("bcrypt")
23 #include "modules/hash.h"
25 #include <crypt_blowfish.c>
27 class BCryptProvider : public HashProvider
33 for (unsigned int i = 0; i < sizeof(entropy); ++i)
34 entropy[i] = ServerInstance->GenRandomInt(0xFF);
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");
46 std::string Generate(const std::string& data, const std::string& salt)
49 _crypt_blowfish_rn(data.c_str(), salt.c_str(), hash, sizeof(hash));
53 std::string GenerateRaw(const std::string& data) CXX11_OVERRIDE
55 return Generate(data, Salt());
58 bool Compare(const std::string& input, const std::string& hash) CXX11_OVERRIDE
60 std::string ret = Generate(input, hash);
69 std::string ToPrintable(const std::string& raw) CXX11_OVERRIDE
74 BCryptProvider(Module* parent)
75 : HashProvider(parent, "bcrypt", 60)
81 class ModuleBCrypt : public Module
83 BCryptProvider bcrypt;
86 ModuleBCrypt() : bcrypt(this)
90 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
92 ConfigTag* conf = ServerInstance->Config->ConfValue("bcrypt");
93 bcrypt.rounds = conf->getUInt("rounds", 10, 1);
96 Version GetVersion() CXX11_OVERRIDE
98 return Version("Implements bcrypt hashing", VF_VENDOR);
102 MODULE_INIT(ModuleBCrypt)