1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
19 #define SHA256_DIGEST_SIZE (256 / 8)
20 #define SHA256_BLOCK_SIZE (512 / 8)
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.
27 class HashRequest : public Request
29 /** The keys (IV) to use */
31 /** The output characters (hex sequence) to use */
33 /** The string to hash */
36 /** Initialize HashRequest as an Hash_RESET message */
37 HashRequest(const char* req, Module* Me, Module* Target) : Request(Me, Target, req)
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)
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("")
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("")
56 /** Get data to be hashed */
57 const char* GetHashData()
59 return tohash.c_str();
62 /** Get keys (IVs) to be used */
63 unsigned int* GetKeyData()
68 /** Get output characters (hex sequence) to be used */
69 const char* GetOutputs()
75 /** Send this class to the hashing module to query for its name.
79 * cout << "Using hash algorithm: " << HashNameRequest(this, HashModule).Send();
82 class HashNameRequest : public HashRequest
85 /** Initialize HashNameRequest for sending.
86 * @param Me A pointer to the sending module
87 * @param Target A pointer to the hashing module
89 HashNameRequest(Module* Me, Module* Target) : HashRequest("NAME", Me, Target)
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.
101 * // Reset the Hash module.
102 * HashResetRequest(this, HashModule).Send();
105 class HashResetRequest : public HashRequest
108 /** Initialize HashResetRequest for sending.
109 * @param Me A pointer to the sending module
110 * @param Target A pointer to the hashing module
112 HashResetRequest(Module* Me, Module* Target) : HashRequest("RESET", Me, Target)
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().
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();
130 class HashSumRequest : public HashRequest
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
138 HashSumRequest(Module* Me, Module* Target, const std::string &sdata) : HashRequest(Me, Target, sdata)
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)
151 * unsigned int iv[] = { 0xFFFFFFFF, 0x00000000, 0xAAAAAAAA, 0xCCCCCCCC };
152 * HashKeyRequest(this, HashModule, iv);
155 class HashKeyRequest : public HashRequest
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.
164 HashKeyRequest(Module* Me, Module* Target, unsigned int* sdata) : HashRequest(Me, Target, sdata)
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).
177 * static const char tab[] = "fedcba9876543210";
178 * HashHexRequest(this, HashModule, tab);
181 class HashHexRequest : public HashRequest
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.
190 HashHexRequest(Module* Me, Module* Target, const char* sdata) : HashRequest(Me, Target, sdata)