2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2010 Daniel De Graaf <danieldg@inspircd.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/>.
24 class HashProvider : public DataProvider
27 const unsigned int out_size;
28 const unsigned int block_size;
29 HashProvider(Module* mod, const std::string& Name, int osiz, int bsiz)
30 : DataProvider(mod, Name), out_size(osiz), block_size(bsiz) {}
31 virtual std::string sum(const std::string& data) = 0;
32 inline std::string hexsum(const std::string& data)
34 return BinToHex(sum(data));
37 inline std::string b64sum(const std::string& data)
39 return BinToBase64(sum(data), NULL, 0);
42 /** HMAC algorithm, RFC 2104 */
43 std::string hmac(const std::string& key, const std::string& msg)
45 std::string hmac1, hmac2;
46 std::string kbuf = key.length() > block_size ? sum(key) : key;
47 kbuf.resize(block_size);
49 for (size_t n = 0; n < block_size; n++)
51 hmac1.push_back(static_cast<char>(kbuf[n] ^ 0x5C));
52 hmac2.push_back(static_cast<char>(kbuf[n] ^ 0x36));
55 hmac1.append(sum(hmac2));