use of com.google.cloud.kms.v1.GenerateRandomBytesResponse in project java-kms by googleapis.
the class GenerateRandomBytes method generateRandomBytes.
// Create a new key for use with MacSign.
public void generateRandomBytes(String projectId, String locationId, int numBytes) throws IOException {
// safely clean up any remaining background resources.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// Build the parent name for the location.
LocationName locationName = LocationName.of(projectId, locationId);
// Generate the bytes.
GenerateRandomBytesResponse response = client.generateRandomBytes(locationName.toString(), numBytes, ProtectionLevel.HSM);
// The data comes back as raw bytes, which may include non-printable
// characters. This base64-encodes the result so it can be printed below.
String encodedData = Base64.getEncoder().encodeToString(response.getData().toByteArray());
System.out.printf("Random bytes: %s", encodedData);
}
}
Aggregations