CryptoKey cryptoKey = CryptoKey.newBuilder() .setPurpose(purpose) .setNextRotationTime(nextRotationTime) .setRotationPeriod(rotationPeriod)
/** * Creates an RSA encrypt/decrypt key pair with the given id. */ public static CryptoKey createAsymmetricKey(String projectId, String locationId, String keyRingId, String cryptoKeyId) throws IOException { try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) { String parent = KeyRingName.format(projectId, locationId, keyRingId); CryptoKeyPurpose purpose = CryptoKeyPurpose.ASYMMETRIC_DECRYPT; CryptoKeyVersionAlgorithm algorithm = CryptoKeyVersionAlgorithm.RSA_DECRYPT_OAEP_2048_SHA256; CryptoKeyVersionTemplate version = CryptoKeyVersionTemplate.newBuilder() .setAlgorithm(algorithm) .build(); CryptoKey cryptoKey = CryptoKey.newBuilder() .setPurpose(purpose) .setVersionTemplate(version) .build(); CryptoKey createdKey = client.createCryptoKey(parent, cryptoKeyId, cryptoKey); return createdKey; } } // [END kms_create_asymmetric_key]
/** * Creates a new crypto key with the given id. */ public static CryptoKey createCryptoKey(String projectId, String locationId, String keyRingId, String cryptoKeyId) throws IOException { // Create the Cloud KMS client. try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) { // The resource name of the location associated with the KeyRing. String parent = KeyRingName.format(projectId, locationId, keyRingId); // This will allow the API access to the key for encryption and decryption. CryptoKey cryptoKey = CryptoKey.newBuilder() .setPurpose(CryptoKeyPurpose.ENCRYPT_DECRYPT) .build(); // Create the CryptoKey for your project. CryptoKey createdKey = client.createCryptoKey(parent, cryptoKeyId, cryptoKey); return createdKey; } } // [END kms_create_cryptokey]
String kmsKeyRingResourcePath = KeyRingName.of(projectId, location, keyRingName).toString(); CryptoKey cryptoKey = CryptoKey.newBuilder().setPurpose(CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT).build(); CreateCryptoKeyRequest createCryptoKeyRequest = CreateCryptoKeyRequest.newBuilder()
@Test @SuppressWarnings("all") public void createCryptoKeyExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockKeyManagementService.addException(exception); try { KeyRingName parent = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); String cryptoKeyId = "my-app-key"; CryptoKey.CryptoKeyPurpose purpose = CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT; long seconds = 2147483647L; Timestamp nextRotationTime = Timestamp.newBuilder().setSeconds(seconds).build(); long seconds2 = 604800L; Duration rotationPeriod = Duration.newBuilder().setSeconds(seconds2).build(); CryptoKey cryptoKey = CryptoKey.newBuilder() .setPurpose(purpose) .setNextRotationTime(nextRotationTime) .setRotationPeriod(rotationPeriod) .build(); client.createCryptoKey(parent, cryptoKeyId, cryptoKey); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }