{"id":22678298,"url":"https://github.com/hyperf-ext/encryption","last_synced_at":"2025-04-12T14:40:48.606Z","repository":{"id":40491645,"uuid":"292254700","full_name":"hyperf-ext/encryption","owner":"hyperf-ext","description":"The Hyperf Encryption package.","archived":false,"fork":false,"pushed_at":"2023-02-15T16:19:44.000Z","size":28,"stargazers_count":8,"open_issues_count":2,"forks_count":9,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-11T22:16:26.782Z","etag":null,"topics":["encryption","hyperf","php"],"latest_commit_sha":null,"homepage":"","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/hyperf-ext.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-02T10:40:46.000Z","updated_at":"2024-12-16T06:01:22.000Z","dependencies_parsed_at":"2024-06-19T01:34:30.953Z","dependency_job_id":"289e2a97-4c78-4452-be1c-047d45696950","html_url":"https://github.com/hyperf-ext/encryption","commit_stats":{"total_commits":8,"total_committers":3,"mean_commits":"2.6666666666666665","dds":0.5,"last_synced_commit":"d20a71ca1c680da1137bb2c6646e088701070728"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf-ext%2Fencryption","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf-ext%2Fencryption/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf-ext%2Fencryption/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf-ext%2Fencryption/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hyperf-ext","download_url":"https://codeload.github.com/hyperf-ext/encryption/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248582425,"owners_count":21128372,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["encryption","hyperf","php"],"created_at":"2024-12-09T18:14:42.060Z","updated_at":"2025-04-12T14:40:48.585Z","avatar_url":"https://github.com/hyperf-ext.png","language":"PHP","readme":"# Hyperf 加密组件\n\n组件当前仅实现了 AES 加密（OpenSSL 所提供的 AES-256 和 AES-128 加密）\n\n所有组件加密之后的结果都会使用消息认证码（MAC）签名，使其底层值不能在加密后再次修改。\n\n\u003e 移植自 [illuminate/encryption](https://github.com/illuminate/encryption )。\n\n## 安装\n\n```shell script\ncomposer require hyperf-ext/encryption\n```\n\n## 发布配置\n\n```shell script\nphp bin/hyperf.php vendor:publish hyperf-ext/encryption\n```\n\n\u003e 配置文件位于 `config/autoload/encryption.php`。\n\n## 设置\n\n在使用之前，你必须先设置配置文件中的 `key` 选项。你应当使用 `php bin/hyperf.php gen:key` 命令来生成密钥，这条命令会使用 PHP 的安全随机字节生成器来构建密钥。如果这个 `key` 值没有被正确设置，则无法进行加密。\n\n## 使用\n\n### 加密\n\n你可以使用 `\\HyperfExt\\Encryption\\Crypt` 类来加密一个值。所有加密过的值都会使用消息认证码 (MAC) 来签名，以检测加密字符串是否被篡改过：\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Http\\Controller;\n\nuse Hyperf\\HttpServer\\Request;\nuse HyperfExt\\Encryption\\Crypt;\n\nclass UpdatePasswordController\n{\n    public function update(Request $request)\n    {\n        // ……\n\n        $user-\u003efill([\n            'secret' =\u003e Crypt::encrypt($request-\u003einput('secret'))\n        ])-\u003esave();\n    }\n}\n```\n\n加密的时候，对象和数组加密过的值都需要通过 serialize 序列化后传递。因此，非 PHP 客户端接收加密值，需要对数据进行 unserialize 反序列化。如果想要在不序列化的情况下加密或解密值，你可以将 `Crypt::encrypt` 方法的第二个参数设置为 `false`：\n\n```php\nuse HyperfExt\\Encryption\\Crypt;\n\n$encrypted = Crypt::encrypt('Hello world.', false);\n$decrypted = Crypt::decrypt($encrypted);\n```\n\n### 解密\n\n你可以使用 `Crypt::decrypt` 方法来进行解密。如果该值不能被正确解密，例如 MAC 无效时，会抛出异常 `HyperfExt\\Encryption\\Exception\\DecryptException`：\n\n```php\nuse HyperfExt\\Encryption\\Crypt;\nuse HyperfExt\\Encryption\\Exception\\DecryptException;\n\ntry {\n    $decrypted = Crypt::decrypt($encryptedValue);\n} catch (DecryptException $e) {\n    // \n}\n```\n\n### 使用指定驱动\n\n```php\nuse HyperfExt\\Encryption\\Crypt;\n\n$hasher = Crypt::getDriver('rsa'); // RSA 尚未实现\n$hasher-\u003eencrypt('Hello world.', false);\n```\n\n### 使用自定义加密类\n\n实现 `\\HyperfExt\\Hashing\\Encryption\\SymmetricDriverInterface` 或 `\\HyperfExt\\Hashing\\Encryption\\AsymmetricDriverInterface` 接口，并参照配置文件中的其他算法进行配置。\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhyperf-ext%2Fencryption","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhyperf-ext%2Fencryption","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhyperf-ext%2Fencryption/lists"}