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(Module* Me, Module* Target) : Request(Me, Target, "RESET")
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 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.
85 * // Reset the Hash module.
86 * HashResetRequest(this, HashModule).Send();
89 class HashResetRequest : public HashRequest
92 /** Initialize HashResetRequest for sending.
93 * @param Me A pointer to the sending module
94 * @param Target A pointer to the hashing.so module
96 HashResetRequest(Module* Me, Module* Target) : HashRequest(Me, Target)
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().
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();
114 class HashSumRequest : public HashRequest
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
122 HashSumRequest(Module* Me, Module* Target, const std::string &data) : HashRequest(Me, Target, data)
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)
135 * unsigned int iv[] = { 0xFFFFFFFF, 0x00000000, 0xAAAAAAAA, 0xCCCCCCCC };
136 * HashKeyRequest(this, HashModule, iv);
139 class HashKeyRequest : public HashRequest
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.
148 HashKeyRequest(Module* Me, Module* Target, unsigned int* data) : HashRequest(Me, Target, data)
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).
161 * static const char tab[] = "fedcba9876543210";
162 * HashHexRequest(this, HashModule, tab);
165 class HashHexRequest : public HashRequest
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.
174 HashHexRequest(Module* Me, Module* Target, const char* data) : HashRequest(Me, Target, data)