{"id":18750636,"url":"https://github.com/webiny/crypt","last_synced_at":"2025-04-12T23:32:20.129Z","repository":{"id":20225298,"uuid":"23497096","full_name":"webiny/Crypt","owner":"webiny","description":"[READ-ONLY] The `Crypt` component provides PHP methods for generating random numbers and strings, also, password hashing and password hash verification and methods for encryption and decryption of strings. (master at Webiny/Framework)","archived":false,"fork":false,"pushed_at":"2017-11-26T21:24:31.000Z","size":51,"stargazers_count":2,"open_issues_count":0,"forks_count":4,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-10T12:42:28.943Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://www.webiny.com/","language":"PHP","has_issues":false,"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/webiny.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":null,"support":null}},"created_at":"2014-08-30T17:36:33.000Z","updated_at":"2024-09-05T05:57:52.000Z","dependencies_parsed_at":"2022-06-28T19:02:49.763Z","dependency_job_id":null,"html_url":"https://github.com/webiny/Crypt","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FCrypt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FCrypt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FCrypt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FCrypt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webiny","download_url":"https://codeload.github.com/webiny/Crypt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248647257,"owners_count":21139081,"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":[],"created_at":"2024-11-07T17:12:39.758Z","updated_at":"2025-04-12T23:32:15.268Z","avatar_url":"https://github.com/webiny.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Crypt Component\n===============\nThe `Crypt` component provides methods for generating random numbers and strings, also, password hashing and password\nhash verification and methods for encryption and decryption of strings. Internally it uses cryptographically secure methods.\n\n\n**Disclaimer:**\nThe library was not reviewed by a security expert. \n\n\nInstall the component\n---------------------\nThe best way to install the component is using Composer. This library requires that you also add a repository to your\ncomposer.json file.\n\n```bash\ncomposer require webiny/crypt\n```\nFor additional versions of the package, visit the [Packagist page](https://packagist.org/packages/webiny/crypt).\n\n## Using Crypt\n\n```php\nclass MyClass\n{\n    use Webiny\\Component\\Crypt\\CryptTrait;\n\n    function myMethod()\n    {\n        $this-\u003ecrypt()-\u003eencrypt('to encrypt', 'secret key');\n    }\n}\n```\n\n## Generate random integers\n\nTo generate a random integer you just have to pass the range to the `Crypt` instance:\n\n```php\n    $randomInt = $crypt-\u003egenerateRandomInt(10, 20); // e.g. 15\n```\n\n## Generate random strings\n\nWhen you want to generate random string, you have several options. You can call the general `generateRandomString` method,\nor you can call `generateUserReadableString` method to get a more user-readable string that doesn't contain any special\ncharacters. There is also a method called `generateHardReadableString` that, among letters and numbers, uses special\ncharacters to make the string more \"harder\".\nHere are a few examples:\n\n```php\n    // generate a string from a defined set of characters\n    $randomString = $crypt-\u003egenerateRandomString(5, 'abc'); // e.g. cabcc\n\n    // generate a string that contains only letters (lower \u0026 upper case and numbers)\n    $randomString = $crypt-\u003egenerateUserReadableString(5); // A12uL\n\n    // generate a string that can contain special characters\n    $randomString = $crypt-\u003egenerateHardReadableString(5); // \u0026\"!3g\n```\n\n## Password hashing and validation\n\n\n```php\n    // hash password\n    $passwordHash = $crypt-\u003ecreatePasswordHash('login123'); // $2y$08$GgGha6bh53ofEPnBawShwO5FA3Q8ImvPXjJzh662/OAWkjeejAJKa\n\n    // (on login page) verify the hash with the correct password\n    $passwordsMatch = $crypt-\u003everifyPasswordHash('login123', $passwordHash); // true or false\n```\n\n## Encrypting and decrypting strings\n\n\n```php\n    // encrypt it\n    $encrypted = $crypt-\u003eencrypt('some data', 'abcdefgh12345678');\n\n    // decrypt it\n    $decrypted = $crypt-\u003edecrypt($result, 'abcdefgh12345678'); // \"some data\"\n```\n\n## Crypt config\n\nThere are three different internal crypt libraries that you can choose from:\n 1. **OpenSSL** - this is the default library\n 2. **Sodium** - library that utilizes [paragonie/halite](https://github.com/paragonie/halite) internally for password hashing, password verification, encryption and decryption. Please note that this library is highly CPU intensive.\n 3. **Mcrypt** - this is the **depricated** library which will be removed once we hit PHP v7.2\n\nTo switch between libraries, just set a different `Bridge` in your configuration:\n```yaml\nCrypt:\n    Bridge: \\Webiny\\Component\\Crypt\\Bridge\\Sodium\\Crypt\n```\n\nand then in your code just call:\n```php\n\\Webiny\\Components\\Crypt\\Crypt::setConfig($pathToYourYaml);\n```\n\n## Custom `Crypt` driver\n\nTo create a custom `Crypt` driver, first you need to create a class that implements `\\Webiny\\Component\\Crypt\\Bridge\\CryptInterface`.\nOnce you have implemented all the requested methods, you now need to change the `Bridge` path\ninside your component configuration.\n\nResources\n---------\n\nTo run unit tests, you need to use the following command:\n\n    $ cd path/to/Webiny/Component/Crypt/\n    $ composer.phar install\n    $ phpunit\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebiny%2Fcrypt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebiny%2Fcrypt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebiny%2Fcrypt/lists"}