1
|
|
|
/* |
2
|
|
|
* Copyright 2014, Armenak Grigoryan, and individual contributors as indicated |
3
|
|
|
* by the @authors tag. See the copyright.txt in the distribution for a |
4
|
|
|
* full listing of individual contributors. |
5
|
|
|
* |
6
|
|
|
* This is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation; either version 2.1 of |
9
|
|
|
* the License, or (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This software is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14
|
|
|
* Lesser General Public License for more details. |
15
|
|
|
*/ |
16
|
|
|
package com.strider.datadefender.anonymizer.functions; |
17
|
|
|
|
18
|
|
|
import com.strider.datadefender.functions.NamedParameter; |
19
|
|
|
import com.strider.datadefender.utils.Encoder; |
20
|
|
|
|
21
|
|
|
import java.io.IOException; |
22
|
|
|
|
23
|
|
|
import lombok.extern.log4j.Log4j2; |
24
|
|
|
import org.apache.commons.lang3.RandomUtils; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Built-in anonymization helper functions for personal bio data. |
28
|
|
|
* |
29
|
|
|
* @author Armenak Grigoryan |
30
|
|
|
*/ |
31
|
|
|
@Log4j2 |
32
|
|
|
public class Bio extends Core { |
33
|
|
|
|
34
|
|
|
public String randomFirstName(@NamedParameter("fistName") String firstName) { |
35
|
|
|
|
36
|
|
|
log.debug("Executing function randomFirstNameD"); |
37
|
|
|
String detFirstName = new Encoder().encrypt(firstName, this.getHash()); |
38
|
|
|
log.debug("detFirstName = " + detFirstName); |
39
|
|
|
log.debug("detFirstName.length() = " + detFirstName.length()); |
40
|
|
|
|
41
|
|
|
return detFirstName; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public String randomFirstName() throws IOException { |
45
|
|
|
return randomStringFromStream( |
46
|
|
|
"resource:first_names.txt", |
47
|
|
|
() -> Lipsum.class.getResourceAsStream("first_names.txt") |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public String randomLastName() throws IOException { |
52
|
|
|
return randomStringFromStream( |
53
|
|
|
"resource:last_names.txt", |
54
|
|
|
() -> Lipsum.class.getResourceAsStream("last_names.txt") |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public String randomLastName(@NamedParameter("lastName") String lastName) { |
59
|
|
|
|
60
|
|
|
log.debug("Executing function randomLastNameD"); |
61
|
|
|
String detLastName = new Encoder().encrypt(lastName, this.getHash()); |
62
|
|
|
log.debug("detLastName = " + detLastName); |
63
|
|
|
log.debug("detLastName.length() = " + detLastName.length()); |
64
|
|
|
|
65
|
|
|
return detLastName; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
public String randomMiddleName(final String file) throws IOException { |
70
|
|
|
return randomFirstName(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
201 |
|
* Creates a random username with up to 10 characters and between 0 and 2 |
75
|
201 |
|
* digits following. |
76
|
402 |
|
* |
77
|
201 |
|
* @return |
78
|
402 |
|
*/ |
79
|
201 |
|
public String randomUser() { |
80
|
|
|
return randomUser(10, RandomUtils.nextInt(0, 3)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Generates a random user of up to maxChars length and followed by |
85
|
|
|
* numDigits digits. |
86
|
|
|
* |
87
|
|
|
* For each '10' chars, an additional word is generated on a random basis |
88
|
|
|
* to make up the user name. So a request for a username with 11 characters |
89
|
|
|
* may be either a single word, 'user' or maybe two, separated by a "_": |
90
|
50 |
|
* 'user_name'. |
91
|
50 |
|
* |
92
|
|
|
* @param maxCharacters |
93
|
|
|
* @param numDigits |
94
|
|
|
* @return |
95
|
|
|
*/ |
96
|
|
|
public String randomUser(@NamedParameter("maxCharacters") int maxCharacters, @NamedParameter("numDigits") int numDigits) { |
97
|
|
|
int maxWords = (int) Math.ceil(maxCharacters / 10d); |
98
|
|
|
int numWords = (maxWords > 1) ? RandomUtils.nextInt(1, maxWords + 1) : 1; |
99
|
|
|
String user = randomString(numWords, maxCharacters).toLowerCase().replaceAll("[^a-z ]", "").replace(" ", "_"); |
100
|
|
|
String digits = (numDigits > 0) ? Integer.toString(RandomUtils.nextInt(0, (int) Math.pow(10, numDigits))) : ""; |
101
|
|
|
return user + ("0".repeat(numDigits) + digits).substring(digits.length()); |
102
|
|
|
} |
103
|
|
|
|
104
|
100 |
|
/** |
105
|
100 |
|
* Creates an email with a user part up to 20 characters long, and between |
106
|
100 |
|
* 0 and 2 digits following, followed by an '@' character, and the provided |
107
|
|
|
* domainName. |
108
|
|
|
* |
109
|
|
|
* @param domainName |
110
|
|
|
* @return |
111
|
|
|
*/ |
112
|
|
|
public String randomEmail(@NamedParameter("domainName") String domainName) { |
113
|
|
|
return randomEmail(domainName, 20, RandomUtils.nextInt(0, 3)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Uses the randomUser method to create the user portion of an email with |
118
|
|
|
* the passed maxUserCharacters and numDigits, but replaces returned '_' |
119
|
|
|
* characters with '.'. |
120
|
|
|
* |
121
|
|
|
* @param domainName |
122
|
|
|
* @param maxUserCharacters |
123
|
|
|
* @param numDigits |
124
|
|
|
* @return |
125
|
|
|
*/ |
126
|
|
|
public String randomEmail(@NamedParameter("domainName") String domainName, @NamedParameter("maxUserCharacters") int maxUserCharacters, @NamedParameter("numDigits") int numDigits) { |
127
|
|
|
String user = randomUser(maxUserCharacters, numDigits).replace("_", "."); |
128
|
|
|
return user + "@" + domainName; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|