]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/hash.h
Change channel name parameter of Module::OnUserPreJoin() and Channel::JoinUser()...
[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         /** HMAC algorithm, RFC 2104 */
44         std::string hmac(const std::string& key, const std::string& msg)
45         {
46                 std::string hmac1, hmac2;
47                 std::string kbuf = key.length() > block_size ? sum(key) : key;
48                 kbuf.resize(block_size);
49
50                 for (size_t n = 0; n < block_size; n++)
51                 {
52                         hmac1.push_back(static_cast<char>(kbuf[n] ^ 0x5C));
53                         hmac2.push_back(static_cast<char>(kbuf[n] ^ 0x36));
54                 }
55                 hmac2.append(msg);
56                 hmac1.append(sum(hmac2));
57                 return sum(hmac1);
58         }
59 };
60
61 #endif
62