{"id":21194133,"url":"https://github.com/petrknap/php-crypto-sodium","last_synced_at":"2025-07-10T03:33:25.469Z","repository":{"id":234227790,"uuid":"788475386","full_name":"petrknap/php-crypto-sodium","owner":"petrknap","description":"The library that packages functional `sodium_crypt_*` into objects.","archived":false,"fork":false,"pushed_at":"2024-10-28T11:25:30.000Z","size":86,"stargazers_count":1,"open_issues_count":7,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-28T12:09:22.760Z","etag":null,"topics":["aead","aes","asymmetric-cryptography","block-cipher","chacha20","cryptography","curve25519","decryption","encryption","php","php-library","poly1305","public-key-cryptography","secret-key-cryptography","security","sodium","stream-cipher","symmetric-cryptography","x25519","xchacha20"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/petrknap.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-18T13:42:38.000Z","updated_at":"2024-10-28T08:00:56.000Z","dependencies_parsed_at":"2024-05-04T09:39:25.455Z","dependency_job_id":null,"html_url":"https://github.com/petrknap/php-crypto-sodium","commit_stats":null,"previous_names":["petrknap/php-crypto-sodium"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrknap%2Fphp-crypto-sodium","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrknap%2Fphp-crypto-sodium/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrknap%2Fphp-crypto-sodium/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrknap%2Fphp-crypto-sodium/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/petrknap","download_url":"https://codeload.github.com/petrknap/php-crypto-sodium/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225615752,"owners_count":17497051,"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":["aead","aes","asymmetric-cryptography","block-cipher","chacha20","cryptography","curve25519","decryption","encryption","php","php-library","poly1305","public-key-cryptography","secret-key-cryptography","security","sodium","stream-cipher","symmetric-cryptography","x25519","xchacha20"],"created_at":"2024-11-20T19:19:49.507Z","updated_at":"2025-07-10T03:33:25.463Z","avatar_url":"https://github.com/petrknap.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Crypto Sodium\n\nA simple library that packages [functional `sodium_crypt_*`](https://www.php.net/manual/en/book.sodium.php) into objects.\n\nInputs and outputs are binary data, don't be afraid to [use the `petrknap/binary`](https://github.com/petrknap/php-binary).\n\n\n## Examples\n\n### Symmetric block encryption\n\n```php\nuse PetrKnap\\CryptoSodium\\SecretBox;\n\n$secretBox = new SecretBox();\n$message = 'Hello World!';\n$key = $secretBox-\u003egenerateKey();\n\n$ciphertext = $secretBox-\u003eencrypt($message, $key);\n\necho $secretBox-\u003edecrypt($ciphertext, $key);\n\n$secretBox-\u003eeraseData($key);\n```\n\n### Asymmetric block encryption\n\n```php\nuse PetrKnap\\CryptoSodium\\Box;\n\n$box = new Box();\n$message = 'Hello World!';\n$sendersKeyPair = $box-\u003egenerateKeyPair();\n$recipientsKeyPair = $box-\u003egenerateKeyPair();\n\n$encryptionKeyPair = $box-\u003egenerateKeyPair(\n    $box-\u003eextractSecretKey($sendersKeyPair),\n    $box-\u003eextractPublicKey($recipientsKeyPair),\n);\n$ciphertext = $box-\u003eencrypt($message, $encryptionKeyPair);\n$box-\u003eeraseData($encryptionKeyPair);\n\n$decryptionKeyPair = $box-\u003egenerateKeyPair(\n    $box-\u003eextractSecretKey($recipientsKeyPair),\n    $box-\u003eextractPublicKey($sendersKeyPair),\n);\necho $box-\u003edecrypt($ciphertext, $decryptionKeyPair);\n$box-\u003eeraseData($decryptionKeyPair);\n\n$box-\u003eeraseData($sendersKeyPair);\n$box-\u003eeraseData($recipientsKeyPair);\n```\n\n### Symmetric stream encryption\n\n```php\nuse PetrKnap\\CryptoSodium\\SecretStream\\XChaCha20Poly1305;\n\n$xChaCha20Poly1305 = new XChaCha20Poly1305();\n$messageChunk1 = 'Hello ';\n$messageChunk2 = 'World!';\n$key = $xChaCha20Poly1305-\u003egenerateKey();\n\n$pushStream = $xChaCha20Poly1305-\u003einitPush($key);\n$ciphertextHeader = $pushStream-\u003eheader;\n$ciphertextChunk1 = $pushStream-\u003epush($messageChunk1);\n$ciphertextChunk2 = $pushStream-\u003epush($messageChunk2, tag: XChaCha20Poly1305::TAG_FINAL);\n\n$pullStream = $xChaCha20Poly1305-\u003einitPull($ciphertextHeader, $key);\necho $pullStream-\u003epull($ciphertextChunk1);\necho $pullStream-\u003epull($ciphertextChunk2);\n\n$xChaCha20Poly1305-\u003eeraseData($key);\n```\n\n### Symmetric block encryption with additional data\n\n```php\nuse PetrKnap\\CryptoSodium\\Aead\\Aes256Gcm;\n\n$aes256Gcm = new Aes256Gcm();\n$message = 'Hello World!';\n$purpose = 'example';\n$key = $aes256Gcm-\u003egenerateKey();\n\n$ciphertext = $aes256Gcm-\u003eencrypt($message, $key, additionalData: $purpose);\n\necho $aes256Gcm-\u003edecrypt($ciphertext, $key, additionalData: $purpose);\n\n$aes256Gcm-\u003eeraseData($key);\n```\n\n### Data signing\n\n```php\nuse PetrKnap\\CryptoSodium\\Sign;\n\n$signer = new Sign();\n$message = 'Hello World!';\n$keyPair = $signer-\u003egenerateKeyPair();\n$secretKey = $signer-\u003eextractSecretKey($keyPair);\n$publicKey = $signer-\u003eextractPublicKey($keyPair);\n\n$signedMessage = $signer-\u003esign($message, $secretKey);\n\necho $signer-\u003everified($signedMessage, $publicKey);\n\n$signer-\u003eeraseData($secretKey);\n$signer-\u003eeraseData($keyPair);\n```\n\n---\n\nRun `composer require petrknap/crypto-sodium` to install it.\nYou can [support this project via donation](https://petrknap.github.io/donate.html).\nThe project is licensed under [the terms of the `LGPL-3.0-or-later`](./COPYING.LESSER).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetrknap%2Fphp-crypto-sodium","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpetrknap%2Fphp-crypto-sodium","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetrknap%2Fphp-crypto-sodium/lists"}