{"record":{"id":"7a74400634ab7f5e","repo":"ramsey/uuid","slug":"unable-to-hash-namespace-and-name-with-algorithm","errorCode":null,"errorMessage":"Unable to hash namespace and name with algorithm '%s'","messagePattern":"Unable to hash namespace and name with algorithm '(.+?)'","errorType":"exception","errorClass":"NameException","httpStatus":null,"severity":"error","filePath":"src/Generator/DefaultNameGenerator.php","lineNumber":36,"sourceCode":"use Ramsey\\Uuid\\UuidInterface;\nuse ValueError;\n\nuse function hash;\n\n/**\n * DefaultNameGenerator generates strings of binary data based on a namespace, name, and hashing algorithm\n */\nclass DefaultNameGenerator implements NameGeneratorInterface\n{\n    /**\n     * @pure\n     */\n    public function generate(UuidInterface $ns, string $name, string $hashAlgorithm): string\n    {\n        try {\n            return hash($hashAlgorithm, $ns->getBytes() . $name, true);\n        } catch (ValueError $e) {\n            throw new NameException(\n                message: sprintf('Unable to hash namespace and name with algorithm \\'%s\\'', $hashAlgorithm),\n                previous: $e,\n            );\n        }\n    }\n}\n","sourceCodeStart":18,"sourceCodeEnd":43,"githubUrl":"https://github.com/ramsey/uuid/blob/da5b521600a707d2dd097598464bd3090de850f5/src/Generator/DefaultNameGenerator.php#L18-L43","documentation":"DefaultNameGenerator::generate() calls PHP's hash($hashAlgorithm, $ns->getBytes() . $name, true); when the algorithm is unknown to the hash extension, PHP 8 raises ValueError, which this generator converts into a NameException naming the algorithm. It is the failure path for name-based (v3/v5-style) UUID generation with any algorithm not in hash_algos().","triggerScenarios":"A factory or custom code path that hashes names with an invalid algorithm string: typos like 'sha-1'/'SHA-1 ' instead of 'sha1', algorithms missing on the deployed PHP build (e.g. 'xxh3', 'murmur3' on older PHP), or user/config-supplied algorithm names.","commonSituations":"Config-driven hash algorithms; code developed against a newer PHP/openssl build then deployed to an older runtime; hyphenated or uppercase algorithm names; whitespace from config files.","solutions":["Whitelist before generating: in_array($algorithm, hash_algos(), true) and reject early.","For RFC 4122 v3/v5 UUIDs stick to 'md5' and 'sha1'.","Normalize names: strtolower + str_replace('-', '') + trim, then validate."],"exampleFix":"// before\n$bytes = $nameGenerator->generate($ns, $name, 'sha-256'); // NameException\n\n// after\n$algorithm = strtolower(str_replace('-', '', trim($configuredAlgorithm)));\nif (!in_array($algorithm, hash_algos(), true)) {\n    throw new InvalidArgumentException(\"unsupported hash algorithm: {$algorithm}\");\n}\n$bytes = $nameGenerator->generate($ns, $name, $algorithm); // 'sha256'","handlingStrategy":"validation","validationCode":"$algorithm = strtolower(str_replace('-', '', trim($configuredAlgorithm)));\nif (!in_array($algorithm, hash_algos(), true)) {\n    throw new InvalidArgumentException(sprintf('unsupported hash algorithm: %s', $algorithm));\n}\n$bytes = $nameGenerator->generate($ns, $name, $algorithm);","typeGuard":"function isSupportedHashAlgorithm(string $algorithm): bool\n{\n    return in_array(strtolower(trim($algorithm)), hash_algos(), true);\n}","tryCatchPattern":"try {\n    $bytes = $nameGenerator->generate($ns, $name, $algorithm);\n} catch (\\Ramsey\\Uuid\\Exception\\NameException $e) {\n    throw new InvalidConfigurationException(\"hash algorithm '{$algorithm}' unavailable on this PHP build\", $e);\n}","preventionTips":["Validate algorithm names against hash_algos() before generating.","Use 'md5' or 'sha1' for RFC-compliant v3/v5 UUIDs.","Normalize config-supplied names (lowercase, no hyphens, trimmed).","Verify hash_algos() parity between dev and production PHP builds when using newer algorithms."],"tags":["php","ramsey-uuid","name-generator","hash-algorithm","uuid-v3","uuid-v5"],"backgroundTag":"unsupported-hash-algorithm","analyzedSha":"da5b521600a707d2dd097598464bd3090de850f5","analyzedAt":"2026-08-21T01:35:29.252Z","schemaVersion":2},"datasetVersion":"2026-08-21T03:17:12.404Z"}