{"id":22092115,"url":"https://github.com/krakphp/crypto","last_synced_at":"2025-03-23T23:44:54.392Z","repository":{"id":57009019,"uuid":"57081186","full_name":"krakphp/crypto","owner":"krakphp","description":"Cryptographic Library","archived":false,"fork":false,"pushed_at":"2017-04-20T00:25:25.000Z","size":30,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-02T03:58:23.147Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/krakphp.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}},"created_at":"2016-04-25T22:43:42.000Z","updated_at":"2016-04-25T22:44:07.000Z","dependencies_parsed_at":"2022-08-21T14:50:50.034Z","dependency_job_id":null,"html_url":"https://github.com/krakphp/crypto","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fcrypto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fcrypto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fcrypto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fcrypto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/krakphp","download_url":"https://codeload.github.com/krakphp/crypto/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245186925,"owners_count":20574554,"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-12-01T03:08:22.214Z","updated_at":"2025-03-23T23:44:54.373Z","avatar_url":"https://github.com/krakphp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Crypto\n\nA well designed cryptographic library for php.\n\n## Install\n\n```\ncomposer require krak/crypto\n```\n\n## Design\n\nThe Crypto Library has two main interfaces: `Crypt` and `Pad`.\n\nA Crypt is what does the encryption and decryption.\n\nA Pad is what does the padding and stripping.\n\n## Usage\n\n```php\n\u003c?php\n\nuse Krak\\Crypto;\n\n$key = random_bytes(16);\n$hmac_key = random_bytes(16);\n\n$crypt = new Crypto\\OpenSSLCrypt($key);\n$crypt = new Crypto\\Base64Crypt(new Crypto\\HmacCrypt($crypt, $hmac_key));\n\n$encrypted = $crypt-\u003eencrypt('data');\necho $crypt-\u003edecrypt($encrypted);\n// outputs: data\n```\n\nAll Crypts implement the interface `Krak\\Crypto\\Crypt`\n\nYou can also use any of the `Krak\\Crypto\\Pad` classes\n\n```php\n\u003c?php\n\nuse Krak\\Crypto;\n\n$pad = new Crypto\\Pkcs7Pad();\n$padded = $pad-\u003epad('abc');\necho $pad-\u003estrip($padded);\n// outputs: abc\n```\n\n### Crypt\n\nThe Crypt libraries are responsible for encrypting the data. There are crypt implementations that do encryption and others that are just decorators.\n\n**McryptCrypt** and **OpenSSLCrypt** handle encryption. Each crypt uses the `Krak\\Crypto\\pack_payload` method to prepend the iv to the cipher text.\n\n**Note:** Please be knowledgeable of the keys you pass in. The key size depends on the algorithm and typically ranges from 8, 16, 24, or 32 bytes.\n\n**Base64Crypt**, **HmacCrypt**, and are decorators for providing base64 encoding and hmac signing/authentication for your messages.\n\n**GnuPGCliCrypt** handles encrypting via the `gpg` cli utility.\n\n```php\n\u003c?php\n\n$crypt = new Krak\\Crypt\\GnuPGCliCrypt('User Name', $passphrase = 'secret', $gpg_executable_path = 'gpg');\n```\n\nIt will encrypt/decrypt data with the public and private keys for the given `$username`. **Important:** you need to make sure the keys are properly imported into your gpg cli tool. We use the `--always-trust` flag for encrypting, so make sure the keys you add are properly trusted.\n\nThis crypt also requires the `symfony/process` component to be installed.\n\n**NullCrypt** is used more for testing or mocking. It just returns the data passed to it.\n\n### Pad Types\n\n- **Krak\\Crypto\\Pkcs7Pad** - pads via the pkcs7 algorithm\n- **Krak\\Crypto\\NullBytePad** - pads by appending null bytes.\n- **Krak\\Crypto\\NoPad** - doesn't apply any padding, just returns the string as is.\n\n### Iv Gen\n\nThe crypts take in a parameter for iv generation. There are three types:\n\n- **Krak\\Crypto\\mcrypt_iv_gen()** - creates a mcrypt iv generator which uses `mcrypt_create_iv`\n- **Krak\\Crypto\\php_iv_gen()** - creates an iv gen that uses `random_bytes`. We use the `paragonie/random_compat` library to handle non php7 users\n- **Krak\\Crypto\\static_iv_gen($iv)** - creates an iv gen that takes an iv and always returns that iv for generation.\n\n## Streams\n\nThe crypt library has also created a concept called a Stream. Crypto streams works very similar to nodejs streams, where they are stream of buffers/content. Streams are very handy for encrypting large amounts of data because of how they efficiently pipe their information along. Here's an example of using streams to upper case content, encrypt, and then encode.\n\n```php\n\u003c?php\n\nuse Krak\\Crypto;\n\n$stream = Crypto\\str_stream('this is some data'); // create a stream from raw string.\n$stream = new Crypto\\StreamPipe($stream);\n\n$crypt_stream = new Crypto\\Stream\\CryptStream(new Crypto\\OpenSSLCrypt($key), 16); // encrypt/decrypt 16 byte chunks at a time\n$base64_stream = new Crypto\\Stream\\Base64Stream(64); // encode/decode 64 byte chunks at a time\n\n$key = random_bytes(16);\n$dst = fopen('php://stdout', 'w');\n$stream-\u003epipe(Crypto\\map_stream('strtoupper'))\n    -\u003epipe($crypt_stream-\u003eencrypt())\n    -\u003epipe($base64_stream-\u003eencode())\n    -\u003epipe(Crypt\\write_stream($dst));\n// at this point, stdout will have encrypted uppercased info.\n```\n\nLook at the API to see all of the different streams and how to use them.\n\n## API\n\nRun `make api` to create the api documentation. Then open up `docs/api/index.html` to view the API docs.\n\n## Test\n\nRun tests with peridot via\n\n```\nmake test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrakphp%2Fcrypto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrakphp%2Fcrypto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrakphp%2Fcrypto/lists"}