|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2021 Google LLC. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +declare(strict_types=1); |
| 19 | + |
| 20 | +// [START kms_create_key_mac] |
| 21 | +use Google\Cloud\Kms\V1\CryptoKey; |
| 22 | +use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose; |
| 23 | +use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionAlgorithm; |
| 24 | +use Google\Cloud\Kms\V1\CryptoKeyVersionTemplate; |
| 25 | +use Google\Cloud\Kms\V1\KeyManagementServiceClient; |
| 26 | +use Google\Protobuf\Duration; |
| 27 | + |
| 28 | +function create_key_mac_sample( |
| 29 | + string $projectId = 'my-project', |
| 30 | + string $locationId = 'us-east1', |
| 31 | + string $keyRingId = 'my-key-ring', |
| 32 | + string $id = 'my-mac-key' |
| 33 | +) { |
| 34 | + // Create the Cloud KMS client. |
| 35 | + $client = new KeyManagementServiceClient(); |
| 36 | + |
| 37 | + // Build the parent key ring name. |
| 38 | + $keyRingName = $client->keyRingName($projectId, $locationId, $keyRingId); |
| 39 | + |
| 40 | + // Build the key. |
| 41 | + $key = (new CryptoKey()) |
| 42 | + ->setPurpose(CryptoKeyPurpose::MAC) |
| 43 | + ->setVersionTemplate((new CryptoKeyVersionTemplate()) |
| 44 | + ->setAlgorithm(CryptoKeyVersionAlgorithm::HMAC_SHA256) |
| 45 | + ) |
| 46 | + |
| 47 | + // Optional: customize how long key versions should be kept before destroying. |
| 48 | + ->setDestroyScheduledDuration((new Duration()) |
| 49 | + ->setSeconds(24*60*60) |
| 50 | + ); |
| 51 | + |
| 52 | + // Call the API. |
| 53 | + $createdKey = $client->createCryptoKey($keyRingName, $id, $key); |
| 54 | + printf('Created mac key: %s' . PHP_EOL, $createdKey->getName()); |
| 55 | + return $createdKey; |
| 56 | +} |
| 57 | +// [END kms_create_key_mac] |
| 58 | + |
| 59 | +if (isset($argv)) { |
| 60 | + if (count($argv) === 0) { |
| 61 | + return printf("Usage: php %s PROJECT_ID LOCATION_ID KEY_RING_ID ID\n", basename(__FILE__)); |
| 62 | + } |
| 63 | + |
| 64 | + require_once __DIR__ . '/../vendor/autoload.php'; |
| 65 | + list($_, $projectId, $locationId, $keyRingId, $id) = $argv; |
| 66 | + create_key_mac_sample($projectId, $locationId, $keyRingId, $id); |
| 67 | +} |
0 commit comments