MCPcopy Index your code
hub / github.com/nodejs/node / hash

Function hash

lib/internal/crypto/hash.js:291–337  ·  view source on GitHub ↗
(algorithm, input, options)

Source from the content-addressed store, hash-verified

289}
290
291function hash(algorithm, input, options) {
292 validateString(algorithm, 'algorithm');
293 if (typeof input !== 'string' && !isArrayBufferView(input)) {
294 throw new ERR_INVALID_ARG_TYPE('input', ['Buffer', 'TypedArray', 'DataView', 'string'], input);
295 }
296 let outputEncoding;
297 let outputLength;
298
299 if (typeof options === 'string') {
300 outputEncoding = options;
301 } else if (options !== undefined) {
302 validateObject(options, 'options');
303 outputLength = options.outputLength;
304 outputEncoding = options.outputEncoding;
305 }
306
307 outputEncoding ??= 'hex';
308
309 let normalized = outputEncoding;
310 // Fast case: if it's 'hex', we don't need to validate it further.
311 if (normalized !== 'hex') {
312 validateString(outputEncoding, 'outputEncoding');
313 normalized = normalizeEncoding(outputEncoding);
314 // If the encoding is invalid, normalizeEncoding() returns undefined.
315 if (normalized === undefined) {
316 // normalizeEncoding() doesn't handle 'buffer'.
317 if (StringPrototypeToLowerCase(outputEncoding) === 'buffer') {
318 normalized = 'buffer';
319 } else {
320 throw new ERR_INVALID_ARG_VALUE('outputEncoding', outputEncoding);
321 }
322 }
323 }
324
325 if (outputLength !== undefined) {
326 validateUint32(outputLength, 'outputLength');
327 // Coerce -0 to +0.
328 outputLength += 0;
329 }
330
331 if (outputLength === undefined) {
332 maybeEmitDeprecationWarning(algorithm);
333 }
334
335 return oneShotDigest(algorithm, getCachedHashId(algorithm), getHashCache(),
336 input, normalized, encodingsMap[normalized], outputLength);
337}
338
339module.exports = {
340 Hash,

Callers 4

mainFunction · 0.50
__hash__Method · 0.50
FreezeFunction · 0.50
stringrefs-exec.jsFile · 0.50

Calls 3

normalizeEncodingFunction · 0.85
getCachedHashIdFunction · 0.85
getHashCacheFunction · 0.85

Tested by

no test coverage detected