-
Notifications
You must be signed in to change notification settings - Fork 2
/
hash.js
85 lines (77 loc) · 2.76 KB
/
hash.js
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
83
84
85
const crypto = require('crypto');
/**
* The hash module contains a small set of utilities for computing hashes.
* @module easy-crypto/hash
*/
/**
* Hash a given message using the specified hash algorithm.
* @since 0.1.0
* @function hash
* @param {string} algorithm The hashing algorithm to be used.
* @param {Data} message The message to be hashed.
* @param {string} inputEncoding The encoding of the `message`. If nothing is provided and `message` is a string, an encoding of `'utf8'` is enforced. If `message` is a Buffer, TypedArray, or DataView, then inputEncoding is ignored.
* @param {string} outputEncoding The encoding of the output. If encoding is provided a string will be returned; otherwise a Buffer is returned.
* @returns {string|Buffer} The hash of the input message.
* @static
*/
function hash(algorithm, message, inputEncoding, outputEncoding) {
const func = crypto.createHash(algorithm);
func.update(message, inputEncoding);
return func.digest(outputEncoding);
}
/**
* Hash a message encapsulated inside a stream using the specified hash algorithm.
* @since 0.1.0
* @async
* @function hashStream
* @param {string} algorithm The hashing algorithm to be used.
* @param {ReadableStream<Data>} input The stream containing the message to be hashed.
* @param {string} inputEncoding The encoding of the `message`. If nothing is provided and `message` is a string, an encoding of `'utf8'` is enforced. If `message` is a Buffer, TypedArray, or DataView, then inputEncoding is ignored.
* @param {string} outputEncoding The encoding of the output. If encoding is provided a string will be returned; otherwise a Buffer is returned.
* @returns {Promise<string|Buffer>} The hash of the input message.
* @static
*/
function hashStream(algorithm, input, inputEncoding, outputEncoding) {
return new Promise((resolve, reject) => {
let data;
let wasBuffer;
const dataHandler = chunk => {
const isBuffer = Buffer.isBuffer(chunk);
if ((!isBuffer && wasBuffer) || (isBuffer && wasBuffer === false)) {
reject(new Error('Inconsisent data.'));
input.removeListener('data', dataHandler);
return;
}
if (isBuffer) {
wasBuffer = true;
chunk = chunk.toString('utf8');
} else {
wasBuffer = false;
}
if (data === undefined) {
data = chunk;
} else {
data += chunk;
}
};
input.on('data', dataHandler);
input.on('end', () => {
if (data === undefined) {
reject(new Error('No data to hash.'));
} else {
resolve(
hash(
algorithm,
data,
wasBuffer ? 'utf8' : inputEncoding,
outputEncoding
)
);
}
});
});
}
module.exports = {
hash,
hashStream
};