]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_hash.h
0c9eecdca40f6b8e9741a8d7091a5769ff7bc7c5
[user/henk/code/inspircd.git] / src / modules / m_hash.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #ifndef __HASH_H__
18 #define __HASH_H__
19
20 #include "modules.h"
21
22 #define SHA256_DIGEST_SIZE (256 / 8)
23 #define SHA256_BLOCK_SIZE  (512 / 8)
24
25 /** HashRequest is the base class used to send Hash requests to hashing.so.
26  * You should not instantiate classes of type HashRequest directly, instead
27  * you should instantiate classes of type HashResetRequest, HashSumRequest,
28  * HashKeyRequest and HashHexRequest, shown below.
29  */
30 class HashRequest : public Request
31 {
32         /** The keys (IV) to use */
33         unsigned int* keys;
34         /** The output characters (hex sequence) to use */
35         const char* outputs;
36         /** The string to hash */
37         std::string tohash;
38  public:
39         /** Initialize HashRequest as an Hash_RESET message */
40         HashRequest(Module* Me, Module* Target) : Request(Me, Target, "RESET")
41         {
42         }
43
44         /** Initialize HashRequest as an Hash_SUM message */
45         HashRequest(Module* Me, Module* Target, const std::string &hashable) : Request(Me, Target, "SUM"), keys(NULL), outputs(NULL), tohash(hashable)
46         {
47         }
48
49         /** Initialize HashRequest as an Hash_KEY message */
50         HashRequest(Module* Me, Module* Target, unsigned int* k) : Request(Me, Target, "KEY"), keys(k), outputs(NULL), tohash("")
51         {
52         }
53
54         /** Initialize HashRequest as an Hash_HEX message */
55         HashRequest(Module* Me, Module* Target, const char* out) : Request(Me, Target, "HEX"), keys(NULL), outputs(out), tohash("")
56         {
57         }
58
59         /** Get data to be hashed */
60         const char* GetHashData()
61         {
62                 return tohash.c_str();
63         }
64
65         /** Get keys (IVs) to be used */
66         unsigned int* GetKeyData()
67         {
68                 return keys;
69         }
70
71         /** Get output characters (hex sequence) to be used */
72         const char* GetOutputs()
73         {
74                 return outputs;
75         }
76 };
77
78 /** Send this class to hashing.so to reset the Hash module to a known state.
79  * This will reset the IV to the defaults specified by the Hash spec,
80  * and reset the hex sequence to "0123456789abcdef". It should be sent before
81  * ANY other Request types.
82  *
83  * Example:
84  * \code
85  * // Reset the Hash module.
86  * HashResetRequest(this, HashModule).Send();
87  * \endcode
88  */
89 class HashResetRequest : public HashRequest
90 {
91  public:
92         /** Initialize HashResetRequest for sending.
93          * @param Me A pointer to the sending module
94          * @param Target A pointer to the hashing.so module
95          */
96         HashResetRequest(Module* Me, Module* Target) : HashRequest(Me, Target)
97         {
98         }
99 };
100
101 /** Send this class to hashing.so to HashSUM a std::string.
102  * You should make sure you know the state of the module before you send this
103  * class, e.g. by first sending an HashResetRequest class. The hash will be
104  * returned when you call Send().
105  *
106  * Example:
107  * \code
108  * // ALWAYS ALWAYS reset first, or set your own IV and hex chars.
109  * HashResetRequest(this, HashModule).Send();
110  * // Get the Hash sum of the string 'doodads'.
111  * std::string result = HashSumRequest(this, HashModule, "doodads").Send();
112  * \endcode
113  */
114 class HashSumRequest : public HashRequest
115 {
116  public:
117         /** Initialize HashSumRequest for sending.
118          * @param Me A pointer to the sending module
119          * @param Target A pointer to the hashing.so module
120          * @param data The data to be hashed
121          */
122         HashSumRequest(Module* Me, Module* Target, const std::string &data) : HashRequest(Me, Target, data)
123         {
124         }
125 };
126
127 /** Send this class to hashing.so to change the IVs (keys) to use for hashing.
128  * You should make sure you know the state of the module before you send this
129  * class, e.g. by first sending an HashResetRequest class. The default values for
130  * the IV's are those specified in the Hash specification. Only in very special
131  * circumstances should you need to change the IV's (see for example m_cloaking.cpp)
132  *
133  * Example:
134  * \code
135  * unsigned int iv[] = { 0xFFFFFFFF, 0x00000000, 0xAAAAAAAA, 0xCCCCCCCC };
136  * HashKeyRequest(this, HashModule, iv);
137  * \endcode
138  */
139 class HashKeyRequest : public HashRequest
140 {
141  public:
142         /** Initialize HashKeyRequest for sending.
143          * @param Me A pointer to the sending module
144          * @param Target A pointer to the hashing.so module
145          * @param data The new IV's. This should be an array of exactly four 32 bit values.
146          * On 64-bit architectures, the upper 32 bits of the values will be discarded.
147          */
148         HashKeyRequest(Module* Me, Module* Target, unsigned int* data) : HashRequest(Me, Target, data)
149         {
150         }
151 };
152
153 /** Send this class to hashing.so to change the hex sequence to use for generating the returned value.
154  * You should make sure you know the state of the module before you send this
155  * class, e.g. by first sending an HashResetRequest class. The default value for
156  * the hex sequence is "0123456789abcdef". Only in very special circumstances should
157  * you need to change the hex sequence (see for example m_cloaking.cpp).
158  *
159  * Example:
160  * \code
161  * static const char tab[] = "fedcba9876543210";
162  * HashHexRequest(this, HashModule, tab);
163  * \endcode
164  */
165 class HashHexRequest : public HashRequest
166 {
167  public:
168         /** Initialize HashHexRequest for sending.
169          * @param Me A pointer to the sending module
170          * @param Target A pointer to the hashing.so module
171          * @param data The hex sequence to use. This should contain exactly 16 ASCII characters,
172          * terminated by a NULL char.
173          */
174         HashHexRequest(Module* Me, Module* Target, const char* data) : HashRequest(Me, Target, data)
175         {
176         }
177 };
178
179 #endif
180