]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/hash.h
Add ModeHandler::IsListModeBase() and MC_LIST
[user/henk/code/inspircd.git] / include / 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 #pragma once
21
22 #include "modules.h"
23
24 class HashProvider : public DataProvider
25 {
26  public:
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)
33         {
34                 return BinToHex(sum(data));
35         }
36
37         inline std::string b64sum(const std::string& data)
38         {
39                 return BinToBase64(sum(data), NULL, 0);
40         }
41
42         /** HMAC algorithm, RFC 2104 */
43         std::string hmac(const std::string& key, const std::string& msg)
44         {
45                 std::string hmac1, hmac2;
46                 std::string kbuf = key.length() > block_size ? sum(key) : key;
47                 kbuf.resize(block_size);
48
49                 for (size_t n = 0; n < block_size; n++)
50                 {
51                         hmac1.push_back(static_cast<char>(kbuf[n] ^ 0x5C));
52                         hmac2.push_back(static_cast<char>(kbuf[n] ^ 0x36));
53                 }
54                 hmac2.append(msg);
55                 hmac1.append(sum(hmac2));
56                 return sum(hmac1);
57         }
58 };