1
- package io .devxchange .obfuscator ;
1
+ package io .devxchange .obfuscator . util ;
2
2
3
- import com .fasterxml .jackson .core .type .TypeReference ;
4
- import com .fasterxml .jackson .databind .ObjectMapper ;
5
3
import org .apache .commons .codec .digest .DigestUtils ;
6
4
import org .apache .commons .lang3 .StringUtils ;
7
5
import org .slf4j .Logger ;
8
6
import org .slf4j .LoggerFactory ;
9
7
10
- import java .io .IOException ;
11
8
import java .nio .charset .StandardCharsets ;
12
9
import java .util .*;
13
10
import java .util .stream .Collectors ;
14
11
15
12
/**
16
13
* Created by io.devxchange on 5/2/17.
17
14
*/
18
- public class JsonObfuscator {
19
-
20
- private static final Logger LOG = LoggerFactory .getLogger (JsonObfuscator .class );
15
+ public class ObfuscatorUtil {
21
16
17
+ private static final Logger LOG = LoggerFactory .getLogger (ObfuscatorUtil .class );
22
18
23
19
public static final String MASK_FIXED = "********" ;
24
20
25
- private static final TypeReference defaultTypeRef = new TypeReference <Map <String , Object >>(){};
26
- private static final ObjectMapper jsonObjectMapper = new ObjectMapper ();
21
+
27
22
28
23
public static final String STRATEGY_MD5 = "md5" ;
29
24
public static final String STRATEGY_SHA256 = "sha256" ;
@@ -56,12 +51,12 @@ public static String obfuscate(String strategy, String value) {
56
51
return applyFixedMask (value );
57
52
}
58
53
} else {
59
- LOG .warn ("Unknown obfuscation strategy: {}" ,strategy );
54
+ LOG .warn ("Unknown obfuscation strategy: {}" ,strategy );
60
55
return applyFixedMask (value );
61
56
}
62
57
}
63
58
64
- public static Map <String ,String > createFieldStrategyMap (Map <String ,Set <String >> strategyFieldMap ) {
59
+ public static Map <String ,String > createFieldStrategyMap (Map <String , Set <String >> strategyFieldMap ) {
65
60
Map <String ,String > obfuscateMap = new HashMap <>();
66
61
strategyFieldMap .forEach ((key ,values ) -> {
67
62
values .forEach (value -> {
@@ -107,37 +102,7 @@ public static String applyHashSha256(String target) {
107
102
}
108
103
}
109
104
}
110
-
111
- public static String obfuscateJsonString (String payload , Map <String ,Set <String >> strategyMap ) {
112
- if (StringUtils .isBlank (payload ) || strategyMap ==null || strategyMap .isEmpty ()) {
113
- return payload ;
114
- } else {
115
- Map <String , Object > jsonMap ;
116
- try {
117
- jsonMap = jsonObjectMapper .readValue (payload , defaultTypeRef );
118
- } catch (IOException ioEx ) {
119
- LOG .error ("Failed to parse json payload. Returning payload unaltered." ,ioEx );
120
- return payload ;
121
- }
122
- if (jsonMap !=null ) {
123
- Map <String ,String > fieldStrategyMap = createFieldStrategyMap (strategyMap );
124
- processJsonMap (jsonMap ,fieldStrategyMap );
125
- // Convert back map to json
126
- String jsonResult = "" ;
127
- try {
128
- jsonResult = jsonObjectMapper .writeValueAsString (jsonMap );
129
- } catch (IOException e ) {
130
- LOG .warn ("cannot create json from Map" + e .getMessage ());
131
- }
132
- return jsonResult ;
133
- } else {
134
- LOG .error ("JSON object mapping of payload returned null. Returning original payload." );
135
- return payload ;
136
- }
137
- }
138
- }
139
-
140
- private static void processJsonMap (Map <String , Object > jsonMap , Map <String ,String > fieldStrategyMap ) {
105
+ public static void processJsonMap (Map <String , Object > jsonMap , Map <String ,String > fieldStrategyMap ) {
141
106
jsonMap .forEach ((fieldName ,value ) -> {
142
107
if (fieldStrategyMap .containsKey (fieldName )) {
143
108
jsonMap .put (fieldName ,processJsonField (fieldName ,value ,fieldStrategyMap ));
@@ -153,7 +118,7 @@ private static void processJsonMap(Map<String, Object> jsonMap, Map<String,Strin
153
118
});
154
119
}
155
120
156
- private static Object processJsonField (String fieldName , Object value , Map <String ,String > fieldStrategyMap ) {
121
+ public static Object processJsonField (String fieldName , Object value , Map <String ,String > fieldStrategyMap ) {
157
122
if (value ==null ) {
158
123
return value ;
159
124
} else if (value instanceof String ) {
@@ -172,4 +137,7 @@ private static Object processJsonField(String fieldName, Object value, Map<Strin
172
137
return value ;
173
138
}
174
139
}
140
+ public static String extractFieldRegex (Map <String ,Set <String >> fieldMap ) {
141
+ return fieldMap .entrySet ().stream ().map (Map .Entry ::getValue ).distinct ().map (value -> String .join ("|" ,value )).collect (Collectors .joining ("|" ));
142
+ }
175
143
}
0 commit comments