{"id":49351700,"url":"https://github.com/channor/opaque-route-key","last_synced_at":"2026-04-27T10:30:26.688Z","repository":{"id":349745770,"uuid":"1176100779","full_name":"channor/opaque-route-key","owner":"channor","description":"Deterministic hashed route keys for integer IDs    with keyed integrity checks.","archived":false,"fork":false,"pushed_at":"2026-04-07T10:18:10.000Z","size":53,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-07T12:10:35.193Z","etag":null,"topics":["hashids","laravel","obfuscation","php","route-key"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/channor/hashed-route-key","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/channor.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-03-08T16:09:36.000Z","updated_at":"2026-04-07T10:17:40.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/channor/opaque-route-key","commit_stats":null,"previous_names":["channor/opaque-route-key"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/channor/opaque-route-key","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/channor%2Fopaque-route-key","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/channor%2Fopaque-route-key/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/channor%2Fopaque-route-key/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/channor%2Fopaque-route-key/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/channor","download_url":"https://codeload.github.com/channor/opaque-route-key/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/channor%2Fopaque-route-key/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32333192,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-26T23:26:28.701Z","status":"online","status_checked_at":"2026-04-27T02:00:06.769Z","response_time":128,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hashids","laravel","obfuscation","php","route-key"],"created_at":"2026-04-27T10:30:25.521Z","updated_at":"2026-04-27T10:30:26.661Z","avatar_url":"https://github.com/channor.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Opaque Route Key\n\nDeterministic, model-level opaque route keys for Laravel.\n\nReplaces sequential integer IDs in URLs with compact, HMAC-verified route keys without storing\nanything extra in the database:\n\n```text\n/teams/3             -\u003e /teams/kX9mG7\n/teams/3/members/42  -\u003e /teams/kX9mG7/members/bR4nYp2w\n```\n\nThe result is deterministic and decodable. It is obfuscation with integrity checks, **not encryption,\nnot secrecy, and not authorization**.\n\n\u003e **Note:** This package is not a security boundary and does not replace authentication,\n\u003e authorization, or signed URLs.\n\nThe Eloquent trait keeps route-key generation and route binding on the model itself, while deriving\ndistinct salts per model so the same integer ID produces different route keys across models by\ndefault.\n\n## Installation\n\n```bash\ncomposer require channor/opaque-route-key\n```\n\nSupported targets:\n\n- PHP 8.2, 8.3, and 8.4\n- Laravel 11 and 12\n\nLaravel package discovery registers the service provider automatically. If your application disables\npackage discovery, register it manually in `bootstrap/providers.php`:\n\n```php\nreturn [\n    App\\Providers\\AppServiceProvider::class,\n    Channor\\OpaqueRouteKey\\OpaqueRouteKeyServiceProvider::class,\n];\n```\n\nPublish config when you need to customize behavior:\n\n```bash\nphp artisan vendor:publish --tag=opaque-route-key-config\n```\n\n## Quick Start\n\nAdd the trait to an Eloquent model with an integer primary key:\n\n```php\nuse Channor\\OpaqueRouteKey\\UsesOpaqueRouteKey;\n\nclass Team extends Model\n{\n    use UsesOpaqueRouteKey;\n}\n```\n\nThat is enough for route model binding and `route()` URL generation to use the opaque key.\nSerialization includes the computed `route_key` by default.\n\n## Upgrade To V2\n\nVersion `2.0.0` removes the deprecated `hashed-route-key` compatibility layer kept during `v1.x`.\nSee [the v2 upgrade guide](docs/upgrade-v2.md) for package, import, and config migration steps.\n\n## Configuration\n\n```php\nreturn [\n    'salt' =\u003e env('OPAQUE_ROUTE_KEY_SALT', env('APP_KEY')),\n    'append_route_key' =\u003e true,\n    'default_attribute_name' =\u003e 'route_key',\n    'min_payload_length' =\u003e 3,\n    'check_length' =\u003e 4,\n    'offset_multiplier' =\u003e 1,\n    'reserved_words' =\u003e [\n        // 'admin',\n        // 'root',\n        // 'create',\n        // 'edit',\n        // 'new',\n        // 'settings',\n        // 'search',\n    ],\n    'reserved_words_case_sensitive' =\u003e true,\n    'auto_reserve_model_names' =\u003e false,\n    'reserved_word_max_attempts' =\u003e 10,\n];\n```\n\n| Key | Purpose | Default |\n|-----|---------|---------|\n| `salt` | Base salt for all route-key derivations | `APP_KEY` |\n| `append_route_key` | Auto-append `route_key` during serialization | `true` |\n| `default_attribute_name` | Attribute name when appending | `route_key` |\n| `min_payload_length` | Minimum encoded payload characters | `3` |\n| `check_length` | HMAC check tag characters (max 32) | `4` |\n| `offset_multiplier` | Shifts encoding space for low IDs | `1` |\n| `reserved_words` | Generated keys to avoid emitting | `[]` |\n| `reserved_words_case_sensitive` | Match manual reserved words by exact case only | `true` |\n| `auto_reserve_model_names` | Reserve each model's lowercase singular and plural basename | `false` |\n| `reserved_word_max_attempts` | Maximum candidate encodings, including the original | `10` |\n\nOnce URLs are public, keep the effective salt and per-model strategy settings stable. Changing salt,\nsalt suffix, payload length, check length, offset multiplier, reserved words, or reserved-word\nattempt settings can invalidate existing URLs or change future generated URLs for affected IDs.\n\n## Reserved Words\n\n`reserved_words` prevents the codec from emitting route keys that collide with route paths such as\n`create`, `edit`, or `new`. The default list is empty because reserved route words are\napplication-specific. With the default encoding settings, generated keys are at least 7 characters\nlong, so shorter words cannot be emitted.\n\nManual reserved words are case-sensitive by default. Reserving `account` will not reserve `aCcOuNt`\nunless `reserved_words_case_sensitive` is `false`.\n\nWhen `auto_reserve_model_names` is `true`, each model using the trait reserves its lowercase singular\nand plural class basename, for example `account` and `accounts` for `Account`. These model-name\nreservations are always matched case-insensitively.\n\nIf a generated key is reserved, the codec retries deterministic alternate encodings up to\n`reserved_word_max_attempts`. If every attempt collides, encoding throws a `RuntimeException`.\nExisting keys remain decodable even if you later reserve a word that was previously emitted.\n\n## Customization\n\nOverride strategy methods on a model when the defaults do not fit:\n\n```php\nclass Account extends Model\n{\n    use UsesOpaqueRouteKey;\n\n    protected function routeKeyMinPayloadLength(): int\n    {\n        return 2;\n    }\n\n    protected function routeKeyCheckLength(): int\n    {\n        return 3;\n    }\n\n    protected function routeKeyOffsetMultiplier(): int\n    {\n        return 2;\n    }\n\n    protected function routeKeySaltSuffix(): string\n    {\n        return 'account';\n    }\n}\n```\n\nBy default, the codec salt is:\n\n```php\nconfig('opaque-route-key.salt').':'.routeKeySaltSuffix()\n```\n\n`routeKeySaltSuffix()` defaults to `snake_case(class_basename(Model::class))`. Keep custom suffixes\nstable once URLs are public.\n\nTo customize the serialized attribute:\n\n```php\nprotected bool|string $appendRouteKey = false;\n```\n\n```php\nprotected bool|string $appendRouteKey = 'opaque_key';\n```\n\n```php\npublic function appendRouteKey(): bool|string\n{\n    return $this-\u003eis_public ? 'route_key' : false;\n}\n```\n\n## Contract Tests\n\nGenerate app-level stability tests before changing route-key settings:\n\n```bash\nphp artisan route-key:generate-test --class=User\nphp artisan route-key:generate-test --all\nphp artisan route-key:generate-test --reserved\nphp artisan route-key:generate-test --all --reserved\n```\n\nUseful options:\n\n- `--force` overwrites existing generated tests\n- `--path=tests/Feature/RouteKeys` changes the output directory\n- `--namespace=App\\\\Domain\\\\People\\\\Models` changes the `--all` discovery namespace\n- `--model-path=src/Domain/People/Models` sets discovery for non-`App\\\\...` namespaces\n\nGenerated model tests pin strategy values and fixed route-key outputs. The reserved config test pins\nreserved-word settings.\n\n## Decode Failures\n\nWhen an opaque key is invalid, tampered, wrong-model, or malformed:\n\n- `OpaqueRouteKeyCodec::decode()` returns `null`\n- route model binding queries `WHERE id = -1`, which matches nothing and results in a `404`\n- `$model-\u003eroute_key` returns `null` on unsaved models\n\n## Advanced Usage\n\nIf you need the lower-level codec directly, use the same model-specific salt strategy as the trait:\n\n```php\nuse Channor\\OpaqueRouteKey\\OpaqueRouteKeyCodec;\n\n$codec = new OpaqueRouteKeyCodec(\n    salt: config('opaque-route-key.salt').':team',\n    minPayloadLength: config('opaque-route-key.min_payload_length'),\n    checkLength: config('opaque-route-key.check_length'),\n    offsetMultiplier: config('opaque-route-key.offset_multiplier'),\n);\n\n$routeKey = $codec-\u003eencode(42);\n$id = $codec-\u003edecode($routeKey); // 42\n```\n\nCapacity with the default base-62 alphabet:\n\n| Payload length | Unique values |\n|----------------|---------------|\n| `p=2` | 3,844 |\n| `p=3` | 238,328 |\n| `p=4` | 14,776,336 |\n\nThe payload starts at `min_payload_length` characters and adds characters as IDs grow beyond what\nthe current length can represent. `offset_multiplier` shifts where these length boundaries fall.\n\n## Alternatives\n\n[`vinkla/laravel-hashids`](https://github.com/vinkla/laravel-hashids) and\n[`cybercog/laravel-optimus`](https://github.com/cybercog/laravel-optimus) are relevant alternatives\nfor Laravel applications that want obfuscated identifiers. Either may be a better fit depending on\nyour needs and existing conventions.\n\n## Notes\n\n- Only non-negative integer IDs are supported.\n\n## Development Note\n\nThis package was developed with LLM assistance. Final design, review, and release decisions remain\nwith the maintainer.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchannor%2Fopaque-route-key","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchannor%2Fopaque-route-key","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchannor%2Fopaque-route-key/lists"}