[go: up one dir, main page]

Skip to content

aliyun/php-kms-sdk

Repository files navigation

English | 简体中文

Alibaba Cloud KMS SDK for PHP

Latest Stable Version Latest Unstable Version composer.lock Total Downloads License
codecov Scrutinizer Code Quality Travis Build Status Appveyor Build Status Code Intelligence Status

Alibaba Cloud KMS SDK for PHP Supports PHP developers using Alibaba Cloud Key Management Service (KMS).

Prerequisites

Your system will need to meet the Prerequisites, including having PHP >= 5.5. We highly recommend having it compiled with the cURL extension and cURL 7.16.2+.

Installation

If Composer is already installed globally on your system, run the following in the base directory of your project to install Alibaba Cloud KMS SDK for PHP as a dependency:

composer require alibabacloud/kms

Some users may not be able to install due to network problems, you can try to switch the Composer mirror.

Please see the Installation for more detailed information about installing through Composer and other means.

Setting up the client

Before you begin, you need to sign up for an Alibaba Cloud account and retrieve your Credentials. Please pass in your accessKeyId, accessKeySecret, endpoint, View the list of KMS endpoints.

<?php

use AlibabaCloud\Kms\V20160120\Kms;

$client = new Kms('accessKeyId', 'accessKeySecret', 'kms.cn-hangzhou.aliyuncs.com');

Set request options

Please refer to Guzzle Request Options.

<?php

$options = [];

Cancel Key Deletion

Cancels the deletion of a CMK. When this operation is successful, the CMK is set to the Enabled state. API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->cancelKeyDeletion(
        [
            'KeyId' => 'key_id',
        ],
        $options
    );
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

Create Alias

Creates a display name for a CMK. API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->createAlias(
        [
            'AliasName' => 'alias/1234',
            'KeyId'     => 'key_id',
        ]
    );
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

Create Key

Creates a customer master key (CMK). API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->createKey(
        [
            'Origin'      => 'Aliyun_KMS',
            'Description' => 'test key',
            'KeyUsage'    => 'ENCRYPT/DECRYPT',
        ],
        $options
    );
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

Decrypt

Decrypts ciphertext. Ciphertext is plaintext that has been previously encrypted by using any of the following operations. API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->decrypt(
        [
            'CiphertextBlob'    => 'CiphertextBlob',
            'EncryptionContext' => json_encode(['k' => 'v']),
        ],
        $options
    );
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

Delete Alias

Deletes the specified alias. API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->deleteAlias(
        [
            'AliasName' => 'alias/12345',
        ],
        $options
    );
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

Delete Key Material

Deletes the imported key material. API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->deleteKeyMaterial(
        [
            'KeyId' => 'id',
        ],
        $options
    );
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

Describe Key

Returns detailed information about the specified CMK. API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->describeKey(
        [
            'KeyId' => 'id',
        ],
        $options
    );
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

Describe Regions

Returns available regions for the specified account. API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->describeRegions();
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

Disable Key

Sets the state of a CMK to disabled, thereby preventing its use for cryptographic operations. The ciphertext encrypted using the CMK cannot be decrypted until the CMK is enabled again. API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->disableKey(
        [
            'KeyId' => 'id',
        ],
        $options
    );
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

Enable Key

Sets the state of the specified CMK to Enabled, thereby permitting its use for cryptographic operations. API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->enableKey(
        [
            'KeyId' => 'id',
        ],
        $options
    );
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

Encrypt

Encrypts plaintext into ciphertext by using a CMK. API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->encrypt(
        [
            'KeyId'             => 'id',
            'Plaintext'         => 'text',
            'EncryptionContext' => json_encode(['k' => 'v']),
        ],
        $options
    );
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

Generate Data Key

Returns a data encryption key that you can use in your application to encrypt data locally. API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->generateDataKey(
        [
            'KeyId' => 'id',
        ],
        $options
    );
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

Get Parameters for Import

Returns the items you need in order to import key material into KMS from your existing key management infrastructure. The returned items are used in the subsequent ImportKeyMaterial request. API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->getParametersForImport(
        [
            'KeyId'             => 'external_key_id',
            'WrappingAlgorithm' => 'RSAES_OAEP_SHA_256',
            'WrappingKeySpec'   => 'RSA_2048',
        ],
        $options
    );
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

Import Key Material

Imports key material to an existing KMS CMK that was created without key material. API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->importKeyMaterial(
        [
            'KeyId'                 => 'external_key_id',
            'EncryptedKeyMaterial'  => base64_encode('test'),
            'ImportToken'           => 'import_token',
            'KeyMaterialExpireUnix' => time() + 3600,
        ],
        $options
    );
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

List Aliases

Gets a list of all aliases in the caller’s account and region. API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->listAliases(
        [
            'PageNumber' => 1,
            'PageSize'   => 100,
        ],
        $options
    );
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

List Aliases by KeyId

Lists all aliases associated with the CMK. API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->listAliasesByKeyId(
        [
            'KeyId'      => 'key_id',
            'PageNumber' => 1,
            'PageSize'   => 100,
        ],
        $options
    );
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

List Keys

Returns a list of all CMKs in the caller’s account and region. API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->listKeys(
        [
            'PageNumber' => 1,
            'PageSize'   => 100,
        ],
        $options
    );
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

Schedule Key Deletion

Schedules the deletion of a CMK. API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->scheduleKeyDeletion(
        [
            'KeyId'               => 'key_id',
            'PendingWindowInDays' => 7,
        ],
        $options
    );
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

Update Alias

Associates an existing alias with a different CMK. API Reference

<?php

use GuzzleHttp\Exception\GuzzleException;

try {
    $result = $client->updateAlias(
        [
            'KeyId'     => 'key_id',
            'AliasName' => 'alias/12345',
        ],
        $options
    );
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

Issues

Opening an Issue, Issues not conforming to the guidelines may be closed immediately.

Changelog

Detailed changes for each release are documented in the release notes.

Contribution

Please make sure to read the Contributing Guide before making a pull request.

References

License

Apache-2.0

Copyright 1999-2019 Alibaba Group Holding Ltd.