]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/hash.h
m_namedmodes Only show chan key to members and opers with channels/auspex
[user/henk/code/inspircd.git] / src / modules / hash.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2010 Daniel De Graaf <danieldg@inspircd.org>
5  *
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.
9  *
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
13  * details.
14  *
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/>.
17  */
18
19
20 #ifndef HASH_H
21 #define HASH_H
22
23 #include "modules.h"
24
25 class HashProvider : public DataProvider
26 {
27  public:
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)
34         {
35                 return BinToHex(sum(data));
36         }
37
38         inline std::string b64sum(const std::string& data)
39         {
40                 return BinToBase64(sum(data), NULL, 0);
41         }
42
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.
48          *
49          * Example:
50          * \code
51          * unsigned int iv[] = { 0xFFFFFFFF, 0x00000000, 0xAAAAAAAA, 0xCCCCCCCC };
52          * std::string result = Hash.sumIV(iv, "fedcba9876543210", "data");
53          * \endcode
54          */
55         virtual std::string sumIV(unsigned int* IV, const char* HexMap, const std::string &sdata) = 0;
56
57         /** HMAC algorithm, RFC 2104 */
58         std::string hmac(const std::string& key, const std::string& msg)
59         {
60                 std::string hmac1, hmac2;
61                 std::string kbuf = key.length() > block_size ? sum(key) : key;
62                 kbuf.resize(block_size);
63
64                 for (size_t n = 0; n < block_size; n++)
65                 {
66                         hmac1.push_back(static_cast<char>(kbuf[n] ^ 0x5C));
67                         hmac2.push_back(static_cast<char>(kbuf[n] ^ 0x36));
68                 }
69                 hmac2.append(msg);
70                 hmac1.append(sum(hmac2));
71                 return sum(hmac1);
72         }
73 };
74
75 #endif
76