2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2014 Daniel Vassdal <shutter@canternet.org>
5 * Copyright (C) 2013 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
7 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
8 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
9 * Copyright (C) 2006 Craig Edwards <brain@inspircd.org>
11 * This file is part of InspIRCd. InspIRCd is free software: you can
12 * redistribute it and/or modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation, version 2.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 class HashProvider : public DataProvider
32 const unsigned int out_size;
33 const unsigned int block_size;
34 HashProvider(Module* mod, const std::string& Name, unsigned int osiz = 0, unsigned int bsiz = 0)
35 : DataProvider(mod, "hash/" + Name), out_size(osiz), block_size(bsiz)
39 virtual std::string GenerateRaw(const std::string& data) = 0;
41 virtual std::string ToPrintable(const std::string& raw)
46 virtual bool Compare(const std::string& input, const std::string& hash)
48 return InspIRCd::TimingSafeCompare(Generate(input), hash);
51 std::string Generate(const std::string& data)
53 return ToPrintable(GenerateRaw(data));
56 /** HMAC algorithm, RFC 2104 */
57 std::string hmac(const std::string& key, const std::string& msg)
59 std::string hmac1, hmac2;
60 std::string kbuf = key.length() > block_size ? GenerateRaw(key) : key;
61 kbuf.resize(block_size);
63 for (size_t n = 0; n < block_size; n++)
65 hmac1.push_back(static_cast<char>(kbuf[n] ^ 0x5C));
66 hmac2.push_back(static_cast<char>(kbuf[n] ^ 0x36));
69 hmac1.append(GenerateRaw(hmac2));
70 return GenerateRaw(hmac1);