Skip to content

Commit 438adc6

Browse files
authored
Merge pull request #878 from scalecube/add-utils
Added HexUtil, MaskUtil
2 parents c604eb3 + 73adc0b commit 438adc6

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.scalecube.services;
2+
3+
public final class HexUtil {
4+
5+
private HexUtil() {
6+
// Do not instantiate
7+
}
8+
9+
/**
10+
* Converts bytes array to hex string.
11+
*
12+
* @param bytes bytes array
13+
* @return hex string
14+
*/
15+
public static String toHex(byte[] bytes) {
16+
StringBuilder sb = new StringBuilder();
17+
for (byte b : bytes) {
18+
sb.append(String.format("%02x", b));
19+
}
20+
return sb.toString();
21+
}
22+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.scalecube.services;
2+
3+
import java.util.Map;
4+
import java.util.Map.Entry;
5+
import java.util.UUID;
6+
import java.util.stream.Collectors;
7+
8+
public final class MaskUtil {
9+
10+
private MaskUtil() {
11+
// Do not instantiate
12+
}
13+
14+
/**
15+
* Mask sensitive data by replacing part of string with an asterisk symbol.
16+
*
17+
* @param data sensitive data to be masked
18+
* @return masked data
19+
*/
20+
public static String mask(String data) {
21+
if (data == null || data.length() < 5) {
22+
return "*****";
23+
}
24+
return data.replace(data.substring(2, data.length() - 2), "***");
25+
}
26+
27+
/**
28+
* Mask sensitive data by replacing part of string with an asterisk symbol.
29+
*
30+
* @param data sensitive data to be masked
31+
* @return masked data
32+
*/
33+
public static String mask(UUID data) {
34+
return data != null ? mask(data.toString()) : null;
35+
}
36+
37+
/**
38+
* Mask sensitive data by replacing part of string with an asterisk symbol.
39+
*
40+
* @param map map with sensitive data to be masked
41+
* @return string representation
42+
*/
43+
public static String mask(Map<String, String> map) {
44+
if (map == null || map.isEmpty()) {
45+
return String.valueOf(map);
46+
}
47+
return map.entrySet().stream()
48+
.collect(Collectors.toMap(Entry::getKey, entry -> mask(entry.getValue())))
49+
.toString();
50+
}
51+
}

services-api/src/main/java/io/scalecube/services/Reflect.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import reactor.core.scheduler.Scheduler;
3333
import reactor.core.scheduler.Schedulers;
3434

35-
public class Reflect {
35+
public final class Reflect {
3636

3737
private Reflect() {
3838
// Do not instantiate

0 commit comments

Comments
 (0)