1 % ![](img/mhash/MHash-384.jpg)
3 % Simple fast portable header-only hashing library
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).
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).
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**.
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.
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.
20 #include <mhash_384.h>
22 ## Example for C language
24 If you source code is written in plain **C**, simply use the provided *global* functions:
27 const uint8_t *data_ptr;
29 uint8_t result[MHASH_384_LEN];
33 mhash_384_initialize(&context);
35 /*input data processing*/
36 while(have_more_data())
38 data_ptr = fetch_next_data_chunk(&data_len);
39 mhash_384_update(&context, data_ptr, data_len);
43 mhash_384_finalize(&context, result);
45 ## Example for C++ language
47 And, if you source code is written in **C++**, the *MHash384* class from *mhash* namespace is used:
50 std::vector<uint8_t> data;
51 uint8_t result[MHASH_384_LEN];
54 mhash_384::MHash384 instance;
56 /*input data processing*/
57 while(source.have_more_data())
59 data = source.fetch_next_data_chunk();
60 instance.update(data);
64 instance.finalize(result);
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.
73 The MHash-384 command-line application takes a number of optional options followed by an one or more input files.
75 If **no** input file is specified, input will be read from standard input (*stdin*).
77 The digest will be written to the standard output (*stdout*). Diagnostic message are written to the standard error (*stderr*).
79 mhash_384 [options] [file_1] [file_2] ... [file_n]
83 MHash-384 supports the following options:
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.
89 * **`-u`**, **`--upper`**
90 Output the final digest (hash) in *upper case* Hex letters. Default mode is *lower case*.
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.
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!
99 * **`-b`**, **`--bench`**
100 Compute performance statistics (bytes processed per second) and print them to the *stderr* at the end of the process.
102 * **`-i`**, **`--ignore`**
103 Ignore errors. Proceeds with the *next* file when an error is encountered. Default behavior stops immediately on errors.
104 *Note:* This option only has a noteworthy effect, if *multiple* input files have been given!
106 * **`-v`**, **`--version`**
107 Print library version to the *stdout* and exit program. Format is: `mhash-384 version X.Y.Z (built MMM DD YYYY)`
109 * **`-t`**, **`--test`**
110 Run *built-in self-test* and exit program. Computes hashes from test vectors and compares results to reference hashes.
112 * **`-h`**, **`--help`**
113 Print help screen (man page) and exit program.
117 On completion, the computed digest (hash value) will be written to the standard output (*stdout*).
119 The digest will be printed in lower-case hexadecimal notation, by default. Options `-u` and `-c` can influence the format.
121 If *multiple* files have been given, the hash value of each files is printed in a separate line, followed by the file name.
123 With the `-r` option specified, hash values will be written to standard output (*stdout*) as "raw" bytes – 48 bytes per hash.
127 On success, *zero* is returned. On error or user interruption, a *non-zero* error code is returned.
131 Compute MHash-384 hash of a single local file:
133 mhash_384 "C:\Images\debian-8.3.0-amd64-DVD-1.iso"
135 Compute MHash-384 hash of *two* files:
137 mhash_384 "C:\Images\debian-8.3.0-amd64-DVD-1.iso" "C:\Images\debian-8.3.0-i386-DVD-1.iso"
139 Compute MHash-384 hash of *multiple* files, using wildcard expansion ([globbing](https://en.wikipedia.org/wiki/Glob_(programming))):
141 mhash_384 "C:\Images\*.iso"
143 Compute MHash-384 hash of a file, with more verbose status outputs:
145 mhash_384 -p -b "C:\Images\debian-8.3.0-amd64-DVD-1.iso"
147 Compute MHash-384 from random bytes, passing data directly from [**`dd`**](https://en.wikipedia.org/wiki/Dd_%28Unix%29) via [pipeline](https://en.wikipedia.org/wiki/Pipeline_(Unix)):
149 dd if=/dev/urandom bs=100 count=1 | mhash_384
152 # Algoritm Description
154 This chapter describes the of the MHash-384 algorithm, which is a novel design (from the scratch), in full detail.
156 ## Informal Description
158 We start with an informal description of the MHash-384 algorithm, which also explains the design goals:
162 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).
164 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.
166 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…
168 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 – a subset that differs from *all* other possible "flip" subsets (i.e. *all* other possible input byte values) at approximately ½ of the bit positions.
170 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.
174 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.
176 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 – 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 – with very high probability.
178 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).
180 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.
184 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 – which means that results greater than or equal to 256 will wrap around to zero.
186 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 – 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/).
188 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.
190 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.
194 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 – 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.
196 The substitution operation replaces the *i*‑th byte of the original hash value with a value chosen from the *i*‑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…
198 The *non-linear* substitution operation improves the "confusion" between the input message and the resulting hash value.
202 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 – 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.
204 The *unique* finalization step ensures that it will ***not*** be possible to "continue" the computation of a known *final* MHash-384 hash value – 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.
208 The MHash-384 algorithm can be summed up with the following simple pseudocode:
213 TABLE_INI: matrix[ 2 × HASH_SIZE] of byte
214 TABLE_XOR: matrix[257 × HASH_SIZE] of byte
215 TABLE_MIX: matrix[256 × HASH_SIZE] of byte
216 TABLE_RND: matrix[256 × HASH_SIZE] of byte
217 TABLE_SBX: matrix[256 × HASH_SIZE] of byte
219 message: array[N] of byte
221 hash: array[HASH_SIZE] of byte
225 tmp_src: array[HASH_SIZE] of byte
226 tmp_dst: array[HASH_SIZE] of byte
230 tmp_src ← TABLE_INI[0]
231 tmp_dst ← TABLE_INI[1]
233 /*input message processing*/
235 for i = 0 to HASH_SIZE-1 do
236 val ← ((tmp_src[TABLE_MIX[ctr,i]] ⊕ TABLE_XOR[message[k],i]) + TABLE_RND[ctr,i]) mod 256
237 tmp_dst[i] ← tmp_dst[i] ⊕ TABLE_SBX[val,i]
239 ctr ← (ctr + 1) mod 256
240 xchg(tmp_src ⇄ tmp_dst)
244 for i = 0 to HASH_SIZE-1 do
245 val ← ((tmp_src[TABLE_MIX[ctr,i]] ⊕ TABLE_XOR[256,i]) + TABLE_RND[ctr,i]) mod 256
246 hash[i] ← tmp_dst[i] ⊕ TABLE_SBX[val,i]
250 *Note:* The `⊕` symbol refers to the binary XOR operation, whereas `+` refers to the arithmetic add operation.
253 # Detailed API Specification
255 Global definitions for both, C and C++, API's.
257 ## Global definitions
261 This constant specifies the length of a MHash-384 digest (hash value) in bytes/octets. It is equal to `48UL`.
263 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.
265 ### MHASH_384_VERSION_MAJOR
267 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.
269 ### MHASH_384_VERSION_MINOR
271 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.
273 ### MHASH_384_VERSION_PATCH
275 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.
277 ## API for for C language
279 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").
283 typedef struct mhash_384_t;
285 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!
287 *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!
289 ### mhash_384_initialize()
291 void mhash_384_initialize(mhash_384_t *const ctx);
293 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.
298 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!
300 ### mhash_384_update()
302 void mhash_384_update(mhash_384_t *const ctx, const uint8_t *const input, const size_t len);
304 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.
309 Pointer to the hash computation state of type `mhash_384_t` that will be updated by this operation.
311 * `const uint8_t *input`
312 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.
313 *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.
316 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!
318 ### mhash_384_finalize()
320 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.
322 *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.
324 void mhash_384_finalize(const mhash_384_t *const ctx, uint8_t *const output);
326 * `const mhash_384_t *ctx`
327 Pointer to the hash computation state of type `mhash_384_t` from which the final digest is computed.
330 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).
332 ## API for for C++ language
334 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").
336 *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!
340 MHash384::MHash384(void);
342 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.
346 void MHash384::update(const uint8_t *const input, const size_t len);
348 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.
352 * `const uint8_t *input`
353 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.
354 *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.
357 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!
361 void MHash384::update(const std::vector<uint8_t> &input, const size_t offset = 0, const size_t len = 0);
363 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.
367 * `const std::vector<uint8_t> &input`
368 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.
371 Optional. Specifies the *zero-based* index of the *first* vector element to be processed. By default, processing starts at index **0**.
374 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.
378 void MHash384::update(const std::string &input, const size_t offset = 0, const size_t len = 0);
380 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.
384 * `const std::vector<uint8_t> &input`
385 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.
388 Optional. Specifies the *zero-based* index of the *first* character in the string to be processed. By default, processing starts at index **0**.
391 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.
395 void MHash384::finalize(uint8_t *const output) const;
397 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.
399 *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.
404 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).
408 std::vector<uint8_t> MHash384::finalize(void) const;
410 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.
412 *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.
416 * 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).
420 void MHash384::reset(void);
422 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.
424 *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.
427 # Supported Platforms
429 MHash-384 library should compile on any standard-compliant C/C++ compiler. In particular, the following platforms have been tested successfully:
432 - Microsoft C/C++ Compiler, using Visual Studio 2010 or later
433 - MinGW, using Mingw-w64 from [MSYS2](https://msys2.github.io/) project
434 * Intel C/C++ Compiler, version Version 15.0 (XE 2015) or later
435 * GNU/Linux, using GCC/G++, version 4.7 or later
439 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.
443 Bindings of the MHash-384 library are provided for **Microsoft.NET**, in the form of the `MHashDotNet384.dll` assembly.
445 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:
447 using MHashDotNet384;
449 String ComputeHash(FileStream fs)
451 byte[] buffer = new byte[4096];
452 MHash384 digest = new MHash384();
455 int count = fs.Read(buffer, 0, buffer.Length);
458 digest.Update(buffer, 0, count);
463 return digest.ToString();
468 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.
472 Bindings of the MHash-384 library are provided for **Java**, in the form of the `MHashJava384.jar` library.
474 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:
476 import com.muldersoft.mhash384.MHash384;
478 String computeHash(final InputStream inputStream) throws IOException {
479 final byte[] buffer = new byte[4096];
480 final MHash384 mhash384 = new MHash384()
483 count = inputStream.read(buffer);
485 mhash384.update(buffer, 0, count);
488 while(count == buffer.length);
489 return mhash384.digest().toString();
494 In order to use the MHash-384 library in your Java project, the `MHashJava384.jar` **must** be added to the Java *build path*.
498 Bindings of the MHash-384 library are provided for **Python** (version 3.x), in the form of the `MHashPy384.py` module.
500 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:
502 from MHashPy384 import MHash384
504 mhash384 = MHash384()
505 for chunk in read_chunks(fs):
506 mhash384.update(chunk)
507 print(binascii.hexlify(mhash384.digest()))
511 In order to use the MHash-384 library in your Python project, the `MHashPy384.py` **must** be in your Python *library path*.
513 *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.
517 Bindings of the MHash-384 library are provided for **Delphi**, in the form of the `MHash384.pas` unit.
519 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:
524 function ComputeHash(var inputFile: File): TByteArray;
530 SetLength(buffer, 4096);
531 digest := TMHash384.Create();
533 while not Eof(inputFile) do
535 BlockRead(inputFile, buffer[0], Length(buffer), count);
538 digest.Update(buffer, 0, count);
541 digest.Result(Result);
549 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.
554 The MHash-384 source is available from the official [**git**](https://git-scm.com/) mirrors:
556 * `git clone https://github.com/lordmulder/mhash-384.git` [[Browse](https://github.com/lordmulder/mhash-384)]
558 * `git clone https://bitbucket.org/muldersoft/mhash-384.git` [[Browse](https://bitbucket.org/muldersoft/mhash-384)]
560 * `git clone https://git.assembla.com/mhash-384.git` [[Browse](https://www.assembla.com/spaces/mhash-384/git/source)]
562 * `git clone https://gitlab.com/lord_mulder/mhash-384.git` [[Browse](https://gitlab.com/lord_mulder/mhash-384)]
567 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.
569 * For supported versions of *Microsoft Visual Studio*, MHash-384 library ships with project/solution files, which will compile "out of the box".
571 * The *Intel C/C++ Compiler* integrates into Visual Studio, so simply select "Use Intel C++" from the project/solution menu.
573 * 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!
575 * 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.
577 ## Influential Environment Variables
579 The following environment variables may effect the build process and need to be set carefully:
582 - `JAVA_HOME`: The *Java* "home" directory, should be pointing to JDK (*not* JRE) root directory
583 - `ANT_HOME`: The *Apache Ant* "home" directory, should be pointing to root directory
586 - `PYTHON_INC`: Directory to look for Python *include* files (typically `<PYTHON_INSTALL_PATH>/include`)
587 - `PYTHON_LIB32`: Directory to look for 32-Bit (x86) Python *library* files (typically `<PYTHON_X86_PATH>/libs`)
588 - `PYTHON_LIB64`: Directory to look for 64-Bit (x64) Python *library* files (typically `<PYTHON_X64_PATH>/libs`)
591 - `DELPHI_PATH`: The *Borland Delphi* installation directory (for Windows only)
594 - `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!
595 - `CPLUSPLUS`: If set to `1`, build CLI front-end from *C++* sources, otherwise from *plain C* sources (defaults to `0`)
596 - `NO_JAVA`: If set to `1`, the *Java* bindings are **not** built (defaults to `0`, i.e. *do* build)
597 - `NO_PYTHON`: If set to `1`, the *Python* bindings are **not** built (defaults to `0`, i.e. *do* build)
600 - `MSVC_PATH`: *Microsoft Visual C++* installation directory
601 - `PDOC_PATH`: *Pandoc v2.x* installation directory
602 - `GIT2_PATH`: *Git for Windows* (formerly *MSYS Git*) installation directory
607 **Copyright(c) 2016-2018 LoRd_MuldeR <mulder2@gmx.de>, released under the MIT License.**
608 **Check <http://muldersoft.com/> or <http://muldersoft.sourceforge.net/> for updates!**
610 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
611 and associated documentation files (the "Software"), to deal in the Software without
612 restriction, including without limitation the rights to use, copy, modify, merge, publish,
613 distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
614 Software is furnished to do so, subject to the following conditions:
616 The above copyright notice and this permission notice shall be included in all copies or
617 substantial portions of the Software.
619 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
620 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
621 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
622 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
623 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
625 <https://opensource.org/licenses/MIT>
630 ## Version 1.2.0 [2018-01-20]
632 * Implemented the new INI-, RND- and SBX-tables for a further increased hash quality
634 * ***Note:*** This change, unfortunately, breaks compatibility with v1.1 hashes!
636 * Many improvements to the C and C++ command-line front-ensd have been implemented
638 * Various improvements and fixes for the Java, Microsoft.NET, Python and Delphi ports
640 ## Version 1.1.0 [2017-12-22]
642 * Re-generated the XOR- and MIX-tables with higher hamming distance for increased hash quality
644 * ***Note:*** This change, unfortunately, breaks compatibility with v1.0 hashes!
646 * All language bindings have been *replaced* by full ports of the library to the respective language
648 ## Version 1.0.1 [2016-03-31]
650 * Added language bindings for *Java*.
652 * Added language bindings for *Microsoft.NET*.
654 * Added language bindings for *Python*.
656 * Added language bindings for *Delphi*.
658 ## Version 1.0.0 [2016-03-03]
660 * First public release.
664 [■](https://www.youtube.com/watch?v=dng06ZqI4Ss)