OSDN Git Service

Minimize the headers to be included to the bare minimum.
[mhash384/mhash384.git] / README.md
1 % ![](img/mhash/MHash-384.jpg)  
2 MHash-384
3 % Simple fast portable header-only hashing library
4
5 # Introduction
6
7 **MHash-384** is a fast, portable and secure *header-only* hash library, released under the *MIT license*. It provides a very simple "stream processing" API and produces hash values with a length of 384 bits (48 bytes).
8
9 The MHash-384 library has originally been written for **C** and **C++**. It provides a "plain C" API as well as an *object-oriented* C++ wrapper. It also supports many compilers (MSVC, GCC, MinGW, etc.) on various platforms (Windows, Linux, etc).
10
11 Furthermore, the MHash-384 library has already been *ported* to various other programming languages. This currently includes the **Microsoft.NET** platform (C#, VB.NET, etc.), **Java**, **Delphi** (Pascal) as well as **Python**.
12
13
14 # Quick Start Guide
15
16 In order to use the *MHash-384* library, simply include the header file `mhash_384.h` in your *C* or *C++* source code file.
17
18 This is the *only* file you are going to need. Being a [*header-only*](https://en.wikipedia.org/wiki/Header-only) library, MHash-384 does **not** require any additional library files to be linked to your program. Also, **no** additional DLL files (or *shared objects*) are required at runtime.
19
20         #include <mhash_384.h>
21
22 ## Example for C language
23
24 If you source code is written in plain **C**, simply use the provided *global* functions:
25
26         /*variables*/
27         const uint8_t *data_ptr;
28         uint32_t data_len;
29         uint8_t result[MHASH_384_LEN];
30         mhash_384_t context;
31         
32         /*initialization*/
33         mhash_384_initialize(&context);
34         
35         /*input data processing*/
36         while(have_more_data())
37         {
38                 data_ptr = fetch_next_data_chunk(&data_len);
39                 mhash_384_update(&context, data_ptr, data_len);
40         }
41         
42         /*finalization*/
43         mhash_384_finalize(&context, result);
44
45 ## Example for C++ language
46
47 And, if you source code is written in **C++**, the *MHash384* class from *mhash* namespace is used:
48
49         /*variables*/
50         std::vector<uint8_t> data;
51         uint8_t result[MHASH_384_LEN];
52         
53         /*construction*/
54         mhash_384::MHash384 instance;
55         
56         /*input data processing*/
57         while(source.have_more_data())
58         {
59                 data = source.fetch_next_data_chunk();
60                 instance.update(data);
61         }
62         
63         /*finalization*/
64         instance.finalize(result);
65
66
67 # Command-line Usage
68
69 MHash-384 comes with a simple "standalone" command-line application. This program primarily serves as an example on how to use the library. However, the command-line application may also come in handy to quickly compute checksums (hashes) of local files. Furthermore, the MHash-384 program integrates nicely into the "pipes and filters" design pattern, by consuming arbitrary inputs from the standard input stream and writing hash values (as Hex string) to the standard output stream.
70
71 ## Synopsis
72
73 The MHash-384 command-line application takes a number of optional options followed by an one or more input files.
74
75 If **no** input file is specified, input will be read from standard input (*stdin*).
76
77 The digest will be written to the standard output (*stdout*). Diagnostic message are written to the standard error (*stderr*).
78
79         mhash_384 [options] [file_1] [file_2] ... [file_n]
80
81 ## Options
82
83 MHash-384 supports the following options:
84
85 * **`-p`**, **`--progress`**  
86   Print the total size of the input file and the percentage processed so far to *stderr* while hash computation is running.  
87   If the total input size can **not** be determined (e.g. using pipe), the number of bytes processed so far is printed.
88
89 * **`-u`**, **`--upper`**  
90   Output the final digest (hash) in *upper case* Hex letters. Default mode is *lower case*.
91
92 * **`-c`**, **`--curly`**  
93   Output the final digest (hash) using C-style curly brackets. Also each byte will be written as a separate Hex value.
94
95 * **`-r`**, **`--raw`**  
96   Output the final digest (hash) as "raw" bytes. Default mode converts the final digest (hash) to a Hex string.  
97   *Note:* This is incompatible with the `-u` (`--upper`) and `-c` (`--curly`) options!
98
99 * **`-b`**, **`--bench`**  
100   Compute performance statistics (e.g. bytes processed per second) and print them to the *stderr* at the end of the process.
101
102 * **`-v`**, **`--version`**  
103   Print library version to the *stdout* and exit program.
104
105 * **`-t`**, **`--test`**  
106   Run *built-in self-test* and exit program. Computes hashes from test vectors and compares results to reference hashes.
107
108 * **`-h`**, **`--help`**  
109   Print help screen (man page) and exit program.
110
111 ## Examples
112
113 Compute MHash-384 hash of a local file:
114
115         mhash_384 C:\Images\debian-8.3.0-amd64-DVD-1.iso"
116
117 Compute MHash-384 hash of a local file, with more verbose status outputs:
118
119         mhash_384 -p -b C:\Images\debian-8.3.0-amd64-DVD-1.iso"
120
121 Compute MHash-384 from random bytes, passing data directly from [`dd`](https://en.wikipedia.org/wiki/Dd_%28Unix%29) via pipe:
122
123         dd if=/dev/urandom bs=100 count=1 | mhash_384
124
125
126 # Algoritm Description
127
128 This chapter describes the of the MHash-384 algorithm, which is a novel design (from the scratch), in full detail.
129
130 ## Informal Description
131
132 We start with an informal description of the MHash-384 algorithm, which also explains the design goals:
133
134 ### The XOR table
135
136 MHash-384 uses a table of *257* pre-computed 384-Bit (48-byte) words. This table is referred to as the *XOR-table*. It has been generated in such a way that each possible pair of 384-Bit words has a [Hamming distance](https://en.wikipedia.org/wiki/Hamming_distance) of *at least* 182 bits. This means that every 384-Bit word (row) in the XOR-table differs from *all* other 384-Bit words in that table by about ½ of the bits (columns).
137
138 The MHash-384 digest of a given input message is computed in a *stream-like* fashion. This means that we start with the "empty" (all zeros) hash value, we will process the given input message *byte by byte*, and we will "update" the hash value for each input byte. The final hash value of a message is the hash value that results after all of its bytes have been processed.
139
140 The MHash-384 "update" rule is defined as follows: We select the 384-Bit word from the XOR-table whose index matches the current input byte value, and we *combine* the selected 384-Bit word with the previous hash value in order to form the new (updated) hash value. If, for example, the current input byte equals `0x00`, then we select the *first* 384-Bit word from the XOR-table. If the current input byte equals `0x01`, then we select the *second* 384-Bit word from the XOR-table. And so on&hellip;
141
142 In any case, the selected 384-Bit word (from the XOR-table) will be combined with the previous hash value using the binary [**XOR**](https://en.wikipedia.org/wiki/Exclusive_or) ("exclusive OR") function. XOR'ing the previous hash value with the selected 384-Bit word will effectively *flip* a certain subset of its bits. Because all of the 384-Bit words in the XOR-table have a minimum Hamming distance of about ½ of the hash's total length, each possible input byte value is guaranteed to flip a *different* subset of the hash's bits &ndash; a subset that differs from *all* other possible "flip" subsets (i.e. *all* other possible input byte values) at approximately ½ of the bit positions.
143
144 This is known as the [avalanche effect](https://en.wikipedia.org/wiki/Avalanche_effect). It means that if we apply a *minimal* change to the current input byte, e.g. we flip *one* of its bits (at random), then a *significant* change to the resulting hash value is induced, i.e. about ½ of the hash bits are flipped.
145
146 ### The MIX table
147
148 In fact the "update" rule described in the previous section is slightly more complex. That's because, in each update step, the previous hash value bytes *additionally* will be **shuffled** (permuted). The shuffling of the hash bytes will be performed immediately *before* XOR'ing the previous hash value with the selected (input-dependent) 384-Bit word from the XOR-table.
149
150 Be aware that, because of the *associativity* of the XOR function, simply XOR'ing a set of 384-Bit word from the XOR-table would always give the same result, regardless of the *order* in which those 384-Bit words are processed. Hence, input messages that only differ in the order of their message bytes, but besides that contain the same set of message bytes, would result in the same hash value &ndash; which clearly is undesirable! The shuffling of the hash bytes in each update step ensures that processing the *same* set of input bytes in a *different* order will result in a *different* hash value each time &ndash; with very high probability.
151
152 The shuffling procedure is implemented using the [Fisher-Yates](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) "in-place" shuffle algorithm. For this purpose, MHash-384 uses a table of *256* pre-computed permutations. This table is referred to as the *MIX-table*. Each of its rows, or permutations, contains 48 Fisher-Yates shuffling indices, as required to shuffle the 48 hash bytes. The MIX-table has been generated in such a way, that each permutation (MIX-table row) differs as much as possible from all the other permutations (MIX-table rows).
153
154 We use a counter that keeps track of the MIX-table row (permutation). The counter's value equals the zero-based index of the MIX-table row which is to be applied in the *next* update step. It is initialized to *zero* at the beginning, and it will be increased by one after each update step. After the *last* MIX-table row (i.e. index *255*) the counter will wrap around to index *zero* again.
155
156 ### The RND table
157
158 Furthermore, in each update step, to every byte of the hash value a constant byte value will be *added*. There is a distinct constant for each hash byte position, and a different set of constants is employed in subsequent update steps. In any case, the addition is performed modulus 256 &ndash; which means that results greater than or equal to 256 will wrap around to zero.
159
160 For this purpose, MHash-384 uses a table of *256* pre-defined 384-Bit (48-byte) words. This table is referred to as the *RND-table*. Each of its rows contains 48 "random" byte values &ndash; one for each hash byte position. The contents of the RND-table are based entirely on "true" **random** numbers. The randomness was collected from atmospheric noise, courtesy of [Random.org](https://www.random.org/).
161
162 The rationale is to ensure that there will be enough variation in the hash value bytes, even when there is very few variations in the given input bytes. Note that blending a perfectly uniform byte sequence with a *random* byte sequence yields a "random-looking" byte sequence, whereas blending two random byte sequences still yields a "random-looking" byte sequence. In other words, blending the given input sequence with the random byte sequence can only make the result "look" *more* random.
163
164 Of course, even though the RND-table was generated from "true" random numbers, the exactly same table is used in each hash computation, so the hash function remains deterministic. Also, we use the same counter to select the current RND-table row that is used to select the current MIX-table row, i.e. the "active" RND-table row will wrap around after 256 update steps.
165
166 ### The SBX table
167
168 In addition, after every update step, *all* bytes of the hash value will be subject to a **substitution** operation. For this purpose, MHash-384 uses a table of *256* pre-computed 48-byte (384-Bit) words. This table is referred to as the *SBX-table*. It has been generated in such a way that every *column* represents a permutation of the possible byte values, i.e. each of the byte values `0x00` to `0xFF` appears exactly *once* per column &ndash; in a "randomized" and column-specific order. Also, each of the 48 permutations (columns) stored in the SBX-table differs from all other permutations (columns) in the table as much as possible.
169
170 The substitution operation replaces the *i*&#x2011;th byte of the original hash value with a value chosen from the *i*&#x2011;th *column* of the SBX-table. More specifically, each byte is replaced by the value that is stored in the *row* whose index matches the original byte value. If, for example, the original byte value equals `0x00`, then it is replaced by the value from the *first* row of the SBX-table. If the original byte value equals `0x01`, then it is replaced by the value from the *second* row of the SBX-table. And so on&hellip;
171
172 The *non-linear* substitution operation improves the "confusion" between the input message and the resulting hash value.
173
174 ### Finalization
175
176 Last but not least, the computation of the MHash-384 digest is **finalized** by XOR'ing the current hash value, as it appears after the last message byte has been processed, with the very last 384-Bit word of the XOR-table &ndash; that 384-Bit word has index `0x256` and hence can *never* be selected by any input message byte value in a "regular" update step. Apart from this, the MIX, RND and SBX tables will be applied to the current hash value in the same way as in any "regular" update step.
177
178 The *unique* finalization step ensures that it will ***not*** be possible to "continue" the computation of a known *final* MHash-384 hash value &ndash; simply by appending further message bytes. Implementations that wish to do so may, of course, retain the current hash value, as it appeared right *before* the finalization step, and later "continue" the computation based on *that* value.
179
180 ## Pseudocode
181
182 The MHash-384 algorithm can be summed up with the following simple pseudocode:
183
184         procedure mhash384
185         const:
186           HASH_SIZE = 48
187           TABLE_INI: matrix[  2 × HASH_SIZE] of byte
188           TABLE_XOR: matrix[257 × HASH_SIZE] of byte
189           TABLE_MIX: matrix[256 × HASH_SIZE] of byte
190           TABLE_RND: matrix[256 × HASH_SIZE] of byte
191           TABLE_SBX: matrix[256 × HASH_SIZE] of byte
192         input:
193           message: array[N] of byte
194         output:
195           hash: array[HASH_SIZE] of byte
196         vars:
197           ctr: integer
198           val: byte
199           tmp_src: array[HASH_SIZE] of byte
200           tmp_dst: array[HASH_SIZE] of byte
201         begin
202           /*initialization*/
203           ctr ← 0
204           tmp_src ← TABLE_INI[0]
205           tmp_dst ← TABLE_INI[1]
206           
207           /*input message processing*/
208           for k = 0 to N-1 do
209             for i = 0 to HASH_SIZE-1 do
210               val ← ((tmp_src[TABLE_MIX[ctr,i]] ⊕ TABLE_XOR[message[k],i]) + TABLE_RND[ctr,i]) mod 256
211               tmp_dst[i] ← tmp_dst[i] ⊕ TABLE_SBX[val,i]
212             done
213             ctr ← (ctr + 1) mod 256
214             xchg(tmp_src ⇄ tmp_dst)
215           done
216           
217           /*finalization*/
218           for i = 0 to HASH_SIZE-1 do
219             val ← ((tmp_src[TABLE_MIX[ctr,i]] ⊕ TABLE_XOR[256,i]) + TABLE_RND[ctr,i]) mod 256
220             hash[i] ← tmp_dst[i] ⊕ TABLE_SBX[val,i]
221           done
222         end.
223
224 *Note:* The `⊕` symbol refers to the binary XOR operation, whereas `+` refers to the arithmetic add operation.
225
226
227 # Detailed API Specification
228
229 Global definitions for both, C and C++, API's.
230
231 ## Global definitions
232
233 ### MHASH_384_LEN
234
235 This constant specifies the length of a MHash-384 digest (hash value) in bytes/octets. It is equal to `48UL`.
236
237 A memory block (array) that is intended to hold a MHash-384 digest, e.g. the final result of the hash computation, needs to be at least `MHASH_384_LEN` bytes in size.
238
239 ### MHASH_384_VERSION_MAJOR
240
241 The MHash-384 library *major* version. Major release may change the API, so backwards compatibility is **not** guaranteed between different *major* versions. Applications generally are written for a specific *major* version of the library.
242
243 ### MHASH_384_VERSION_MINOR
244
245 The MHash-384 library *minor* version. Minor releases may add new features, but they do **not** break the API compatibility. Applications may require a certain minimum *minor* version of the library, but will work with higher *minor* versions too.
246
247 ### MHASH_384_VERSION_PATCH
248
249 The MHash-384 library *patch* level. Patch releases may include bug-fixes and optimizations, but they do **not** add new features or change the API. Application code does **not** need to care about the *patch* level of the library for compatibility.
250
251 ## API for for C language
252
253 All functions described in the following are *reentrant*, but **not** *thread-safe*. This means that *multiple* hash computation *can* be performed safely in an "interleaved" fashion, as long as each hash computation uses its own separate state variable. Also, multiple hash computation *can* be performed safely in "concurrent" threads, as long as each thread uses its own separate state variable. If, however, the *same* state variable needs to be accessed from (or shared between) *different* "concurrent" threads, then the application **must** *serialize* the function calls, e.g. by means of a *mutex lock* (or "critical section").
254
255 ### mhash_384_t
256
257         typedef struct mhash_384_t;
258
259 The `mhash_384_t` data-type is used to maintain the hash computation state. Use one instance (variable) of `mhash_384_t` for each ongoing hash computation. The memory for the `mhash_384_t` instance must be allocated by the calling application!
260
261 *Note:* Applications should treat this data-type as *opaque*, i.e. the application **must not** access the fields of the struct directly, because `mhash_384_t` may be subject to change in future library versions!
262
263 ### mhash_384_initialize()
264
265         void mhash_384_initialize(mhash_384_t *const ctx);
266
267 This function is used to initialize the hash computation, i.e. it will set up the initial hash computation state. The application is required to call this function *once* for each hash computation. The function has to be called ***before*** any input data is processed! The application may also call this function again in oder to *reset* the hash computation state.
268
269 *Parameters:*
270
271 * `mhash_384_t *ctx`  
272   Pointer to the hash computation state of type `mhash_384_t` that will be initialized (reset) by this operation. The application must allocate the memory holding the variable of type `mhash_384_t`. The previous state will be lost!
273
274 ### mhash_384_update()
275
276         void mhash_384_update(mhash_384_t *const ctx, const uint8_t *const input, const size_t len);
277
278 This function is used to process the next **N** bytes (octets) of input data. It will update the hash computation state accordingly. The application needs to call this function repeatedly, with *same* context (`mhash_384_t`), until all input has been processed.
279
280 *Parameters:*
281
282 * `mhash_384_t *ctx`  
283   Pointer to the hash computation state of type `mhash_384_t` that will be updated by this operation.
284
285 * `const uint8_t *input`  
286   Pointer to the input data to be processed by this operation. The input data needs to be located in one continuous block of memory. The given pointer specifies the *base address*, i.e. the address of the *first* byte (octet) to be processed.  
287   *Note:* Formally, the input data is defined as a sequence of `uint8_t`, i.e. a sequence of bytes (octets). However, *any* suitable *byte*-based (serializable) input data can be processed using the proper *typecast* operator.
288
289 * `size_t len`  
290   The *length* of the input data to be processed, in bytes (octets). All memory addresses in the range from `input` up to and including `input+(len-1)` will be processed as input. Applications need to carefully check `len` to avoid buffer overruns!
291
292 ### mhash_384_finalize()
293
294 This function is used to finalize the hash computation and output the final digest (hash value). Typically, the application will call this function *once*, **after** all input data has been processed.
295
296 *Note:* The hash computation state is treated *read-only* by this method. This means that the application *may* call the method at any time to get an "intermediate" hash of all input bytes (octets) process *so far* and then continue to process more input.
297
298         void mhash_384_finalize(const mhash_384_t *const ctx, uint8_t *const output);
299
300 * `const mhash_384_t *ctx`  
301   Pointer to the hash computation state of type `mhash_384_t` from which the final digest is computed.
302
303 * `uint8_t *output`
304   Pointer to the memory block where the final digest will be stored. This memory needs to be allocated by the calling application! The digest will be stored to the memory range from `output[0]` to `output[MHASH_384_LEN-1]` (inclusive).
305
306 ## API for for C++ language
307
308 All classes described in the following are *reentrant*, but **not** *thread-safe*. This means that *multiple* hash computation *can* be performed safely in an "interleaved" fashion, as long as each hash computation uses its own separate object (instance). Also, multiple hash computation *can* be performed safely in "concurrent" threads, as long as each thread uses its own separate object (instance). If, however, the *same* object (instance) needs to be accessed from (or shared between) *different* "concurrent" threads, then the application **must** *serialize* the method calls, e.g. by means of a *mutex lock* (or "critical section").
309
310 *Note:* The classes described in the following live in the `mhash` namespace. Any functions, data-types or constants in the `mhash::internals` namespace should be regarded *opaque* by the application; those may be subject to change!
311
312 ### Constructor
313
314         MHash384::MHash384(void);
315
316 Constructs a new `MHash384` object sets up the initial hash computation state. The application is required to use the *same* `MHash384` object for the entire hash computation.
317
318 ### update() [1]
319
320         void MHash384::update(const uint8_t *const input, const size_t len);
321
322 This function is used to process the next **N** bytes (octets) of input data. It will update the hash computation state accordingly. The application needs to call this function repeatedly, with *same* context (`mhash_384_t`), until all input has been processed.
323
324 *Parameters:*
325
326 * `const uint8_t *input`  
327   Pointer to the input data to be processed by this operation. The input data needs to be located in one continuous block of memory. The given pointer specifies the *base address*, i.e. the address of the *first* byte (octet) to be processed.  
328   *Note:* Formally, the input data is defined as a sequence of `uint8_t`, i.e. a sequence of bytes (octets). However, *any* suitable *byte*-based (serializable) input data can be processed using the proper *typecast* operator.
329
330 * `size_t len`  
331   The *length* of the input data to be processed, in bytes (octets). All memory adresses in the range from `input[0]` to `input[len-1]` (inclusive) will be processed. Applications need to carefully check `len` to avoid buffer overruns!
332
333 ### update() [2]
334
335         void MHash384::update(const std::vector<uint8_t> &input, const size_t offset = 0, const size_t len = 0);
336
337 This method is used to process a `std::vector<uint8_t>` as input. It will update the hash computation state accordingly. The application needs to call this method repeatedly, on the *same* `MHash364` instance, until all input data has been processed.
338
339 *Parameters:*
340
341 * `const std::vector<uint8_t> &input`  
342   Reference to the `std::vector<uint8_t>` object to be processed as input. By default, all bytes (octets) in the vector will be processed. Optionally, a sub-range of the vector can be selected.
343
344 * `size_t offset`  
345   Optional. Specifies the *zero-based* index of the *first* vector element to be processed. By default, processing starts at index **0**.
346
347 * `size_t len`  
348   Optional. Specifies the number of vector elements to be processed. All elements from index `offset` up to and including index `offset+(len-1)` will be processed. By default, the *whole* vector is being processed.
349
350 ### update() [3]
351
352         void MHash384::update(const std::string &input, const size_t offset = 0, const size_t len = 0);
353
354 This method is used to process a `std::string` as input. It will update the hash computation state accordingly. The application needs to call this method repeatedly, on the *same* `MHash364` instance, until all input data has been processed.
355
356 *Parameters:*
357
358 * `const std::vector<uint8_t> &input`  
359   Reference to the `std::string` object to be processed as input. By default, all characters (octets) in the string, excluding the terminating `NULL` character, will be processed. Optionally, a sub-range of the vector can be selected.
360
361 * `size_t offset`  
362   Optional. Specifies the *zero-based* index of the *first* character in the string to be processed. By default, processing starts at index **0**.
363
364 * `size_t len`  
365   Optional. Specifies the number of character to be processed. All characters from `offset[0]` to `offset[len-1]` (inclusive) will be processed. By default, the whole string, excluding the terminating `NULL` character, is processed.
366
367 ### finalize() [1]
368
369         void MHash384::finalize(uint8_t *const output) const;
370
371 This method is used to finalize the hash computation and output the final digest (hash value). Typically, the application will call this method *once*, and only ***after*** all input data has been processed.
372
373 *Note:* The hash computation state is treated *read-only* by this method. This means that the application *may* call the method at any time to get an "intermediate" hash of all input bytes (octets) process *so far* and then continue to process more input.
374
375 *Parameters:*
376
377 * `uint8_t *output`
378   Pointer to the memory block where the final digest will be stored. This memory needs to be allocated by the calling application! The digest will be stored to the memory range from `output[0]` to `output[MHASH_384_LEN-1]` (inclusive).
379
380 ### finalize() [2]
381
382         std::vector<uint8_t> MHash384::finalize(void) const;
383
384 This method is used to finalize the hash computation and output the final digest (hash value). Typically, the application will call this method *once*, **after** all input data has been processed.
385
386 *Note:* The hash computation state is treated *read-only* by this method. This means that the application *may* call the method at any time to get an "intermediate" hash of all input bytes (octets) process *so far* and then continue to process more input.
387
388 *Return value:*
389
390 * Returns a `std::vector<uint8_t>` containing the final digest (hash value). The size of the returned vector object will be exactly `MHASH_384_LEN` elements (octets).
391
392 ### reset()
393
394         void MHash384::reset(void);
395
396 Resets the `MHash384` object to the initial hash computation state, i.e. the object will be in the same state again as a newly constructed object. Use this function to compute multiple *separate* hash values with the same `MHash384` object.
397
398 *Note:* In order to do so, call this function *after* the *last* input byte (octet) of the **i**-th message has been processed (and *after* the final digest has been received), but *before* the *first* byte (octet) of the **i+1**-th message is processed.
399
400
401 # Supported Platforms
402
403 MHash-384 library should compile on any standard-compliant C/C++ compiler. In particular, the following platforms have been tested successfully:
404
405 * Microsoft Windows
406     - Microsoft C/C++ Compiler, using Visual Studio 2010 or later
407     - MinGW, using Mingw-w64 from [MSYS2](https://msys2.github.io/) project
408 * Intel C/C++ Compiler, version Version 15.0 (XE 2015) or later
409 * GNU/Linux, using GCC/G++, version 4.7 or later
410
411 # Language Bindings
412
413 While the MHash-384 library is primarily targeted for C/C++ applications, "native" ports of the library *are provided for a variety of programming languages. This allows for using the MHash-384 library in pretty much any scenario/environment.
414
415 ## Microsoft.NET
416
417 Bindings of the MHash-384 library are provided for **Microsoft.NET**, in the form of the `MHashDotNet384.dll` assembly.
418
419 In order to use the MHash-384 library in your Microsoft.NET (e.g. C# or VB.NET) application, simply import and instantiate the provided `MHash384` class from the `MHashDotNet384` namespace:
420
421         using MHashDotNet384;
422
423         String ComputeHash(FileStream fs)
424         {
425                 byte[] buffer = new byte[4096];
426                 MHash384 digest = new MHash384();
427                 while (true)
428                 {
429                         int count = fs.Read(buffer, 0, buffer.Length);
430                         if (count > 0)
431                         {
432                                 digest.Update(buffer, 0, count);
433                                 continue;
434                         }
435                         break;
436                 }
437                 return digest.ToString();
438         }
439
440 ### Prerequisites
441
442 In order to use the MHash-384 library in your Microsoft.NET (e.g. C# or VB.NET) application, a reference to the `MHashDotNet384.dll` assembly file **must** be added to the project.
443
444 ## Java
445
446 Bindings of the MHash-384 library are provided for **Java**, in the form of the `MHashJava384.jar` library.
447
448 In order to use the MHash-384 library in your Java-based application, simply import and instantiate the provided `MHash384` class from the `com.muldersoft.mhash384` package:
449
450         import com.muldersoft.mhash384.MHash384;
451         
452         String computeHash(final InputStream inputStream) throws IOException {
453                 final byte[] buffer = new byte[4096];
454                 final MHash384 mhash384 = new MHash384()
455                 int count;
456                 do {
457                         count = inputStream.read(buffer);
458                         if(count > 0) {
459                                 mhash384.update(buffer, 0, count);
460                         }
461                 }
462                 while(count == buffer.length);
463                 return mhash384.digest().toString();
464         }
465
466 ### Prerequisites
467
468 In order to use the MHash-384 library in your Java project, the `MHashJava384.jar` **must** be added to the Java *build path*.
469
470 ## Python
471
472 Bindings of the MHash-384 library are provided for **Python** (version 3.x), in the form of the `MHashPy384.py` module.
473
474 In order to use the MHash-384 library in your Python-based application, simply import and instantiate the provided `MHash384` class from the `MHashPy384` module:
475
476         from MHashPy384 import MHash384
477         
478         mhash384 = MHash384()
479         for chunk in read_chunks(fs):
480                 mhash384.update(chunk)
481         print(binascii.hexlify(mhash384.digest()))
482
483 ### Prerequisites
484
485 In order to use the MHash-384 library in your Python project, the `MHashPy384.py` **must** be in your Python *library path*.
486
487 *Note:* [CPython](https://www.python.org/), which is the most widely deployed Python interpreter, is known to be **very slow** for computation-intensive workloads, such as hash computations! Greatly improved speed can be achieved by using the [PyPy](http://pypy.org/) instead of CPython.
488
489 ## Delphi
490
491 Bindings of the MHash-384 library are provided for **Delphi**, in the form of the `MHash384.pas` unit.
492
493 In order to use the MHash-384 library in your Delphi application, simply add the `MHash384` unit to the *uses* clause and instantiate the provided `TMHash384` class:
494
495         uses
496                 {...}, MHash384;
497         
498         function ComputeHash(var inputFile: File): TByteArray;
499         var
500                 digest: TMHash384;
501                 buffer: TByteArray;
502                 count: Integer;
503         begin
504                 SetLength(buffer, 4096);
505                 digest := TMHash384.Create();
506                 try
507                         while not Eof(inputFile) do
508                         begin
509                                 BlockRead(inputFile, buffer[0], Length(buffer), count);
510                                 if count > 0 then
511                                 begin
512                                         digest.Update(buffer, 0, count);
513                                 end;
514                         end;
515                         digest.Result(Result);
516                 finally
517                         digest.Destroy();
518                 end;
519         end;
520
521 ### Prerequisites
522
523 In order to use the MHash-384 library in your Delphi application, the Unit file 'MHash384.pas' must be added to the project. This unit contains the `TMHash384` convenience class and associated data types.
524
525
526 # Source Code
527
528 The MHash-384 source is available from the official [**git**](https://git-scm.com/) mirrors:
529
530 * `git clone https://github.com/lordmulder/mhash-384.git` [[Browse](https://github.com/lordmulder/mhash-384)]
531
532 * `git clone https://bitbucket.org/muldersoft/mhash-384.git` [[Browse](https://bitbucket.org/muldersoft/mhash-384)]
533
534 * `git clone https://git.assembla.com/mhash-384.git` [[Browse](https://www.assembla.com/spaces/mhash-384/git/source)]
535
536 * `git clone https://gitlab.com/lord_mulder/mhash-384.git` [[Browse](https://gitlab.com/lord_mulder/mhash-384)]
537
538
539 # Build Instructions
540
541 This section describes how to build the MHash-384 sample application. Please note that you do **not** need to "build" the library in order to use it in your own application.
542
543 * For supported versions of *Microsoft Visual Studio*, MHash-384 library ships with project/solution files, which will compile "out of the box".
544
545 * The *Intel C/C++ Compiler* integrates into Visual Studio, so simply select "Use Intel C++" from the project/solution menu.
546
547 * Optionally, the build script `Make.cmd` may be used to create ready-to-use deployment packages. Note, however, that it may be necessary to adjust the paths in the header section of the script!
548
549 * Finally, for the *GNU/Linux* and *MinGW/MSYS2* platforms, the MHash-384 library provides a Makefile (tested with GNU Make). Just run `make` from the MHash-384 directory.
550
551 ## Influential Environment Variables
552
553 The following environment variables may effect the build process and need to be set carefully:
554
555 * **Java:**
556     - `JAVA_HOME`: The *Java* "home" directory, should be pointing to JDK (*not* JRE) root directory
557     - `ANT_HOME`: The *Apache Ant* "home" directory, should be pointing to root directory  
558
559 * **Python:**
560     - `PYTHON_INC`: Directory to look for Python *include* files (typically `<PYTHON_INSTALL_PATH>/include`)
561     - `PYTHON_LIB32`: Directory to look for 32-Bit (x86) Python *library* files (typically `<PYTHON_X86_PATH>/libs`)
562     - `PYTHON_LIB64`: Directory to look for 64-Bit (x64) Python *library* files (typically `<PYTHON_X64_PATH>/libs`)  
563
564 * **Delphi:**
565     - `DELPHI_PATH`: The *Borland Delphi* installation directory (for Windows only)  
566
567 * **Makefile:**
568     - `CPU_TYPE`: Optimize binaries for the specified CPU type (defaults to "native"), see [`-march`](https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html#x86-Options) for details!
569     - `CPLUSPLUS`:  If set to `1`, build CLI front-end from *C++* sources, otherwise from *plain C* sources (defaults to `0`)
570     - `NO_JAVA`: If set to `1`, the *Java* bindings are **not** built (defaults to `0`, i.e. *do* build)
571     - `NO_PYTHON`: If set to `1`, the *Python* bindings are **not** built (defaults to `0`, i.e. *do* build)  
572
573 * **Windows:**
574     - `MSVC_PATH`: *Microsoft Visual C++* installation directory
575     - `PDOC_PATH`: *Pandoc v2.x* installation directory
576     - `GIT2_PATH`: *Git for Windows* (formerly *MSYS Git*) installation directory  
577
578
579 # License
580
581 **Copyright(c) 2016-2018 LoRd_MuldeR &lt;mulder2@gmx.de&gt;, released under the MIT License.**  
582 **Check <http://muldersoft.com/> or <http://muldersoft.sourceforge.net/> for updates!**
583
584         Permission is hereby granted, free of charge, to any person obtaining a copy of this software
585         and associated documentation files (the "Software"), to deal in the Software without
586         restriction, including without limitation the rights to use, copy, modify, merge, publish,
587         distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
588         Software is furnished to do so, subject to the following conditions:
589
590         The above copyright notice and this permission notice shall be included in all copies or
591         substantial portions of the Software.
592
593         THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
594         BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
595         NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
596         DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
597         OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
598
599 <https://opensource.org/licenses/MIT>
600
601
602 # Version History
603
604 ## Version 1.2.0 [2018-01-20]
605
606 * Implemented the new INI-, RND- and SBX-tables for a further increased hash quality
607
608 *  ***Note:*** This change, unfortunately, breaks compatibility with v1.1 hashes!
609
610 * Many improvements to the C and C++ command-line front-ensd have been implemented
611
612 * Various improvements and fixes for the Java, Microsoft.NET, Python and Delphi ports
613
614 ## Version 1.1.0 [2017-12-22]
615
616 * Re-generated the XOR- and MIX-tables with higher hamming distance for increased hash quality
617
618 * ***Note:*** This change, unfortunately, breaks compatibility with v1.0 hashes! 
619
620 * All language bindings have been *replaced* by full ports of the library to the respective language
621
622 ## Version 1.0.1 [2016-03-31]
623
624 * Added language bindings for *Java*.
625
626 * Added language bindings for *Microsoft.NET*.
627
628 * Added language bindings for *Python*.
629
630 * Added language bindings for *Delphi*.
631
632 ## Version 1.0.0 [2016-03-03]
633
634 * First public release.
635
636 &nbsp;
637
638 [■](https://www.youtube.com/watch?v=dng06ZqI4Ss)