blob: 12e762b5e82ea7ea4fe2aefbdb2c319f6d76d574 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
## General-purpose cryptographic hash
> **See also**: [Libsodium's documentation on its generic hashing features](https://download.libsodium.org/doc/hashing/generic_hashing).
### crypto_generichash
General-purpose cryptographic hash (powered by BLAKE2).
**Parameters and their respective types**:
1. `{Buffer}` message
2. `{CryptographyKey|null}` key (optional)
3. `{number}` output length (optional, defaults to 32)
Returns a `Promise` that resolves to a `Buffer`.
### crypto_generichash_keygen
Returns a `CryptographyKey` object containing a key appropriate
for the `crypto_generichash` API.
### crypto_generichash_init
Initialize a BLAKE2 hash context for stream hashing.
**Parameters and their respective types**:
1. `{CryptographyKey|null}` key (optional)
2. `{number}` output length (optional, defaults to 32)
Returns a `Promise` that resolves to... well, that depends on your backend.
* sodium-native returns a `CryptoGenericHashWrap` object.
* libsodium-wrappers returns a number (a buffer's memory address)
### crypto_generichash_update
Update the BLAKE2 hash state with a block of data.
**Parameters and their respective types**:
1. `{*}` hash state (see [crypto_generichash_init()](#crypto_generichash_init))
2. `{string|Buffer}` message chunk
Returns a `Promise` that resolves to `void`. Instead, `state` is updated in-place.
### crypto_generichash_final
Obtain the final BLAKE2 hash output.
**Parameters and their respective types**:
1. `{*}` hash state (see [crypto_generichash_init()](#crypto_generichash_init))
2. `{number}` output length (optional, defaults to 32)
Returns a `Promise` that resolves to a `Buffer`.
### Example for crypto_generichash
```javascript
const { SodiumPlus } = require('sodium-plus');
let sodium;
(async function () {
if (!sodium) sodium = await SodiumPlus.auto();
let message = 'Any message can go here';
let hashed = await sodium.crypto_generichash(message);
console.log(hashed.toString('hex'));
let key = await sodium.crypto_generichash_keygen();
let hash2 = await sodium.crypto_generichash(message, key, 64);
let state = await sodium.crypto_generichash_init(key, 64);
await sodium.crypto_generichash_update(state, 'Any message ');
await sodium.crypto_generichash_update(state, 'can go here');
let hash3 = await sodium.crypto_generichash_final(state, 64);
if (!await sodium.sodium_memcmp(hash2, hash3)) {
throw new Error('Implementation is broken. You should never see this.');
}
console.log(hash2.toString('hex'));
})();
```
|