Files
int-layer/src.bak/main/java/com/example/util/TextObfuscator.java
2026-02-20 20:38:58 +00:00

99 lines
3.2 KiB
Java

/*
* Copyright 2025 Google. This software is provided as-is, without warranty or representation for any use or purpose.
* Your use of it is subject to your agreement with Google.
*/
package com.example.util;
import java.util.Comparator;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.privacy.dlp.v2.Finding;
import com.google.privacy.dlp.v2.InspectContentResponse;
public class TextObfuscator {
private static final Logger logger = LoggerFactory.getLogger(TextObfuscator.class);
public static String obfuscate(InspectContentResponse response, String textToInspect) {
List<Finding> findings = response.getResult().getFindingsList().stream()
.filter(finding -> finding.getLikelihoodValue() > 3)
.sorted(Comparator.comparing(Finding::getLikelihoodValue).reversed())
.peek(finding -> logger.info("InfoType: {} | Likelihood: {}", finding.getInfoType().getName(),
finding.getLikelihoodValue()))
.toList();
for (Finding finding : findings) {
String quote = finding.getQuote();
switch (finding.getInfoType().getName()) {
case "CREDIT_CARD_NUMBER":
textToInspect = textToInspect.replace(quote, "**** **** **** " + getLast4(quote));
break;
case "CREDIT_CARD_EXPIRATION_DATE":
case "FECHA_VENCIMIENTO":
textToInspect = textToInspect.replace(quote, "[FECHA_VENCIMIENTO_TARJETA]");
break;
case "CVV_NUMBER":
case "CVV":
textToInspect = textToInspect.replace(quote, "[CVV]");
break;
case "EMAIL_ADDRESS":
textToInspect = textToInspect.replace(quote, "[CORREO]");
break;
case "PERSON_NAME":
textToInspect = textToInspect.replace(quote, "[NOMBRE]");
break;
case "PHONE_NUMBER":
textToInspect = textToInspect.replace(quote, "[TELEFONO]");
break;
case "DIRECCION":
case "DIR_COLONIA":
case "DIR_DEL_MUN":
case "DIR_INTERIOR":
case "DIR_ESQUINA":
case "DIR_CIUDAD_EDO":
case "DIR_CP":
textToInspect = textToInspect.replace(quote, "[DIRECCION]");
break;
case "CLABE_INTERBANCARIA":
textToInspect = textToInspect.replace(quote, "[CLABE]");
break;
case "CLAVE_RASTREO_SPEI":
textToInspect = textToInspect.replace(quote, "[CLAVE_RASTREO]");
break;
case "NIP":
textToInspect = textToInspect.replace(quote, "[NIP]");
break;
case "SALDO":
textToInspect = textToInspect.replace(quote, "[SALDO]");
break;
case "CUENTA":
textToInspect = textToInspect.replace(quote, "**************" + getLast4(quote));
break;
case "NUM_ACLARACION":
textToInspect = textToInspect.replace(quote, "[NUM_ACLARACION]");
break;
}
}
textToInspect = cleanDireccion(textToInspect);
return textToInspect;
}
private static String getLast4(String quote) {
char[] last4 = new char[4];
String cleanQuote = quote.trim();
cleanQuote = cleanQuote.replace(" ", "");
cleanQuote.getChars(cleanQuote.length() - 4, cleanQuote.length(), last4, 0);
return new String(last4);
}
private static String cleanDireccion(String quote) {
String output = quote.replaceAll("\\[DIRECCION\\](?:(?:,\\s*|\\s+)\\[DIRECCION\\])*", "[DIRECCION]");
return output.trim();
}
}