1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
22 #define SHA256_DIGEST_SIZE (256 / 8)
23 #define SHA256_BLOCK_SIZE (512 / 8)
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.
30 class HashRequest : public Request
32 /** The keys (IV) to use */
34 /** The output characters (hex sequence) to use */
36 /** The string to hash */
39 /** Initialize HashRequest as an Hash_RESET message */
40 HashRequest(const char* req, Module* Me, Module* Target) : Request(Me, Target, req)
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)
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("")
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("")
59 /** Get data to be hashed */
60 const char* GetHashData()
62 return tohash.c_str();
65 /** Get keys (IVs) to be used */
66 unsigned int* GetKeyData()
71 /** Get output characters (hex sequence) to be used */
72 const char* GetOutputs()
78 /** Send this class to the hashing module to query for its name.
82 * cout << "Using hash algorithm: " << HashNameRequest(this, HashModule).Send();
85 class HashNameRequest : public HashRequest
88 /** Initialize HashNameRequest for sending.
89 * @param Me A pointer to the sending module
90 * @param Target A pointer to the hashing module
92 HashNameRequest(Module* Me, Module* Target) : HashRequest("NAME", Me, Target)
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.
104 * // Reset the Hash module.
105 * HashResetRequest(this, HashModule).Send();
108 class HashResetRequest : public HashRequest
111 /** Initialize HashResetRequest for sending.
112 * @param Me A pointer to the sending module
113 * @param Target A pointer to the hashing module
115 HashResetRequest(Module* Me, Module* Target) : HashRequest("RESET", Me, Target)
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().
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();
133 class HashSumRequest : public HashRequest
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
141 HashSumRequest(Module* Me, Module* Target, const std::string &data) : HashRequest(Me, Target, data)
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)
154 * unsigned int iv[] = { 0xFFFFFFFF, 0x00000000, 0xAAAAAAAA, 0xCCCCCCCC };
155 * HashKeyRequest(this, HashModule, iv);
158 class HashKeyRequest : public HashRequest
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.
167 HashKeyRequest(Module* Me, Module* Target, unsigned int* data) : HashRequest(Me, Target, data)
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).
180 * static const char tab[] = "fedcba9876543210";
181 * HashHexRequest(this, HashModule, tab);
184 class HashHexRequest : public HashRequest
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.
193 HashHexRequest(Module* Me, Module* Target, const char* data) : HashRequest(Me, Target, data)