1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
19 class HashProvider : public DataProvider
22 const unsigned int out_size;
23 const unsigned int block_size;
24 HashProvider(Module* mod, const std::string& Name, int osiz, int bsiz)
25 : DataProvider(mod, Name), out_size(osiz), block_size(bsiz) {}
26 virtual std::string sum(const std::string& data) = 0;
27 inline std::string hexsum(const std::string& data)
29 return BinToHex(sum(data));
32 inline std::string b64sum(const std::string& data)
34 return BinToBase64(sum(data), NULL, 0);
37 /** Allows the IVs for the hash to be specified. As the choice of initial IV is
38 * important for the security of a hash, this should not be used except to
39 * maintain backwards compatability. This also allows you to change the hex
40 * sequence from its default of "0123456789abcdef", which does not improve the
41 * strength of the output, but helps confuse those attempting to implement it.
45 * unsigned int iv[] = { 0xFFFFFFFF, 0x00000000, 0xAAAAAAAA, 0xCCCCCCCC };
46 * std::string result = Hash.sumIV(iv, "fedcba9876543210", "data");
49 virtual std::string sumIV(unsigned int* IV, const char* HexMap, const std::string &sdata) = 0;
51 /** HMAC algorithm, RFC 2104 */
52 std::string hmac(const std::string& key, const std::string& msg)
54 std::string hmac1, hmac2;
55 std::string kbuf = key.length() > block_size ? sum(key) : key;
56 kbuf.resize(block_size);
58 for (size_t n = 0; n < block_size; n++)
60 hmac1.push_back(static_cast<char>(kbuf[n] ^ 0x5C));
61 hmac2.push_back(static_cast<char>(kbuf[n] ^ 0x36));
64 hmac1.append(sum(hmac2));