]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_hash.h
Record compression ratio stats for a /stats char (this isnt finished yet)
[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(const char* req, Module* Me, Module* Target) : Request(Me, Target, req)
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 the hashing module to query for its name.
79  *
80  * Example:
81  * \code
82  * cout << "Using hash algorithm: " << HashNameRequest(this, HashModule).Send();
83  * \endcode
84  */
85 class HashNameRequest : public HashRequest
86 {
87  public:
88         /** Initialize HashNameRequest for sending.
89          * @param Me A pointer to the sending module
90          * @param Target A pointer to the hashing module
91          */
92         HashNameRequest(Module* Me, Module* Target) : HashRequest("NAME", Me, Target)
93         {
94         }
95 };
96
97 /** Send this class to the hashing module to reset the Hash module to a known state.
98  * This will reset the IV to the defaults specified by the Hash spec,
99  * and reset the hex sequence to "0123456789abcdef". It should be sent before
100  * ANY other Request types.
101  *
102  * Example:
103  * \code
104  * // Reset the Hash module.
105  * HashResetRequest(this, HashModule).Send();
106  * \endcode
107  */
108 class HashResetRequest : public HashRequest
109 {
110  public:
111         /** Initialize HashResetRequest for sending.
112          * @param Me A pointer to the sending module
113          * @param Target A pointer to the hashing module
114          */
115         HashResetRequest(Module* Me, Module* Target) : HashRequest("RESET", Me, Target)
116         {
117         }
118 };
119
120 /** Send this class to the hashing module to HashSUM a std::string.
121  * You should make sure you know the state of the module before you send this
122  * class, e.g. by first sending an HashResetRequest class. The hash will be
123  * returned when you call Send().
124  *
125  * Example:
126  * \code
127  * // ALWAYS ALWAYS reset first, or set your own IV and hex chars.
128  * HashResetRequest(this, HashModule).Send();
129  * // Get the Hash sum of the string 'doodads'.
130  * std::string result = HashSumRequest(this, HashModule, "doodads").Send();
131  * \endcode
132  */
133 class HashSumRequest : public HashRequest
134 {
135  public:
136         /** Initialize HashSumRequest for sending.
137          * @param Me A pointer to the sending module
138          * @param Target A pointer to the hashing module
139          * @param data The data to be hashed
140          */
141         HashSumRequest(Module* Me, Module* Target, const std::string &data) : HashRequest(Me, Target, data)
142         {
143         }
144 };
145
146 /** Send this class to hashing module to change the IVs (keys) to use for hashing.
147  * You should make sure you know the state of the module before you send this
148  * class, e.g. by first sending an HashResetRequest class. The default values for
149  * the IV's are those specified in the Hash specification. Only in very special
150  * circumstances should you need to change the IV's (see for example m_cloaking.cpp)
151  *
152  * Example:
153  * \code
154  * unsigned int iv[] = { 0xFFFFFFFF, 0x00000000, 0xAAAAAAAA, 0xCCCCCCCC };
155  * HashKeyRequest(this, HashModule, iv);
156  * \endcode
157  */
158 class HashKeyRequest : public HashRequest
159 {
160  public:
161         /** Initialize HashKeyRequest for sending.
162          * @param Me A pointer to the sending module
163          * @param Target A pointer to the hashing module
164          * @param data The new IV's. This should be an array of exactly four 32 bit values.
165          * On 64-bit architectures, the upper 32 bits of the values will be discarded.
166          */
167         HashKeyRequest(Module* Me, Module* Target, unsigned int* data) : HashRequest(Me, Target, data)
168         {
169         }
170 };
171
172 /** Send this class to the hashing module to change the hex sequence to use for generating the returned value.
173  * You should make sure you know the state of the module before you send this
174  * class, e.g. by first sending an HashResetRequest class. The default value for
175  * the hex sequence is "0123456789abcdef". Only in very special circumstances should
176  * you need to change the hex sequence (see for example m_cloaking.cpp).
177  *
178  * Example:
179  * \code
180  * static const char tab[] = "fedcba9876543210";
181  * HashHexRequest(this, HashModule, tab);
182  * \endcode
183  */
184 class HashHexRequest : public HashRequest
185 {
186  public:
187         /** Initialize HashHexRequest for sending.
188          * @param Me A pointer to the sending module
189          * @param Target A pointer to the hashing module
190          * @param data The hex sequence to use. This should contain exactly 16 ASCII characters,
191          * terminated by a NULL char.
192          */
193         HashHexRequest(Module* Me, Module* Target, const char* data) : HashRequest(Me, Target, data)
194         {
195         }
196 };
197
198 #endif
199