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