{"record":{"id":"5759d6971da910f1","repo":"ramsey/uuid","slug":"unable-to-hash-namespace-and-name-with-algorithm-5759d6","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/PeclUuidNameGenerator.php","lineNumber":40,"sourceCode":"use function uuid_generate_sha1;\nuse function uuid_parse;\n\n/**\n * PeclUuidNameGenerator generates strings of binary data from a namespace and a name, using ext-uuid\n *\n * @link https://pecl.php.net/package/uuid ext-uuid\n */\nclass PeclUuidNameGenerator implements NameGeneratorInterface\n{\n    /**\n     * @pure\n     */\n    public function generate(UuidInterface $ns, string $name, string $hashAlgorithm): string\n    {\n        $uuid = match ($hashAlgorithm) {\n            'md5' => uuid_generate_md5($ns->toString(), $name), /** @phpstan-ignore possiblyImpure.functionCall */\n            'sha1' => uuid_generate_sha1($ns->toString(), $name), /** @phpstan-ignore possiblyImpure.functionCall */\n            default => throw new NameException(\n                sprintf('Unable to hash namespace and name with algorithm \\'%s\\'', $hashAlgorithm),\n            ),\n        };\n\n        /** @phpstan-ignore possiblyImpure.functionCall */\n        return (string) uuid_parse($uuid);\n    }\n}\n","sourceCodeStart":22,"sourceCodeEnd":49,"githubUrl":"https://github.com/ramsey/uuid/blob/da5b521600a707d2dd097598464bd3090de850f5/src/Generator/PeclUuidNameGenerator.php#L22-L49","documentation":"PeclUuidNameGenerator is the name-based-UUID generator that ramsey/uuid wires in automatically when the PECL uuid extension (ext-uuid) is loaded. Its generate() only recognizes the case-sensitive strings 'md5' and 'sha1', mapping them to uuid_generate_md5() and uuid_generate_sha1(); any other hash algorithm falls through the match to the default arm, which throws NameException.","triggerScenarios":"Calling $generator->generate($ns, $name, $hashAlgorithm) directly (or via a custom FeatureSet/UuidFactory that uses PeclUuidNameGenerator) with anything except exactly 'md5' or 'sha1' — e.g. 'sha256', 'MD5', or an algorithm name taken from user input/config. Uuid::uuid3()/uuid5() always pass 'md5'/'sha1', so the standard factory path never triggers this.","commonSituations":"Switching to ext-uuid for performance and reusing an algorithm constant from elsewhere; case or typo mismatches in the algorithm string; attempting SHA-256 name-based UUIDs, which neither RFC 9562 nor this generator support.","solutions":["Pass only 'md5' (version 3) or 'sha1' (version 5) as the hash algorithm","Whitelist before calling: in_array($algo, ['md5', 'sha1'], true), else fail with your own error before reaching the generator","If you genuinely need other hash algorithms, use the pure-PHP DefaultNameGenerator (built on hash()) instead of the PECL-based generator"],"exampleFix":"// before\n$uuid = $featureSet->getNameGenerator()->generate($ns, $name, 'sha256');\n\n// after\n$algo = strtolower($algo);\nif (!in_array($algo, ['md5', 'sha1'], true)) {\n    throw new InvalidArgumentException('Only md5 and sha1 are supported');\n}\n$uuid = $featureSet->getNameGenerator()->generate($ns, $name, $algo);","handlingStrategy":"validation","validationCode":"$allowed = ['md5', 'sha1'];\nif (!in_array($hashAlgorithm, $allowed, true)) {\n    throw new InvalidArgumentException(\n        'Unsupported hash algorithm: ' . $hashAlgorithm . '; use ' . implode(' or ', $allowed)\n    );\n}\n$uuidString = $generator->generate($ns, $name, $hashAlgorithm);","typeGuard":"/** @param string $algo */\nfunction isSupportedNameHashAlgorithm(string $algo): bool\n{\n    return in_array($algo, ['md5', 'sha1'], true);\n}","tryCatchPattern":"try {\n    $name = $generator->generate($ns, $name, $algo);\n} catch (\\Ramsey\\Uuid\\Exception\\NameException $e) {\n    // algorithm not supported by this generator; fall back to factory uuid3/uuid5\n}","preventionTips":["Never pass user input directly as the hash algorithm; map it to a fixed constant","Canonicalize to lowercase and whitelist before any generator call","Remember uuid3=md5 and uuid5=sha1 are the only RFC 9562 name-based versions"],"tags":["php","ramsey-uuid","pecl-uuid","hash-algorithm","name-based-uuid","argument-validation"],"backgroundTag":"unsupported-hash-algorithm","analyzedSha":"da5b521600a707d2dd097598464bd3090de850f5","analyzedAt":"2026-08-21T01:35:29.252Z","schemaVersion":2},"datasetVersion":"2026-08-21T03:17:12.404Z"}