专属KMS SDK for PHP帮助您通过简单的编程访问专属KMS的API,实现加密解密、签名验签和获取凭据信息的业务诉求。

背景信息

您可以访问开源代码仓库,查看SDK源码及代码示例。同时也欢迎您提出宝贵意见,或者提供代码示例。

前提条件

  • 您已经启用专属KMS实例并正常连接密码机,为实例创建密钥及应用接入点,并保存了Client Key及CA证书。具体操作,请参见快速入门
    说明 CA证书下载后文件名默认为PrivateKmsCA_kst-******.pem,应用身份凭证文件下载后文件名默认为ClientKey_******.json。
  • 已经获取专属KMS实例VPC地址,并确保可以通过以下方式访问专属KMS实例VPC地址:
    • 在激活密码机实例集群时设置的VPC中访问专属KMS实例VPC地址。
    • 本地设备所在网络可以正常解析并访问专属KMS实例VPC地址。

    具体操作,请参见查询专属KMS标准版实例

安装SDK

  • 方式一(推荐):通过Composer来管理项目依赖

    1. 在终端中切换到项目目录,直接执行以下代码安装AlibabaCloud DKMS-GCS SDK for PHP作为依赖项。
      composer require alibabacloud/dkms-gcs-sdk
    2. 在composer.json中添加以下内容,声明对AlibabaCloud DKMS-GCS SDK for PHP的依赖。
      "require": {
        "alibabacloud/dkms-gcs-sdk": "SDK版本"
        }
      说明 关于最新的SDK版本,请参考开源代码仓库
    3. 在终端中切换到项目目录下,执行以下代码安装依赖。
      composer install
    4. 使用Composer安装完成后,在PHP代码中引入依赖。
      require_once __DIR__ . '/vendor/autoload.php';
  • 方式二:直接下载SDK源码

    访问开源代码仓库下载SDK源码,在PHP代码中引入SDK目录下的autoload.php文件。

    require_once '/path/to/dkms-gcs-sdk/autoload.php';

初始化SDK

您可以初始化一个专属KMS标准版实例的PHP客户端,用于调用专属KMS标准版实例管理的密钥等资源。使用PHP SDK发起专属KMS API请求,您需要初始化一个Client实例,并根据需要修改Config的默认配置项。

  1. 配置CA证书。

    为保障生产环境通信安全,需要配置PHP可信证书。

    设置RuntimeOptionsverify字段。示例代码如下:
    <?php
    
    use AlibabaCloud\Dkms\Gcs\OpenApi\Util\Models\RuntimeOptions;
    
    $runtimeOptions = new RuntimeOptions();
    $runtimeOptions->verify = 'path/to/caCert.pem';
    
    ...
    $encryptResponse = $client->encryptWithOptions($encryptRequest, $runtimeOptions);

    开发环境可使用RuntimeOptionsignoreSSL设置临时忽略可信证书的验证。示例代码如下:

    <?php
    
    use AlibabaCloud\Dkms\Gcs\OpenApi\Util\Models\RuntimeOptions;
    
    $runtimeOptions = new RuntimeOptions();
    $runtimeOptions->ignoreSSL = true;
    
    ...
    $encryptResponse = $client->encryptWithOptions($encryptRequest, $runtimeOptions);
  2. 创建专属KMS标准版Client。

    创建专属KMS标准版Client时,需要指定实例的Endpoint。EndPoint为专属KMS标准版实例VPC地址去掉https://。关于专属KMS标准版实例VPC地址的更多信息,请参见查询专属KMS标准版实例

    <?php
    
    use AlibabaCloud\Dkms\Gcs\Sdk\Client as AlibabaCloudDkmsGcsSdkClient;
    use AlibabaCloud\Dkms\Gcs\OpenApi\Models\Config as AlibabaCloudDkmsGcsOpenApiConfig;
    
    $config = new AlibabaCloudDkmsGcsOpenApiConfig();
    //连接协议,固定为HTTPS。
    $config->protocol = 'https';
    //专属KMS标准版实例Client Key。
    $config->clientKeyContent = '<your client key content>';
    //专属KMS标准版实例Client Key解密口令。
    $config->password = '<your client key password>';
    //Endpoint,专属KMS标准版实例的服务地址去掉“https://”。
    //示例:<service_id>.cryptoservice.kms.aliyuncs.com;
    $config->endpoint = '<your dkms instance service address>';
    
    $client = new AlibabaCloudDkmsGcsSdkClient($config);

