(algorithm, input, options)
| 289 | } |
| 290 | |
| 291 | function 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 | |
| 339 | module.exports = { |
| 340 | Hash, |
no test coverage detected