1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.util.password;
35
36 import fr.paris.lutece.portal.service.admin.AdminUserService;
37 import fr.paris.lutece.portal.service.util.AppPropertiesService;
38 import fr.paris.lutece.util.date.DateUtil;
39
40 import java.security.SecureRandom;
41 import java.sql.Timestamp;
42
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.Random;
46
47
48
49
50 public final class PasswordUtil
51 {
52 public static final String PROPERTY_PASSWORD_SIZE = "randomPassword.size";
53 public static final int CONSTANT_DEFAULT_RANDOM_PASSWORD_SIZE = 16;
54 private static final int CONSTANT_NUMBER_LETTERS = 26;
55 private static final int CONSTANT_NUMBER_NUMBERS_BASE10 = 10;
56 private static final int CONSTANT_ASCII_CODE_A_UPPERCASE = 65;
57 private static final int CONSTANT_ASCII_CODE_A_LOWERCASE = 97;
58 private static final int CONSTANT_ASCII_CODE_ZERO = 48;
59 private static final char [ ] CONSTANT_SPECIAL_CHARACTERS = {
60 '!', ',', ':', '?', '$', '-', '@', '}', '{', '(', ')', '*', '+', '=', '[', ']', '%', '.',
61 };
62 private static final String CONSTANT_PASSWORD_BEGIN_REGEX = "^";
63 private static final String CONSTANT_PASSWORD_REGEX_NUM = "(?=.*[0-9])";
64 private static final String CONSTANT_PASSWORD_REGEX_SPECIAL = "(?=.*[^a-zA-Z0-9])";
65 private static final String CONSTANT_PASSWORD_REGEX_UPPER_LOWER = "(?=.*[a-z])(?=.*[A-Z])";
66 private static final String CONSTANT_PASSWORD_END_REGEX = "(.*)$";
67 private static final String PARAMETER_PASSWORD_MINIMUM_LENGTH = "password_minimum_length";
68
69
70 private PasswordUtil( )
71 {
72 }
73
74
75
76
77
78
79 public static String makePassword( )
80 {
81
82 int nPasswordSize = AppPropertiesService.getPropertyInt( PROPERTY_PASSWORD_SIZE, CONSTANT_DEFAULT_RANDOM_PASSWORD_SIZE );
83 int nMinPasswordSize = AdminUserService.getIntegerSecurityParameter( PARAMETER_PASSWORD_MINIMUM_LENGTH );
84
85 if ( nMinPasswordSize > nPasswordSize )
86 {
87 nPasswordSize = nMinPasswordSize;
88 }
89
90 return makePassword( nPasswordSize, true, true, true );
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 public static String makePassword( int nPasswordSize, boolean bUpperAndLowerCase, boolean bNumbers, boolean bSpecialCaracters )
108 {
109
110 Random r = new SecureRandom( );
111
112 ArrayList<Character> listCharacters = new ArrayList<>( nPasswordSize );
113
114
115 int nNumCapitalLetters = bUpperAndLowerCase ? ( r.nextInt( nPasswordSize - 3 ) + 1 ) : 0;
116
117
118 int nNumSpecial = bSpecialCaracters ? ( r.nextInt( nPasswordSize - 2 - nNumCapitalLetters ) + 1 ) : 0;
119
120
121
122 int nNumNumbers = bNumbers ? ( r.nextInt( nPasswordSize - 1 - nNumCapitalLetters - nNumSpecial ) + 1 ) : 0;
123
124
125
126 int nNumSmallLetters = nPasswordSize - nNumCapitalLetters - nNumSpecial - nNumNumbers;
127
128
129 for ( int j = 0; j < nNumCapitalLetters; j++ )
130 {
131 char c1 = (char) ( r.nextInt( CONSTANT_NUMBER_LETTERS ) + CONSTANT_ASCII_CODE_A_UPPERCASE );
132 listCharacters.add( Character.valueOf( c1 ) );
133 }
134
135 for ( int j = 0; j < nNumSmallLetters; j++ )
136 {
137 char c1 = (char) ( r.nextInt( CONSTANT_NUMBER_LETTERS ) + CONSTANT_ASCII_CODE_A_LOWERCASE );
138 listCharacters.add( Character.valueOf( c1 ) );
139 }
140
141 for ( int j = 0; j < nNumNumbers; j++ )
142 {
143 char c1 = (char) ( r.nextInt( CONSTANT_NUMBER_NUMBERS_BASE10 - 1 ) + CONSTANT_ASCII_CODE_ZERO );
144 listCharacters.add( Character.valueOf( c1 ) );
145 }
146
147 for ( int j = 0; j < nNumSpecial; j++ )
148 {
149 char c1 = CONSTANT_SPECIAL_CHARACTERS [r.nextInt( CONSTANT_SPECIAL_CHARACTERS.length )];
150 listCharacters.add( Character.valueOf( c1 ) );
151 }
152
153 Collections.shuffle( listCharacters, r );
154
155 StringBuilder sbPassword = new StringBuilder( listCharacters.size( ) );
156
157 for ( Character myChar : listCharacters )
158 {
159 sbPassword.append( myChar );
160 }
161
162 return sbPassword.toString( );
163 }
164
165
166
167
168
169
170
171
172 public static boolean checkPasswordFormat( String strPassword )
173 {
174 return checkPasswordFormat( strPassword, true, true, true );
175 }
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191 public static boolean checkPasswordFormat( String strPassword, boolean bUpperAndLowerCase, boolean bNumero, boolean bSpecialCaracters )
192 {
193 if ( ( strPassword == null ) || strPassword.isEmpty( ) )
194 {
195 return false;
196 }
197
198 StringBuilder sbRegex = new StringBuilder( CONSTANT_PASSWORD_BEGIN_REGEX );
199
200 if ( bUpperAndLowerCase )
201 {
202 sbRegex.append( CONSTANT_PASSWORD_REGEX_UPPER_LOWER );
203 }
204
205 if ( bNumero )
206 {
207 sbRegex.append( CONSTANT_PASSWORD_REGEX_NUM );
208 }
209
210 if ( bSpecialCaracters )
211 {
212 sbRegex.append( CONSTANT_PASSWORD_REGEX_SPECIAL );
213 }
214
215 sbRegex.append( CONSTANT_PASSWORD_END_REGEX );
216
217 return strPassword.matches( sbRegex.toString( ) );
218 }
219
220
221
222
223
224
225
226
227 public static Timestamp getPasswordMaxValidDate( int nNumberDay )
228 {
229 if ( nNumberDay <= 0 )
230 {
231 return null;
232 }
233
234 long nMilliSeconds = DateUtil.convertDaysInMiliseconds( nNumberDay );
235 return new Timestamp( new java.util.Date( ).getTime( ) + nMilliSeconds );
236 }
237 }