use of com.google.cloud.kms.v1.KeyRingName in project java-kms by googleapis.
the class CreateKeyAsymmetricDecrypt method createKeyAsymmetricDecrypt.
// Create a new asymmetric key for the purpose of encrypting and decrypting
// data.
public void createKeyAsymmetricDecrypt(String projectId, String locationId, String keyRingId, String id) throws IOException {
// safely clean up any remaining background resources.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// Build the parent name from the project, location, and key ring.
KeyRingName keyRingName = KeyRingName.of(projectId, locationId, keyRingId);
// Build the asymmetric key to create.
CryptoKey key = CryptoKey.newBuilder().setPurpose(CryptoKeyPurpose.ASYMMETRIC_DECRYPT).setVersionTemplate(CryptoKeyVersionTemplate.newBuilder().setAlgorithm(CryptoKeyVersionAlgorithm.RSA_DECRYPT_OAEP_2048_SHA256)).setDestroyScheduledDuration(Duration.newBuilder().setSeconds(24 * 60 * 60)).build();
// Create the key.
CryptoKey createdKey = client.createCryptoKey(keyRingName, id, key);
System.out.printf("Created asymmetric key %s%n", createdKey.getName());
}
}
use of com.google.cloud.kms.v1.KeyRingName in project java-kms by googleapis.
the class CreateKeyAsymmetricSign method createKeyAsymmetricSign.
// Create a new asymmetric key for the purpose of signing and verifying data.
public void createKeyAsymmetricSign(String projectId, String locationId, String keyRingId, String id) throws IOException {
// safely clean up any remaining background resources.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// Build the parent name from the project, location, and key ring.
KeyRingName keyRingName = KeyRingName.of(projectId, locationId, keyRingId);
// Build the asymmetric key to create.
CryptoKey key = CryptoKey.newBuilder().setPurpose(CryptoKeyPurpose.ASYMMETRIC_SIGN).setVersionTemplate(CryptoKeyVersionTemplate.newBuilder().setAlgorithm(CryptoKeyVersionAlgorithm.RSA_SIGN_PKCS1_2048_SHA256)).setDestroyScheduledDuration(Duration.newBuilder().setSeconds(24 * 60 * 60)).build();
// Create the key.
CryptoKey createdKey = client.createCryptoKey(keyRingName, id, key);
System.out.printf("Created asymmetric key %s%n", createdKey.getName());
}
}
use of com.google.cloud.kms.v1.KeyRingName in project java-kms by googleapis.
the class CreateKeyHsm method createKeyHsm.
// Create a new key that is stored in an HSM.
public void createKeyHsm(String projectId, String locationId, String keyRingId, String id) throws IOException {
// safely clean up any remaining background resources.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// Build the parent name from the project, location, and key ring.
KeyRingName keyRingName = KeyRingName.of(projectId, locationId, keyRingId);
// Build the hsm key to create.
CryptoKey key = CryptoKey.newBuilder().setPurpose(CryptoKeyPurpose.ENCRYPT_DECRYPT).setVersionTemplate(CryptoKeyVersionTemplate.newBuilder().setProtectionLevel(ProtectionLevel.HSM).setAlgorithm(CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION)).setDestroyScheduledDuration(Duration.newBuilder().setSeconds(24 * 60 * 60)).build();
// Create the key.
CryptoKey createdKey = client.createCryptoKey(keyRingName, id, key);
System.out.printf("Created hsm key %s%n", createdKey.getName());
}
}
use of com.google.cloud.kms.v1.KeyRingName in project java-docs-samples by GoogleCloudPlatform.
the class CreateKeyLabels method createKeyLabels.
// Create a new key with labels.
public void createKeyLabels(String projectId, String locationId, String keyRingId, String id) throws IOException {
// safely clean up any remaining background resources.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// Build the parent name from the project, location, and key ring.
KeyRingName keyRingName = KeyRingName.of(projectId, locationId, keyRingId);
// Build the key to create with labels.
CryptoKey key = CryptoKey.newBuilder().setPurpose(CryptoKeyPurpose.ENCRYPT_DECRYPT).setVersionTemplate(CryptoKeyVersionTemplate.newBuilder().setAlgorithm(CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION)).putLabels("team", "alpha").putLabels("cost_center", "cc1234").build();
// Create the key.
CryptoKey createdKey = client.createCryptoKey(keyRingName, id, key);
System.out.printf("Created key with labels %s%n", createdKey.getName());
}
}
use of com.google.cloud.kms.v1.KeyRingName in project java-docs-samples by GoogleCloudPlatform.
the class CreateKeyRotationSchedule method createKeyRotationSchedule.
// Create a new key that automatically rotates on a schedule.
public void createKeyRotationSchedule(String projectId, String locationId, String keyRingId, String id) throws IOException {
// safely clean up any remaining background resources.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// Build the parent name from the project, location, and key ring.
KeyRingName keyRingName = KeyRingName.of(projectId, locationId, keyRingId);
// Calculate the date 24 hours from now (this is used below).
long tomorrow = java.time.Instant.now().plus(24, ChronoUnit.HOURS).getEpochSecond();
// Build the key to create with a rotation schedule.
CryptoKey key = CryptoKey.newBuilder().setPurpose(CryptoKeyPurpose.ENCRYPT_DECRYPT).setVersionTemplate(CryptoKeyVersionTemplate.newBuilder().setAlgorithm(CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION)).setRotationPeriod(Duration.newBuilder().setSeconds(java.time.Duration.ofDays(30).getSeconds())).setNextRotationTime(Timestamp.newBuilder().setSeconds(tomorrow)).build();
// Create the key.
CryptoKey createdKey = client.createCryptoKey(keyRingName, id, key);
System.out.printf("Created key with rotation schedule %s%n", createdKey.getName());
}
}
Aggregations