{"id":19198440,"url":"https://github.com/virgilsecurity/virgil-sdk-php","last_synced_at":"2025-04-15T14:34:43.826Z","repository":{"id":35637892,"uuid":"39912185","full_name":"VirgilSecurity/virgil-sdk-php","owner":"VirgilSecurity","description":"Virgil Core SDK allows developers to get up and running with Virgil Cards Service API quickly and add end-to-end security to their new or existing digital solutions to become HIPAA and GDPR compliant and more.","archived":false,"fork":false,"pushed_at":"2024-10-01T10:15:39.000Z","size":2469,"stargazers_count":10,"open_issues_count":0,"forks_count":4,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-03-28T21:12:36.565Z","etag":null,"topics":["cryptography","encryption","end-to-end-encryption","gdpr","hipaa","pki","sdk"],"latest_commit_sha":null,"homepage":"https://virgilsecurity.com/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/VirgilSecurity.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","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":"2015-07-29T19:29:01.000Z","updated_at":"2024-05-01T02:35:14.000Z","dependencies_parsed_at":"2024-05-01T03:29:24.804Z","dependency_job_id":"e4da8a43-1d3b-4641-bcac-3663b0293dc0","html_url":"https://github.com/VirgilSecurity/virgil-sdk-php","commit_stats":{"total_commits":373,"total_committers":12,"mean_commits":"31.083333333333332","dds":0.6005361930294906,"last_synced_commit":"a9afb55f6feeaad611ca4cad17987fa614428a2d"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VirgilSecurity%2Fvirgil-sdk-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VirgilSecurity%2Fvirgil-sdk-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VirgilSecurity%2Fvirgil-sdk-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VirgilSecurity%2Fvirgil-sdk-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/VirgilSecurity","download_url":"https://codeload.github.com/VirgilSecurity/virgil-sdk-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249089526,"owners_count":21210967,"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":["cryptography","encryption","end-to-end-encryption","gdpr","hipaa","pki","sdk"],"created_at":"2024-11-09T12:22:01.688Z","updated_at":"2025-04-15T14:34:43.776Z","avatar_url":"https://github.com/VirgilSecurity.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Virgil Core SDK PHP\n\n[![Build Status](https://github.com/VirgilSecurity/virgil-sdk-php/actions/workflows/build.yml/badge.svg?branch=master)](https://travis-ci.com/VirgilSecurity/virgil-sdk-php)\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/virgil/sdk.svg?style=flat-square)](https://packagist.org/packages/virgil/sdk)\n[![Total Downloads](https://img.shields.io/packagist/dt/virgil/sdk.svg?style=flat-square)](https://packagist.org/packages/virgil/sdk.svg)\n[![GitHub license](https://img.shields.io/badge/license-BSD%203--Clause-blue.svg)](https://github.com/VirgilSecurity/virgil/blob/master/LICENSE)\n\n[Introduction](#introduction) | [SDK Features](#sdk-features) | [Installation](#installation) | [Configure SDK](#configure-sdk) | [Usage Examples](#usage-examples) | [Docs](#docs) | [Support](#support)\n\n## Introduction\n\n\u003ca href=\"https://developer.virgilsecurity.com/docs\"\u003e\u003cimg width=\"230px\" src=\"https://cdn.virgilsecurity.com/assets/images/github/logos/virgil-logo-red.png\" align=\"left\" hspace=\"10\" vspace=\"6\"\u003e\u003c/a\u003e [Virgil Security](https://virgilsecurity.com) provides a set of APIs for adding security to any application. In a few simple steps you can encrypt communications, securely store data, and ensure data integrity. Virgil Security products are available for desktop, embedded (IoT), mobile, cloud, and web applications in a variety of modern programming languages.\n\nThe Virgil Core SDK is a low-level library that allows developers to get up and running with [Virgil Cards Service API](https://developer.virgilsecurity.com/docs/platform/api-reference/cards-service/) quickly and add end-to-end security to their new or existing digital solutions.\n\nIn case you need additional security functionality for multi-device support, group chats and more, try our high-level [Virgil E3Kit framework](https://github.com/VirgilSecurity/awesome-virgil#E3Kit).\n\n## SDK Features\n\n- Communicate with [Virgil Cards Service](https://developer.virgilsecurity.com/docs/platform/api-reference/cards-service/)\n- Manage users' public keys\n- Encrypt, sign, decrypt and verify data\n- Store private keys in secure local storage\n- Use [Virgil Crypto Library](https://github.com/VirgilSecurity/virgil-crypto-php)\n\n## Installation\n\nThe Virgil Core SDK is provided as a package named [_virgil/sdk_](https://packagist.org/packages/virgil/sdk). The package is distributed via [Composer package](https://getcomposer.org/doc/) management system.\n\nThe package is available for PHP version 8.2 and newer.\n\nInstalling the package using Package Manager Console:\n\n```bash\ncomposer require virgil/sdk\n```\n\n### Crypto Extensions notice\n\nIn order to support crypto operations, you'll also need to install a Virgil crypto extensions. We supply Virgil Core SDK with our own extensions that can be easily used by everyone. To install automatically extensions in your current system just run this command:\n\n```bash\n./vendor/virgil/crypto-wrapper/_extensions/setup.sh -all -vendor\n```\n\nBe aware that crypto-wrapper package appears in your vendors after virgil/sdk have been installed by composer.\nTo check Virgil crypto extensions is proper installed run:\n\n```bash\nphp -m\n```\n\nThere are should be available following extensions: `vsce_phe_php`, `vscf_foundation_php`, `vscp_pythia_php`\n\nNOTE: If following warning is occurred export environment variable `LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/php7/modules` (/usr/lib/php7/modules - your php extensions path can be other):\n\n```\nPHP Warning:  PHP Startup: Unable to load dynamic library 'vsce_phe_php' (tried: /usr/lib/php7/modules/vsce_phe_php (Error loading shared library /usr/lib/php7/modules/vsce_phe_php: No such file or directory), /usr/lib/php7/modules/vsce_phe_php.so (Error loading shared library vscf_foundation_php.so: No such file or directory (needed by /usr/lib/php7/modules/vsce_phe_php.so))) in Unknown on line 0\n```\n\n`LD_LIBRARY_PATH` is environment variable which keeps all path that contains users dynamic shared libraries.\n\nNow Virgil Core SDK is ready to be used, lets configure it and run some samples.\n\n## Configure SDK\n\nThis section contains guides on how to set up Virgil Core SDK modules for authenticating users, managing Virgil Cards and storing private keys.\n\n### Set up authentication\n\nSet up user authentication with tokens that are based on the [JSON Web Token standard](https://jwt.io/) with some Virgil modifications.\n\nIn order to make calls to Virgil Services (for example, to publish user's Card on Virgil Cards Service), you need to have a JSON Web Token (\"JWT\") that contains the user's `identity`, which is a string that uniquely identifies each user in your application.\n\nCredentials that you'll need:\n\n| Parameter  | Description                                                                                                                                                                                               |\n| ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| App ID     | ID of your Application at [Virgil Dashboard](https://dashboard.virgilsecurity.com)                                                                                                                        |\n| App Key ID | A unique string value that identifies your account at the Virgil developer portal                                                                                                                         |\n| App Key    | A Private Key that is used to sign API calls to Virgil Services. For security, you will only be shown the App Key when the key is created. Don't forget to save it in a secure location for the next step |\n\n#### Set up JWT provider on Client side\n\nUse these lines of code to specify which JWT generation source you prefer to use in your project:\n\n```php\nuse Virgil\\Sdk\\Web\\Authorization\\CallbackJwtProvider;\nuse Virgil\\Sdk\\Web\\Authorization\\TokenContext;\n\n$authenticatedQueryToServerSide = function (TokenContext $context){\n    // Get generated token from server-side\n    return \"eyJraWQiOiI3MGI0NDdlMzIxZjNhMGZkIiwidHlwIjoiSldUIiwiYWxnIjoiVkVEUzUxMiIsImN0eSI6InZpcmdpbC1qd3Q7dj0xIn0.eyJleHAiOjE1MTg2OTg5MTcsImlzcyI6InZpcmdpbC1iZTAwZTEwZTRlMWY0YmY1OGY5YjRkYzg1ZDc5Yzc3YSIsInN1YiI6ImlkZW50aXR5LUFsaWNlIiwiaWF0IjoxNTE4NjEyNTE3fQ.MFEwDQYJYIZIAWUDBAIDBQAEQP4Yo3yjmt8WWJ5mqs3Yrqc_VzG6nBtrW2KIjP-kxiIJL_7Wv0pqty7PDbDoGhkX8CJa6UOdyn3rBWRvMK7p7Ak\";\n};\n\n// Setup AccessTokenProvider\n$accessTokenProvider = new CallbackJwtProvider($authenticatedQueryToServerSide);\n```\n\n#### Generate JWT on Server side\n\nNext, you'll need to set up the `JwtGenerator` and generate a JWT using the Virgil SDK.\n\nHere is an example of how to generate a JWT:\n\n```php\nuse Virgil\\Crypto\\VirgilCrypto;\nuse Virgil\\Sdk\\Web\\Authorization\\JwtGenerator;\n\n// App Key (you got this Key at Virgil Dashboard)\n$privateKeyStr = \"MC4CAQAwBQYDK2VwBCIEIH2RKUdXkK/3tfVWO2AJahOhCYG2hCEHg4mPJEAuvmj7\";\n$appKeyData = base64_decode($privateKeyStr);\n\n// VirgilCrypto imports a private key into a necessary format\n$crypto = new VirgilCrypto();\n$privateKey = $crypto-\u003eimportPrivateKey($appKeyData);\n\n// use your App Credentials you got at Virgil Dashboard:\n$appId = \"be00e10e4e1f4bf58f9b4dc85d79c77a\"; // App ID\n$appKeyId = \"70b447e321f3a0fd\";              // App Key ID\n$ttl = 3600; // 1 hour (JWT's lifetime)\n\n// setup JWT generator with necessary parameters:\n$jwtGenerator = new JwtGenerator($privateKey-\u003egetPrivateKey(), $appKeyId, $crypto, $appId, $ttl);\n\n// generate JWT for a user\n// remember that you must provide each user with his unique JWT\n// each JWT contains unique user's identity (in this case - Alice)\n// identity can be any value: name, email, some id etc.\n$identity = \"Alice\";\n$token = $jwtGenerator-\u003egenerateToken($identity);\n\n// as result you get users JWT, it looks like this: \"eyJraWQiOiI3MGI0NDdlMzIxZjNhMGZkIiwidHlwIjoiSldUIiwiYWxnIjoiVkVEUzUxMiIsImN0eSI6InZpcmdpbC1qd3Q7dj0xIn0.eyJleHAiOjE1MTg2OTg5MTcsImlzcyI6InZpcmdpbC1iZTAwZTEwZTRlMWY0YmY1OGY5YjRkYzg1ZDc5Yzc3YSIsInN1YiI6ImlkZW50aXR5LUFsaWNlIiwiaWF0IjoxNTE4NjEyNTE3fQ.MFEwDQYJYIZIAWUDBAIDBQAEQP4Yo3yjmt8WWJ5mqs3Yrqc_VzG6nBtrW2KIjP-kxiIJL_7Wv0pqty7PDbDoGhkX8CJa6UOdyn3rBWRvMK7p7Ak\"\n// you can provide users with JWT at registration or authorization steps\n// Send a JWT to client-side\n$token-\u003e__toString();\n```\n\nFor this subsection we've created a sample backend that demonstrates how you can set up your backend to generate the JWTs. To set up and run the sample backend locally, head over to your GitHub repo of choice:\n\n[Node.js](https://github.com/VirgilSecurity/sample-backend-nodejs) | [Golang](https://github.com/VirgilSecurity/sample-backend-go) | [PHP](https://github.com/VirgilSecurity/sample-backend-php) | [Java](https://github.com/VirgilSecurity/sample-backend-java) | [Python](https://github.com/VirgilSecurity/virgil-sdk-python/tree/master#sample-backend-for-jwt-generation)\nand follow the instructions in README.\n\n### Set up Card Verifier\n\nVirgil Card Verifier helps you automatically verify signatures of a user's Card, for example when you get a Card from Virgil Cards Service.\n\nBy default, `VirgilCardVerifier` verifies only two signatures - those of a Card owner and Virgil Cards Service.\n\nSet up `VirgilCardVerifier` with the following lines of code:\n\n```php\nuse Virgil\\Crypto\\VirgilCrypto;\nuse Virgil\\Sdk\\Verification\\VerifierCredentials;\nuse Virgil\\Sdk\\Verification\\VirgilCardVerifier;\nuse Virgil\\Sdk\\Verification\\Whitelist;\n\n// initialize Crypto library\n$crypto = new VirgilCrypto();\n\n$publicKey = $crypto-\u003eimportPublicKey(\"EXPORTED_PUBLIC_KEY\");\n\n$yourBackendVerifierCredentials = new VerifierCredentials(\"YOUR_BACKEND\", $publicKey);\n\n$yourBackendWhitelist = new Whitelist([$yourBackendVerifierCredentials]);\n\n$cardVerifier = new VirgilCardVerifier($crypto, true, true, [$yourBackendWhitelist]);\n```\n\n### Set up Card Manager\n\nThis subsection shows how to set up a Card Manager module to help you manage users' public keys.\n\nWith Card Manager you can:\n\n- specify an access Token (JWT) Provider.\n- specify a Card Verifier used to verify signatures of your users, your App Server, Virgil Services (optional).\n\nUse the following lines of code to set up the Card Manager:\n\n```php\nuse Virgil\\Sdk\\CardManager;\nuse Virgil\\Sdk\\Verification\\VirgilCardVerifier;\n\n$cardVerifier = new VirgilCardVerifier($virgilCrypto, true, true);\n\n// initialize cardManager and specify accessTokenProvider, cardVerifier\n$cardManager = new CardManager($virgilCrypto, $accessTokenProvider, $cardVerifier);\n```\n\n## Usage Examples\n\nBefore you start practicing with the usage examples, make sure that the SDK is configured. See the [Configure SDK](#configure-sdk) section for more information.\n\n### Generate and publish Virgil Cards at Cards Service\n\nUse the following lines of code to create a user's Card with a public key inside and publish it at Virgil Cards Service:\n\n```php\nuse Virgil\\Crypto\\VirgilCrypto;\nuse Virgil\\Sdk\\CardParams;\n\n$crypto = new VirgilCrypto();\n\n// generate a key pair\n$keyPair = $crypto-\u003egenerateKeyPair();\n\n// save Alice private key into key storage\n$privateKeyStorage-\u003estore($keyPair-\u003egetPrivateKey(), \"Alice\");\n\n// create and publish user's card with identity Alice on the Cards Service\n$card = $cardManager-\u003epublishCard(\n    CardParams::create(\n        [\n            CardParams::PublicKey  =\u003e $keyPair-\u003egetPublicKey(),\n            CardParams::PrivateKey =\u003e $keyPair-\u003egetPrivateKey(),\n        ]\n    )\n);\n```\n\n### Sign then encrypt data\n\nVirgil Core SDK allows you to use a user's private key and their Virgil Cards to sign and encrypt any kind of data.\n\nIn the following example, we load a private key from a customized key storage and get recipient's Card from the Virgil Cards Service. Recipient's Card contains a public key which we will use to encrypt the data and verify a signature.\n\n```php\nuse Virgil\\Crypto\\Core\\VirgilKeys\\VirgilPublicKeyCollection;\n\n// prepare a message\n$dataToEncrypt = 'Hello, Bob!';\n\n// load a private key from a device storage\n$alicePrivateKeyEntry = $privateKeyStorage-\u003eload('Alice');\n\n// using cardManager search for Bob's cards on Cards Service\n$cards = $cardManager-\u003esearchCards('Bob');\n\n$bobRelevantCardsPublicKeys = array_map(\n    function (Virgil\\Sdk\\Card $cards) {\n        return $cards-\u003egetPublicKey();\n    },\n    $cards\n);\n\n$bobRelevantCardsPublicKeysCollection = new VirgilPublicKeyCollection($bobRelevantCardsPublicKeys);\n\n// sign a message with a private key then encrypt using Bob's public keys\n$encryptedData = $crypto-\u003esignAndEncrypt(\n    $dataToEncrypt,\n    $alicePrivateKeyEntry-\u003egetPrivateKey(),\n    $bobRelevantCardsPublicKeysCollection\n);\n```\n\n### Decrypt data and verify signature\n\nOnce the user receives the signed and encrypted message, they can decrypt it with their own private key and verify the signature with the sender's Card:\n\n```php\nuse Virgil\\Crypto\\Core\\VirgilKeys\\VirgilPublicKeyCollection;\n\n// load a private key from a device storage\n$bobPrivateKeyEntry = $privateKeyStorage-\u003eload('Bob');\n\n// using cardManager search for Alice's cards on Cards Service\n$cards = $cardManager-\u003esearchCards('Alice');\n\n$aliceRelevantCardsPublicKeys = array_map(\n    function (Virgil\\Sdk\\Card $cards) {\n        return $cards-\u003egetPublicKey();\n    },\n    $cards\n);\n\n$aliceRelevantCardsPublicKeysCollection = new VirgilPublicKeyCollection($aliceRelevantCardsPublicKeys);\n\n// decrypt with a private key and verify using one of Alice's public keys\n$decryptedData = $crypto-\u003edecryptAndVerify(\n    $encryptedData,\n    $bobPrivateKeyEntry-\u003egetPrivateKey(),\n    $aliceRelevantCardsPublicKeysCollection\n);\n```\n\n### Get Card by its ID\n\nUse the following lines of code to get a user's card from Virgil Cloud by its ID:\n\n```php\n// using cardManager get a user's card from the Cards Service\n$card = $cardManager-\u003egetCard(\"f4bf9f7fcbedaba0392f108c59d8f4a38b3838efb64877380171b54475c2ade8\");\n```\n\n### Get Card by user's identity\n\nFor a single user, use the following lines of code to get a user's Card by a user's `identity`:\n\n```php\n// using cardManager search for user's cards on Cards Service\n$cards = $cardManager-\u003esearchCards(\"Bob\");\n```\n\n### Revoke Card\n\nYou can revoke user's Card in case they don't need it anymore. Revoked Card can still be obtained using its identifier, but this card won't appear during search query.\n\n```php\n// using cardManager revoke user's card on Cards Service by card id\n$cardManager-\u003erevokeCard(\"f4bf9f7fcbedaba0392f108c59d8f4a38b3838efb64877380171b54475c2ade8\");\n```\n\n### Generate key pair using VirgilCrypto\n\nYou can generate a key pair and save it in a secure key storage with the following code:\n\n```php\nuse \\Virgil\\Crypto\\VirgilCrypto;\n\n$crypto = new VirgilCrypto();\n\n$keyPair = $crypto-\u003egenerateKeyPair();\n```\n\n### Save and retrieve key from filesystem key storage\n\n```php\nuse Virgil\\Sdk\\Storage\\PrivateKeyStorage;\nuse Virgil\\Crypto\\VirgilCrypto;\n\n$crypto = new VirgilCrypto();\n\n$keyPair = $crypto-\u003egenerateKeyPair();\n$storage = new PrivateKeyStorage($crypto, '/var/www/storage');\n\n$storage-\u003estore($keyPair-\u003egetPrivateKey(), 'alicePk');\n\n$alicePk = $storage-\u003eload('alicePk');\n```\n\n## Docs\n\nVirgil Security has a powerful set of APIs, and the [Developer Documentation](https://developer.virgilsecurity.com/) can get you started today.\n\n## License\n\nThis library is released under the [3-clause BSD License](LICENSE).\n\n## Support\n\nOur developer support team is here to help you. Find out more information on our [Help Center](https://help.virgilsecurity.com/).\n\nYou can find us on [Twitter](https://twitter.com/VirgilSecurity) or send us email support@VirgilSecurity.com.\n\nAlso, get extra help from our support team on [Slack](https://virgilsecurity.com/join-community).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvirgilsecurity%2Fvirgil-sdk-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvirgilsecurity%2Fvirgil-sdk-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvirgilsecurity%2Fvirgil-sdk-php/lists"}