Search in sources :

Example 1 with CharacterMaskConfig

use of com.google.privacy.dlp.v2.CharacterMaskConfig in project java-docs-samples by GoogleCloudPlatform.

the class DeIdentification method deIdentifyWithMask.

// [START dlp_deidentify_masking]
/**
 * Deidentify a string by masking sensitive information with a character using the DLP API.
 *
 * @param string The string to deidentify.
 * @param maskingCharacter (Optional) The character to mask sensitive data with.
 * @param numberToMask (Optional) The number of characters' worth of sensitive data to mask.
 *     Omitting this value or setting it to 0 masks all sensitive chars.
 * @param projectId ID of Google Cloud project to run the API under.
 */
private static void deIdentifyWithMask(String string, Character maskingCharacter, int numberToMask, String projectId) {
    // instantiate a client
    try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
        ContentItem contentItem = ContentItem.newBuilder().setValue(string).build();
        CharacterMaskConfig characterMaskConfig = CharacterMaskConfig.newBuilder().setMaskingCharacter(maskingCharacter.toString()).setNumberToMask(numberToMask).build();
        // Create the deidentification transformation configuration
        PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder().setCharacterMaskConfig(characterMaskConfig).build();
        InfoTypeTransformation infoTypeTransformationObject = InfoTypeTransformation.newBuilder().setPrimitiveTransformation(primitiveTransformation).build();
        InfoTypeTransformations infoTypeTransformationArray = InfoTypeTransformations.newBuilder().addTransformations(infoTypeTransformationObject).build();
        DeidentifyConfig deidentifyConfig = DeidentifyConfig.newBuilder().setInfoTypeTransformations(infoTypeTransformationArray).build();
        // Create the deidentification request object
        DeidentifyContentRequest request = DeidentifyContentRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setDeidentifyConfig(deidentifyConfig).setItem(contentItem).build();
        // Execute the deidentification request
        DeidentifyContentResponse response = dlpServiceClient.deidentifyContent(request);
        // Print the character-masked input value
        // e.g. "My SSN is 123456789" --> "My SSN is *********"
        String result = response.getItem().getValue();
        System.out.println(result);
    } catch (Exception e) {
        System.out.println("Error in deidentifyWithMask: " + e.getMessage());
    }
}
Also used : InfoTypeTransformations(com.google.privacy.dlp.v2.InfoTypeTransformations) DeidentifyContentRequest(com.google.privacy.dlp.v2.DeidentifyContentRequest) PrimitiveTransformation(com.google.privacy.dlp.v2.PrimitiveTransformation) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) DeidentifyConfig(com.google.privacy.dlp.v2.DeidentifyConfig) CharacterMaskConfig(com.google.privacy.dlp.v2.CharacterMaskConfig) ByteString(com.google.protobuf.ByteString) InfoTypeTransformation(com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation) ContentItem(com.google.privacy.dlp.v2.ContentItem) DeidentifyContentResponse(com.google.privacy.dlp.v2.DeidentifyContentResponse) DateTimeParseException(java.time.format.DateTimeParseException) ParseException(org.apache.commons.cli.ParseException)

Aggregations

DlpServiceClient (com.google.cloud.dlp.v2.DlpServiceClient)1 CharacterMaskConfig (com.google.privacy.dlp.v2.CharacterMaskConfig)1 ContentItem (com.google.privacy.dlp.v2.ContentItem)1 DeidentifyConfig (com.google.privacy.dlp.v2.DeidentifyConfig)1 DeidentifyContentRequest (com.google.privacy.dlp.v2.DeidentifyContentRequest)1 DeidentifyContentResponse (com.google.privacy.dlp.v2.DeidentifyContentResponse)1 InfoTypeTransformations (com.google.privacy.dlp.v2.InfoTypeTransformations)1 InfoTypeTransformation (com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation)1 PrimitiveTransformation (com.google.privacy.dlp.v2.PrimitiveTransformation)1 ByteString (com.google.protobuf.ByteString)1 DateTimeParseException (java.time.format.DateTimeParseException)1 ParseException (org.apache.commons.cli.ParseException)1