]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/hash.h
Use IsCTCP in blockcolor for ignoring CTCPs.
[user/henk/code/inspircd.git] / include / modules / hash.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2014 Daniel Vassdal <shutter@canternet.org>
5  *   Copyright (C) 2013 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
8  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
9  *   Copyright (C) 2006 Craig Edwards <brain@inspircd.org>
10  *
11  * This file is part of InspIRCd.  InspIRCd is free software: you can
12  * redistribute it and/or modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation, version 2.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24
25 #pragma once
26
27 #include "modules.h"
28
29 class HashProvider : public DataProvider
30 {
31  public:
32         const unsigned int out_size;
33         const unsigned int block_size;
34         HashProvider(Module* mod, const std::string& Name, unsigned int osiz = 0, unsigned int bsiz = 0)
35                 : DataProvider(mod, "hash/" + Name), out_size(osiz), block_size(bsiz)
36         {
37         }
38
39         virtual std::string GenerateRaw(const std::string& data) = 0;
40
41         virtual std::string ToPrintable(const std::string& raw)
42         {
43                 return BinToHex(raw);
44         }
45
46         virtual bool Compare(const std::string& input, const std::string& hash)
47         {
48                 return InspIRCd::TimingSafeCompare(Generate(input), hash);
49         }
50
51         std::string Generate(const std::string& data)
52         {
53                 return ToPrintable(GenerateRaw(data));
54         }
55
56         /** HMAC algorithm, RFC 2104 */
57         std::string hmac(const std::string& key, const std::string& msg)
58         {
59                 std::string hmac1, hmac2;
60                 std::string kbuf = key.length() > block_size ? GenerateRaw(key) : key;
61                 kbuf.resize(block_size);
62
63                 for (size_t n = 0; n < block_size; n++)
64                 {
65                         hmac1.push_back(static_cast<char>(kbuf[n] ^ 0x5C));
66                         hmac2.push_back(static_cast<char>(kbuf[n] ^ 0x36));
67                 }
68                 hmac2.append(msg);
69                 hmac1.append(GenerateRaw(hmac2));
70                 return GenerateRaw(hmac1);
71         }
72
73         bool IsKDF() const
74         {
75                 return (!block_size);
76         }
77 };