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/>.
25 class HashProvider : public DataProvider
28 const unsigned int out_size;
29 const unsigned int block_size;
30 HashProvider(Module* mod, const std::string& Name, int osiz, int bsiz)
31 : DataProvider(mod, Name), out_size(osiz), block_size(bsiz) {}
32 virtual std::string sum(const std::string& data) = 0;
33 inline std::string hexsum(const std::string& data)
35 return BinToHex(sum(data));
38 inline std::string b64sum(const std::string& data)
40 return BinToBase64(sum(data), NULL, 0);
43 /** Allows the IVs for the hash to be specified. As the choice of initial IV is
44 * important for the security of a hash, this should not be used except to
45 * maintain backwards compatability. This also allows you to change the hex
46 * sequence from its default of "0123456789abcdef", which does not improve the
47 * strength of the output, but helps confuse those attempting to implement it.
51 * unsigned int iv[] = { 0xFFFFFFFF, 0x00000000, 0xAAAAAAAA, 0xCCCCCCCC };
52 * std::string result = Hash.sumIV(iv, "fedcba9876543210", "data");
55 virtual std::string sumIV(unsigned int* IV, const char* HexMap, const std::string &sdata) = 0;
57 /** HMAC algorithm, RFC 2104 */
58 std::string hmac(const std::string& key, const std::string& msg)
60 std::string hmac1, hmac2;
61 std::string kbuf = key.length() > block_size ? sum(key) : key;
62 kbuf.resize(block_size);
64 for (size_t n = 0; n < block_size; n++)
66 hmac1.push_back(static_cast<char>(kbuf[n] ^ 0x5C));
67 hmac2.push_back(static_cast<char>(kbuf[n] ^ 0x36));
70 hmac1.append(sum(hmac2));