Added JavaScript port of the NuHash algorithm.
[nuhash.git] / README.md
blob101a37e04bfeb74a35acaff159358639c3155e0b
1 ![](etc/images/nuhash_logo.png)
4 Introduction
5 ============
7 **NuHash** is a fast, portable and secure hashing library, released under the CC0 license.
9 The "core" library (*libnuhash*) is written in pure **C99**, but a high-level wrapper for **C++** is also provided.
11 Supported platforms include Linux, Windows, *BSD, Illumos, Haiku OS, GNU Hurd and MacOS X.
14 Getting Started
15 ===============
17 The following example shows how to use the *libnuhash* “stream” API in C99 code:
19 ```c
20 #include <nuhash.h>
21 #include <stdio.h>
23 int main(int argc, char *argv[])
25     /* state variables */
26     nuhash_t context;
27     uint8_t buffer[BUFSIZ], digest[NUHASH_BYTES];
28     char hexstr[NUHASH_CHARS];
30     /* initialization */
31     nuhash_init(&context);
33     /* input data processing */
34     while(more_data())
35     {
36         const size_t len = read_data(buffer, BUFSIZ);
37         nuhash_update(&context, buffer, len);
38     }
40     /* finalization */
41     nuhash_final(&context, digest);
43     /* print the result */
44     puts(nuhash_tohex(digest, 0, hexstr));
45
46 ```
48 If all input data is available at once, the *libnuhash* “simple” API can be used instead:
50 ```c
51 #include <nuhash.h>
52 #include <stdio.h>
54 int main(int argc, char *argv[])
56     /* state variables */
57     uint8_t digest[NUHASH_BYTES];
58     char hexstr[NUHASH_CHARS];
60     /* compute hash */
61     nuhash_compute(get_data(), length(), digest);
63     /* print the result */
64     puts(nuhash_tohex(digest, 0, hexstr));
65
66 ```
69 Command-line Usage
70 ==================
72 The **`nuhash`** command-line tool provides an interface similar to [`sha256sum`](https://linux.die.net/man/1/sha256sum) and friends:
74 ```
75 Usage:
76   nuhash.exe [options] [<file_1> [... <file_n>]]
78 Options:
79   -b --binary     Output digest as binary, default is hex-string.
80   -h --help       Print the help screen and exit.
81   -i --ignore     Ignore interrupt signals.
82   -l --line-mode  Process the input file(s) as text, line by line.
83   -p --progress   Print progress, while computing the hash.
84   -s --stop       Stop on all errors, default is to keep going.
85   -t --self-test  Run the built-in self-test.
86   -u --uppercase  Print digest as uppercase, default is lowercase.
87   -v --version    Print version information and exit.
88 ```
90 ### Example output:
92 ```
93 abe9d2f9f55d62951540397875f1104d0374a7ce3ca9e21cfde6efd638b75765885f186477669b79d78d13b6d1abe0a2 foo
94 06573eaba880f77db3a9e6a085e3a4f8c1bf6f2997d6789bc38439d1f581e0907b21200aff43df37de27d241dc64d19e bar
95 e26b0338b7a3d662397cbc1ff9f81ac8885ee71f861f5897fc3a7b113ea314994f749caf7618a4bfc1ac90727ef64f94 baz
96 d113572666421a1e2cf6e17481f1f94b2b006e8c48504fea03e4b24d04930db4302368dc0e7bf44b104f7118bdd62f2d qux
97 ```
100 Application Programming Interface (API)
101 =======================================
103 C99 API
104 -------
106 ### Constants
108 The C99 API defines the following constants:
110 * **`NUHASH_WORDS`:** The size of the “raw” hash value (digest), in [`uint64_t`](https://cplusplus.com/reference/cstdint/) words.
112 * **`NUHASH_BYTES`:** The size of the “raw” hash value (digest), in bytes.
114 * **`NUHASH_CHARS`:** The size of the hash value (digest) encoded as a null-terminated “hex” string, in characters. This is twice the size of a “raw” hash value *plus* an additional slot for the terminating `\0` character.
116 ### Simple
118 The ***simple*** API can be used to compute a hash value in an *“all at once”* fashion.
120 Use this API, if *all* input data is available at once. It can compute the hash value with as *single* function call.
122 #### `nuhash_compute()`
124 Compute the hash value from the given input data and return the resulting hash value (digest).
126 * Syntax:
127   ```c
128   uint8_t *nuhash_compute(const uint8_t *const src, const size_t len, uint8_t *const out);
129   ```
131 * Parameters:
132   - **`src`:** Pointer to the source array containing all input data to be processed (sequence of bytes).
133   - **`len`:** The length of the input data, in bytes.
134   - **`out`:** Pointer to the destination array where the hash value is stored, **must** be `NUHASH_BYTES` in size.
136 * Return value:
137   - This function returns the given `out` pointer.
139 #### `nuhash_compute_with_key()`
141 Compute the hash value from the given input data and a user-defined key; returns the hash value (digest).
143 * Syntax:
144   ```c
145   uint8_t *nuhash_compute_with_key(const uint8_t *const key, const size_t key_len,
146                       const uint8_t *const src, const size_t in_len, uint8_t *const out);
147   ```
149 * Parameters:
150   - **`key`:** Pointer to the source array containing a user-defined key (sequence of bytes).
151   - **`key_len`:** The length of the user-defined key, in bytes.
152   - **`src`:** Pointer to the source array containing all input data to be processed (sequence of bytes).
153   - **`in_len`:** The length of the input data, in bytes.
154   - **`out`:** Pointer to the destination array where the hash value is stored, **must** be `NUHASH_BYTES` in size.
156 * Return value:
157   - This function returns the given `out` pointer.
159 ### Stream API
161 The ***stream*** API can be used to compute a hash value in a *“step by step”* fashion.
163 Use this API, if **not** all input data is available at once. The input data can be processed in chunks of arbitrary size.
165 #### `nuhash_t`
167 An object containing the state of an ongoing hash computation.
169 The application **shall** treat this type as an *opaque* data structure; it is **not** meant to be accessed directly!
171 #### `nuhash_init()`
173 Initialize or reset the state for a new hash computation.
175 This function is supposed to be called exactly *once* at the beginning of a new hash computation!
177 * Syntax:
178   ```c
179   void nuhash_init(nuhash_t *const ctx);
180   ```
182 * Parameters:
183   - **`ctx`:** Pointer to the `nuhash_t` instance to be initialized.
185 #### `nuhash_init_with_key()`
187 Initialize or reset the state for a new hash computation with a user-defined key.
189 This function is supposed to be called exactly *once* at the beginning of a new hash computation!
191 * Syntax:
192   ```c
193   void nuhash_init_with_key(nuhash_t *const ctx, const uint8_t *const key, const size_t len);
194   ```
196 * Parameters:
197   - **`ctx`:** Pointer to the `nuhash_t` instance to be initialized.
198   - **`key`:** Pointer to the source array containing the user-defined key (sequence of bytes).
199   - **`len`:** The length of the user-defined key, in bytes.
201 #### `nuhash_update()`
203 Process the next chunk of input data and update the state.
205 This function is supposed to be called *repeatedly* until all the input data has been processed!
207 * Syntax:
208   ```c
209   void nuhash_update(nuhash_t *const ctx, const uint8_t *const src, const size_t len);
210   ```
212 * Parameters:
213   - **`ctx`:** Pointer to the `nuhash_t` instance to be updated.
214   - **`src`:** Pointer to the source array containing the next chunk of input data (sequence of bytes).
215   - **`len`:** The length of the input data, in bytes.
217 #### `nuhash_final()`
219 Finish the hash computation and return the resulting hash value (digest).
221 This function is supposed to be called exactly *once* after all the input data has been processed! After calling this function, the `nuhash_t` instance is **invalidated**; it must be [*reset*](#nuhash_init) in order to start a new hash computation.
223 * Syntax:
224   ```c
225   uint8_t *nuhash_final(nuhash_t *const ctx, uint8_t *const out);
226   ```
228 * Parameters:
229   - **`ctx`:** Pointer to the `nuhash_t` instance to be updated.
230   - **`out`:** Pointer to the destination array where the hash value is stored, **must** be `NUHASH_BYTES` in size.
232 * Return value:
233   - This function returns the given `out` pointer.
235 ### Utilities
237 The C99 API also provides a few additional utility functions that may be helpful.
239 #### `nuhash_tohex()`
241 Converts a “raw” hash value (digest) into a null-terminated “hex” string.
243 * Syntax:
244   ```c
245   char *nuhash_tohex(const uint8_t *const hash, const int upper, char *const out);
246   ```
248 * Parameters:
249   - **`hash`:** Pointer to the source array containing the “raw” hash value, **must** be `NUHASH_BYTES` in size.
250   - **`upper`:** If non-zero, generate upper-case characters; otherwise generate lower-case characters.
251   - **`out`:** Pointer to the destination array where the “hex” string is stored, **must** be `NUHASH_CHARS` in size.
253 * Return value:
254   - This function returns the given `out` pointer.
256 #### `nuhash_version()`
258 Returns the version number and the build date of the current *libnuhash* library.
260 * Syntax:
261   ```c
262   char *nuhash_version(uint16_t version[3U], char *const build);
263   ```
265 * Parameters:
266   - **`version`:** Pointer to the destination array where the *major*, *minor* and *patch* version are stored.
267   - **`build`:** Pointer to the destination array where the build date is stored, **must** be 12 characters in size.
269 * Return value:
270   - This function returns the given `build` pointer.
272 ### Thread-safety
274 All functions provided by ***libnuhash*** are *thread-safe* and *reentrant*, in the sense that there is **no** implicitly shared “global” state; the functions operate strictly on the given input parameters. Specifically, the functions of the “stream” API maintain **all** state of the hash computation within the given `nuhash_t` instance. This means that these functions are *thread-safe* and *reentrant* as long as each “concurrent” invocation uses its own separate `nuhash_t` instance; **no** synchronization is required in that case. However, *libnuhash* makes **no** attempt to coordinate the access to the *same* `nuhash_t` instance between concurrent threads. If the *same* `nuhash_t` instance needs to be accessed by *multiple* concurrent threads (which generally is **not** advised), then the application is responsible for *serializing* all access to that “shared” instance, e.g. by using a [*mutex*](https://pubs.opengroup.org/onlinepubs/009604499/functions/pthread_mutex_lock.html) lock! Reentrancy can **not** be achieved with a single “shared” `nuhash_t`.
278 Source Code
279 ===========
281 The source code is available from these [Git](https://git-scm.com/) mirrors:
283 * `git clone https://github.com/lordmulder/NuHash.git` ([Browse](https://github.com/lordmulder/NuHash))
284 * `git clone https://bitbucket.org/muldersoft/nuhash.git` ([Browse](https://bitbucket.org/muldersoft/nuhash/))
285 * `git clone https://gitlab.com/lord_mulder/nuhash.git` ([Browse](https://gitlab.com/lord_mulder/nuhash))
286 * `git clone https://repo.or.cz/nuhash.git` ([Browse](https://repo.or.cz/nuhash.git))
287 * `git clone https://punkindrublic.mooo.com:3000/Muldersoft/NuHash.git` ([Browse](https://punkindrublic.mooo.com:3000/Muldersoft/NuHash))
290 License
291 =======
293 This work has been released under the **CC0 1.0 Universal** license.
295 For details, please refer to:  
296 <https://creativecommons.org/publicdomain/zero/1.0/legalcode>
298 Acknowledgment
299 --------------
301 Licenses for third-party components used by NuHash:
303 * [**Hash icons**](https://www.flaticon.com/free-icons/hash)  
304   Created by Vectors Market &ndash; Flaticon. Free for personal and commercial use with attribution.
306 &marker;