代码示例

  • 专属KMS标准版Client调用Encrypt接口使用对称密钥加密数据

    详细代码示例,请参见原始代码

    <?php
    
    require __DIR__ . '/vendor/autoload.php';
    
    use AlibabaCloud\Dkms\Gcs\Sdk\Models\EncryptRequest;
    
    $encryptRequest = new EncryptRequest();
    //专属KMS标准版实例加密密钥的ID或别名(Alias)。
    $encryptRequest->keyId = '<your cipher key id>';
    //待加密数据。
    $encryptRequest->plaintext = \AlibabaCloud\Dkms\Gcs\OpenApi\Util\Utils::toBytes('encrypt plaintext');
    
    $encryptResponse = $client->encryptWithOptions($encryptRequest, $runtimeOptions);
    //密文。
    $ciphertextBlob = $encryptResponse->ciphertextBlob;
    //Cipher初始向量,用于解密数据。
    $iv = $encryptResponse->iv;
    //请求ID。
    $requestId = $encryptResponse->requestId;
  • 专属KMS标准版Client调用Decrypt接口使用对称密钥解密密文

    详细代码示例,请参见原始代码

    <?php
    
    require __DIR__ . '/vendor/autoload.php';
    
    use AlibabaCloud\Dkms\Gcs\Sdk\Models\DecryptRequest;
    
    $decryptRequest = new DecryptRequest();
    //专属KMS标准版实例解密密钥的ID或别名(Alias)。
    $decryptRequest->keyId = '<your cipher key id>';
    //待解密数据,加密返回的密文。
    $decryptRequest->ciphertextBlob = <your ciphertext>;
    //Cipher初始向量,必须与加密时一致。
    $decryptRequest->iv = <IV value>;
    
    $decryptResponse = $client->decryptWithOptions($decryptRequest, $runtimeOptions);
    //原始明文数据。
    $plaintext = $decryptResponse->plaintext;
    //请求ID。
    $requestId = $decryptResponse->requestId;
  • 专属KMS标准版Client调用Sign接口使用非对称密钥进行数字签名

    详细代码示例,请参见原始代码

    <?php
    
    require __DIR__ . '/vendor/autoload.php';
    
    use AlibabaCloud\Dkms\Gcs\Sdk\Models\SignRequest;
    
    $signRequest = new SignRequest();
    //专属KMS标准版实例签名密钥的ID或别名(Alias)。
    $signRequest->keyId = 'your cipher key id';
    //待签名数据。
    $signRequest->message = <the data to sign>;
    
    $signResponse = $client->signWithOptions($signRequest, $runtimeOptions);
    //签名值。
    $signature = $signResponse->signature;
    //请求ID。
    $requestId = $signResponse->requestId;
  • 专属KMS标准版Client调用Verify接口使用非对称密钥验证数字签名

    详细代码示例,请参见原始代码

    <?php
    
    require __DIR__ . '/vendor/autoload.php';
    
    use AlibabaCloud\Dkms\Gcs\Sdk\Models\VerifyRequest;
    
    $verifyRequest = new VerifyRequest();
    //专属KMS标准版实例签名密钥的ID或别名(Alias)。
    $verifyRequest->keyId = 'your cipher key id';
    //待验证签名的数据。
    $verifyRequest->message = <the data to sign>;
    //待验证签名值。
    $verifyRequest->signature = <the signature to verify>;
    
    $verifyResponse = $client->verifyWithOptions($verifyRequest, $runtimeOptions);
    //验签结果。
    $value = $verifyResponse->value;
    //请求ID。
    $requestId = $verifyResponse->requestId;
  • 使用专属KMS标准版Client调用GetSecretValue接口获取凭据值

    详细代码示例,请参见原始代码

    重要 0.2.2及以上版本的专属KMS PHP SDK才支持获取凭据值。
    <?php
    
    require __DIR__ . '/vendor/autoload.php';
    
    use AlibabaCloud\Dkms\Gcs\Sdk\Models\GetSecretValueRequest;
    
    // 您在专属KMS创建的凭据名称。
    $secretName = '<your secret name>';
    
    $getSecretValueRequest = new GetSecretValueRequest([
        'secretName' => $secretName,
    ]);
    
    // 调用获取凭据接口。
    $getSecretValueResponse = $client->getSecretValueWithOptions($getSecretValueRequest, $runtimeOptions);
    
    // 凭据值。
    $_secretData = $getSecretValueResponse->secretData;
    // 请求ID。
    $_requestId = $getSecretValueResponse->requestId;