@@ -77,9 +77,7 @@ public class DeIdentification {
77
77
* @param projectId ID of Google Cloud project to run the API under.
78
78
*/
79
79
private static void deIdentifyWithMask (
80
- String string ,
81
- Character maskingCharacter ,
82
- int numberToMask ) {
80
+ String string , Character maskingCharacter , int numberToMask , String projectId ) {
83
81
// [START dlp_deidentify_masking]
84
82
/**
85
83
* Deidentify a string by masking sensitive information with a character using the DLP API.
@@ -95,7 +93,6 @@ private static void deIdentifyWithMask(
95
93
// string = "My SSN is 372819127";
96
94
// numberToMask = 5;
97
95
// maskingCharacter = 'x';
98
-
99
96
ByteContentItem byteContentItem =
100
97
ByteContentItem .newBuilder ()
101
98
.setType (ByteContentItem .BytesType .TEXT_UTF8 )
@@ -112,7 +109,9 @@ private static void deIdentifyWithMask(
112
109
113
110
// Create the deidentification transformation configuration
114
111
PrimitiveTransformation primitiveTransformation =
115
- PrimitiveTransformation .newBuilder ().setCharacterMaskConfig (characterMaskConfig ).build ();
112
+ PrimitiveTransformation .newBuilder ()
113
+ .setCharacterMaskConfig (characterMaskConfig )
114
+ .build ();
116
115
117
116
InfoTypeTransformation infoTypeTransformationObject =
118
117
InfoTypeTransformation .newBuilder ()
@@ -124,15 +123,15 @@ private static void deIdentifyWithMask(
124
123
.addTransformations (infoTypeTransformationObject )
125
124
.build ();
126
125
127
- // Create the deidentification request object
128
126
DeidentifyConfig deidentifyConfig =
129
127
DeidentifyConfig .newBuilder ()
130
128
.setInfoTypeTransformations (infoTypeTransformationArray )
131
129
.build ();
132
130
131
+ // Create the deidentification request object
133
132
DeidentifyContentRequest request =
134
133
DeidentifyContentRequest .newBuilder ()
135
- .setParent (projectId )
134
+ .setParent (String . format ( "projects/%s" , projectId ) )
136
135
.setDeidentifyConfig (deidentifyConfig )
137
136
.setItem (contentItem )
138
137
.build ();
@@ -142,10 +141,11 @@ private static void deIdentifyWithMask(
142
141
143
142
// Print the character-masked input value
144
143
// e.g. "My SSN is 123456789" --> "My SSN is *********"
145
- ContentItem item = response .getItem ();
146
- System .out .println (item . getValue () );
144
+ String result = response .getItem (). getByteItem (). getData (). toStringUtf8 ();
145
+ System .out .println (result );
147
146
} catch (Exception e ) {
148
147
System .out .println ("Error in deidentifyWithMask: " + e .getMessage ());
148
+ System .out .println (e .getStackTrace ());
149
149
}
150
150
}
151
151
// [END dlp_deidentify_mask]
@@ -223,7 +223,7 @@ private static void deIdentifyWithFpe(
223
223
224
224
DeidentifyContentRequest request =
225
225
DeidentifyContentRequest .newBuilder ()
226
- .setParent (projectId )
226
+ .setParent (String . format ( "projects/%s" , projectId ) )
227
227
.setDeidentifyConfig (deidentifyConfig )
228
228
.setItem (contentItem )
229
229
.build ();
@@ -233,8 +233,8 @@ private static void deIdentifyWithFpe(
233
233
234
234
// Print the deidentified input value
235
235
// e.g. "My SSN is 123456789" --> "My SSN is 7261298621"
236
- ContentItem item = response .getItem ();
237
- System .out .println (item . getValue () );
236
+ String result = response .getItem (). getByteItem (). getData (). toStringUtf8 ();
237
+ System .out .println (result );
238
238
} catch (Exception e ) {
239
239
System .out .println ("Error in deidentifyWithFpe: " + e .getMessage ());
240
240
}
@@ -292,7 +292,7 @@ private static void deidentifyWithDateShift(
292
292
KmsWrappedCryptoKey kmsWrappedCryptoKey =
293
293
KmsWrappedCryptoKey .newBuilder ()
294
294
.setCryptoKeyName (keyName )
295
- .setWrappedKey (ByteString .copyFromUtf8 ( wrappedKey ))
295
+ .setWrappedKey (ByteString .copyFrom ( BaseEncoding . base64 (). decode ( wrappedKey ) ))
296
296
.build ();
297
297
dateShiftConfigBuilder .setCryptoKey (
298
298
CryptoKey .newBuilder ().setKmsWrapped (kmsWrappedCryptoKey ).build ());
@@ -352,7 +352,7 @@ private static void deidentifyWithDateShift(
352
352
353
353
DeidentifyContentRequest request =
354
354
DeidentifyContentRequest .newBuilder ()
355
- .setParent (projectId )
355
+ .setParent (String . format ( "projects/%s" , projectId ) )
356
356
.setDeidentifyConfig (deidentifyConfig )
357
357
.setItem (tableItem )
358
358
.build ();
@@ -369,7 +369,7 @@ private static void deidentifyWithDateShift(
369
369
370
370
File outputFile = outputCsvPath .toFile ();
371
371
if (!outputFile .exists ()) {
372
- outputFile .mkdirs ();
372
+ outputFile .getParentFile (). mkdirs ();
373
373
outputFile .createNewFile ();
374
374
}
375
375
BufferedWriter bufferedWriter = new BufferedWriter (new FileWriter (outputFile ));
@@ -379,27 +379,19 @@ private static void deidentifyWithDateShift(
379
379
380
380
// write out each row
381
381
for (Table .Row outputRow : outputRows ) {
382
- String row =
383
- outputRow
384
- .getValuesList ()
385
- .stream ()
386
- .map (
387
- value ->
388
- (value .getDateValue () != null )
389
- ? (String .valueOf (value .getDateValue ().getMonth ())
390
- + "/"
391
- + String .valueOf (value .getDateValue ().getDay ())
392
- + "/"
393
- + String .valueOf (value .getDateValue ().getYear ()))
394
- : value .getStringValue ())
395
- .collect (Collectors .joining ("," ));
382
+ String row = outputRow .getValuesList ()
383
+ .stream ()
384
+ .map (value -> value .getStringValue ())
385
+ .collect (Collectors .joining ("," ));
396
386
bufferedWriter .append (row + "\n " );
397
387
}
398
388
399
389
bufferedWriter .flush ();
400
390
bufferedWriter .close ();
401
391
402
- System .out .println ("Successfully saved date-shift output to:" + outputCsvPath .getFileName ());
392
+ System .out .println ("Successfully saved date-shift output to: " + outputCsvPath .getFileName ());
393
+ } catch (Exception e ) {
394
+ System .out .println ("Error in deidentifyWithDateShift: " + e .getMessage ());
403
395
}
404
396
}
405
397
@@ -446,15 +438,16 @@ public static void main(String[] args) throws Exception {
446
438
optionsGroup .setRequired (true );
447
439
448
440
Option deidentifyMaskingOption =
449
- new Option ("m" , "mask" , true , "Deidentify with character masking" );
441
+ new Option ("m" , "mask" , true , "Deidentify with character masking. " );
450
442
optionsGroup .addOption (deidentifyMaskingOption );
451
443
452
- Option deidentifyFpeOption = new Option ("f" , "fpe" , true , "Deidentify with FFX FPE" );
444
+ Option deidentifyFpeOption =
445
+ new Option ("f" , "fpe" , true , "Deidentify with FFX FPE." );
453
446
optionsGroup .addOption (deidentifyFpeOption );
454
447
455
448
Option deidentifyDateShiftOption =
456
- new Option (
457
- "d" , "date" , true , "Deidentify dates in a CSV file by pseudorandomly shifting them." );
449
+ new Option ("d" , "date" , false , "Deidentify dates in a CSV file." );
450
+ optionsGroup . addOption ( deidentifyDateShiftOption );
458
451
459
452
Options commandLineOptions = new Options ();
460
453
commandLineOptions .addOptionGroup (optionsGroup );
@@ -533,8 +526,8 @@ public static void main(String[] args) throws Exception {
533
526
cmd .getOptionValue (
534
527
alphabetOption .getOpt (), FfxCommonNativeAlphabet .ALPHA_NUMERIC .name ()));
535
528
deIdentifyWithFpe (val , alphabet , keyName , wrappedKey , projectId );
536
- } // deidentify with date shift
537
- else if ( cmd . hasOption ( "d" )) {
529
+ } else if ( cmd . hasOption ( "d" )) {
530
+ //deidentify with date shift
538
531
String inputCsv = cmd .getOptionValue (inputCsvPathOption .getOpt ());
539
532
String outputCsv = cmd .getOptionValue (outputCsvPathOption .getOpt ());
540
533
0 commit comments