Search in sources :

Example 1 with GenerateRandomBytesResponse

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);
    }
}
Also used : GenerateRandomBytesResponse(com.google.cloud.kms.v1.GenerateRandomBytesResponse) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient) LocationName(com.google.cloud.kms.v1.LocationName)

Aggregations

GenerateRandomBytesResponse (com.google.cloud.kms.v1.GenerateRandomBytesResponse)1 KeyManagementServiceClient (com.google.cloud.kms.v1.KeyManagementServiceClient)1 LocationName (com.google.cloud.kms.v1.LocationName)1