Commit 17443a3af78 for nodejs

commit 17443a3af788d9901550aaf7a8ee160172501dd4
Author: Filip Skokan <panva.ip@gmail.com>
Date:   Wed Nov 18 00:00:55 2026 +0100

    crypto: align hash() invalid XOF length errors

    Use the same error as createHash() when outputLength differs from a
    fixed-size digest's length. Preserve acceptance of the exact length.

    Assisted-by: Codex
    Signed-off-by: Filip Skokan <panva.ip@gmail.com>
    PR-URL: https://github.com/nodejs/node/pull/67565
    Reviewed-By: James M Snell <jasnell@gmail.com>
    Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>

diff --git a/src/crypto/crypto_hash.cc b/src/crypto/crypto_hash.cc
index 06ad8bff8b9..fc148cf7121 100644
--- a/src/crypto/crypto_hash.cc
+++ b/src/crypto/crypto_hash.cc
@@ -463,27 +463,20 @@ void OneShotDigestWithMD(Environment* env,
   enum encoding output_enc = ParseEncoding(isolate, args[4], args[5], HEX);

   const bool is_xof = ncrypto::Digest(md).isXof();
-  OPENSSL_SIZE_T output_length = EVP_MD_size(md);
-
-  // This is to cause hash() to fail when an incorrect
-  // outputLength option was passed for a non-XOF hash function.
-  if (!is_xof && !args[6]->IsUndefined()) {
-    output_length = args[6].As<Uint32>()->Value();
-    if (output_length != EVP_MD_size(md)) {
-      Utf8Value method(isolate, args[0]);
-      std::string message =
-          "Output length " + std::to_string(output_length) + " is invalid for ";
-      message += method.ToString() + ", which does not support XOF";
-      return ThrowCryptoError(env, ERR_get_error(), message.c_str());
-    }
-  } else if (is_xof) {
-    if (!args[6]->IsUndefined()) {
-      output_length = args[6].As<Uint32>()->Value();
-    } else if (ShouldRejectMissingXofLength(md, output_length)) {
+  size_t output_length = EVP_MD_size(md);
+
+  if (!args[6]->IsUndefined()) {
+    const auto length = args[6].As<Uint32>()->Value();
+    if (!is_xof && length != output_length) [[unlikely]] {
       MarkInvalidXofLength();
       return ThrowCryptoError(
           env, ERR_get_error(), "Digest method not supported");
     }
+    output_length = length;
+  } else if (is_xof && ShouldRejectMissingXofLength(md, output_length)) {
+    MarkInvalidXofLength();
+    return ThrowCryptoError(
+        env, ERR_get_error(), "Digest method not supported");
   }

   auto return_empty_output = [&]() {
diff --git a/test/parallel/test-crypto-oneshot-hash-xof.js b/test/parallel/test-crypto-oneshot-hash-xof.js
index 46b60b22c75..79e74ca2332 100644
--- a/test/parallel/test-crypto-oneshot-hash-xof.js
+++ b/test/parallel/test-crypto-oneshot-hash-xof.js
@@ -132,9 +132,9 @@ if (isBoringSSL) {
                      'd14a028c2a3a2bc9476102bb288234c4' +
                      '15a2b01f828ea62ac5b3e42f');

-  // Non-XOF hash functions should fail when outputLength isn't their actual outputLength
+  // Non-XOF hash functions reject lengths other than their digest size.
   assert.throws(() => crypto.hash('sha224', '', { outputLength: 32 }),
-                { message: 'Output length 32 is invalid for sha224, which does not support XOF' });
+                invalidXofLength);
   assert.throws(() => crypto.hash('sha224', '', { outputLength: 0 }),
-                { message: 'Output length 0 is invalid for sha224, which does not support XOF' });
+                invalidXofLength);
 }