16
16
* Generated strings can have a {@code minHashLength}.
17
17
* </p>
18
18
* <p>
19
- * If used to obfuscates identities, make sure to not expose your {@code salt}, {@code alphabet} nor
20
- * {@code separators} to a client, client-side is not safe.
19
+ * If used to obfuscate identities make sure to not expose your {@code salt} or {@code alphabet}.
21
20
* </p>
22
21
* <p>
23
22
* Only positive numbers are supported.
@@ -51,12 +50,12 @@ public final class Hashids {
51
50
public static final String DEFAULT_ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" ;
52
51
53
52
/**
54
- * Holds the default separators.
53
+ * The default separators.
55
54
* <p>
56
55
* Used to prevent the generation of strings that contain bad, offensive and rude words.
57
56
* </p>
58
57
*/
59
- public static final String DEFAULT_SEPARATORS = "cfhistuCFHISTU" ;
58
+ private static final String DEFAULT_SEPARATORS = "cfhistuCFHISTU" ;
60
59
61
60
/**
62
61
* Holds the maximum number value.
@@ -90,7 +89,6 @@ public final class Hashids {
90
89
private final String guards ;
91
90
private final int minHashLength ;
92
91
private final String salt ;
93
- private final String separators ;
94
92
95
93
/**
96
94
* Constructs a new instance with all default values.
@@ -99,20 +97,18 @@ public final class Hashids {
99
97
* <li>no salt</li>
100
98
* <li>no minimal hash length</li>
101
99
* <li>{@link #DEFAULT_ALPHABET}</li>
102
- * <li>{@link #DEFAULT_SEPARATORS}</li>
103
100
* </ul>
104
101
*/
105
102
public Hashids () {
106
103
this ("" , 0 );
107
104
}
108
105
109
106
/**
110
- * Constructs a new instance with the specified salt and the default minimal hash length, alphabet and separators .
107
+ * Constructs a new instance with the specified salt, the minimal hash length and default alphabet .
111
108
* <p>
112
109
* <ul>
113
110
* <li>no minimal hash length</li>
114
111
* <li>{@link #DEFAULT_ALPHABET}</li>
115
- * <li>{@link #DEFAULT_SEPARATORS}</li>
116
112
* </ul>
117
113
*
118
114
* @param salt the salt value
@@ -122,12 +118,10 @@ public Hashids(String salt) {
122
118
}
123
119
124
120
/**
125
- * Constructs a new instance with the specified salt and the minimal hash length and the default alphabet and
126
- * separators.
121
+ * Constructs a new instance with the specified salt and hash length and the default alphabet.
127
122
* <p>
128
123
* <ul>
129
124
* <li>{@link #DEFAULT_ALPHABET}</li>
130
- * <li>{@link #DEFAULT_SEPARATORS}</li>
131
125
* </ul>
132
126
*
133
127
* @param salt the salt value
@@ -138,11 +132,10 @@ public Hashids(String salt, int minHashLength) {
138
132
}
139
133
140
134
/**
141
- * Constructs a new instance with the specified salt and alphabet and the default minimal hash length and separators .
135
+ * Constructs a new instance with the specified salt and alphabet and the minimal hash length.
142
136
* <p>
143
137
* <ul>
144
138
* <li>no minimal hash length</li>
145
- * <li>{@link #DEFAULT_SEPARATORS}</li>
146
139
* </ul>
147
140
*
148
141
* @param salt the salt value
@@ -153,29 +146,13 @@ public Hashids(String salt, String alphabet) {
153
146
}
154
147
155
148
/**
156
- * Constructs a new instance with the specified salt, minimal hash length and alphabet and the default separators.
157
- * <p>
158
- * <ul>
159
- * <li>{@link #DEFAULT_SEPARATORS}</li>
160
- * </ul>
149
+ * Constructs a new instance with the specified salt, minimal hash length and alphabet.
161
150
*
162
151
* @param salt the salt value
163
152
* @param minHashLength the minimal length of the hash
164
153
* @param alphabet the alphabet value
165
154
*/
166
155
public Hashids (String salt , int minHashLength , String alphabet ) {
167
- this (salt , minHashLength , alphabet , DEFAULT_SEPARATORS );
168
- }
169
-
170
- /**
171
- * Constructs a new instance with the specified salt, minimal hash length, alphabet and separators.
172
- *
173
- * @param salt the salt value
174
- * @param minHashLength the minimal length of the hash
175
- * @param alphabet the alphabet value
176
- * @param separators the chained separators
177
- */
178
- public Hashids (String salt , int minHashLength , String alphabet , String separators ) {
179
156
if (alphabet == null ) {
180
157
throw new IllegalArgumentException ("alphabet was null" );
181
158
}
@@ -203,11 +180,8 @@ public Hashids(String salt, int minHashLength, String alphabet, String separator
203
180
throw new IllegalArgumentException ("Alphabet cannot contains spaces" );
204
181
}
205
182
206
- /*
207
- * Separators should contain only characters present in alphabet.
208
- * Alphabet should not contain separators.
209
- */
210
- StringBuilder seps = new StringBuilder (separators == null ? "" : separators );
183
+ // Alphabet should not contain separators.
184
+ StringBuilder seps = new StringBuilder (DEFAULT_SEPARATORS );
211
185
for (int sepIdx = 0 ; sepIdx < seps .length (); sepIdx ++) {
212
186
int alphaIdx = uniqueAlphabet .indexOf (String .valueOf (seps .charAt (sepIdx )));
213
187
if (alphaIdx == -1 ) {
@@ -247,7 +221,6 @@ public Hashids(String salt, int minHashLength, String alphabet, String separator
247
221
}
248
222
249
223
this .alphabet = uniqueAlphabet .toString ();
250
- this .separators = seps .toString ();
251
224
}
252
225
253
226
/**
@@ -261,14 +234,12 @@ public Hashids(String salt, int minHashLength, String alphabet, String separator
261
234
* <li>no salt</li>
262
235
* <li>no minimum hash length</li>
263
236
* <li>{@link #DEFAULT_ALPHABET}</li>
264
- * <li>{@link #DEFAULT_SEPARATORS}</li>
265
237
* </ul>
266
238
*/
267
239
public static final class Builder {
268
240
269
241
private final String salt ;
270
242
private final String alphabet ;
271
- private final String separators ;
272
243
private final int minHashLength ;
273
244
274
245
/**
@@ -277,15 +248,13 @@ public static final class Builder {
277
248
public Builder () {
278
249
this .salt = "" ;
279
250
this .alphabet = DEFAULT_ALPHABET ;
280
- this .separators = DEFAULT_SEPARATORS ;
281
251
this .minHashLength = 0 ;
282
252
}
283
253
284
- private Builder (String salt , String alphabet , String separators , int minHashLength ) {
254
+ private Builder (String salt , int minHashLength , String alphabet ) {
285
255
this .salt = salt ;
286
- this .alphabet = alphabet ;
287
- this .separators = separators ;
288
256
this .minHashLength = minHashLength ;
257
+ this .alphabet = alphabet ;
289
258
}
290
259
291
260
/**
@@ -295,7 +264,7 @@ private Builder(String salt, String alphabet, String separators, int minHashLeng
295
264
* @return The builder instance with the specified salt
296
265
*/
297
266
public Builder salt (String salt ) {
298
- return new Builder (salt , alphabet , separators , minHashLength );
267
+ return new Builder (salt , minHashLength , alphabet );
299
268
}
300
269
301
270
/**
@@ -305,17 +274,7 @@ public Builder salt(String salt) {
305
274
* @return The builder instance with the specified custom alphabet
306
275
*/
307
276
public Builder alphabet (String alphabet ) {
308
- return new Builder (salt , alphabet , separators , minHashLength );
309
- }
310
-
311
- /**
312
- * Sets the custom separators string.
313
- *
314
- * @param separators The string to use as custom alphabet
315
- * @return The builder instance with the specified custom separators
316
- */
317
- public Builder separators (String separators ) {
318
- return new Builder (salt , alphabet , separators , minHashLength );
277
+ return new Builder (salt , minHashLength , alphabet );
319
278
}
320
279
321
280
/**
@@ -325,7 +284,7 @@ public Builder separators(String separators) {
325
284
* @return The builder instance with the minimum hash length
326
285
*/
327
286
public Builder minHashLength (int minHashLength ) {
328
- return new Builder (salt , alphabet , separators , minHashLength );
287
+ return new Builder (salt , minHashLength , alphabet );
329
288
}
330
289
331
290
/**
@@ -334,7 +293,7 @@ public Builder minHashLength(int minHashLength) {
334
293
* @return The {@link Hashids} instance
335
294
*/
336
295
public Hashids build () {
337
- return new Hashids (salt , minHashLength , alphabet , separators );
296
+ return new Hashids (salt , minHashLength , alphabet );
338
297
}
339
298
}
340
299
@@ -421,8 +380,8 @@ private String doEncode(long... numbers) {
421
380
422
381
if (idx + 1 < numbers .length ) {
423
382
num %= ((int ) last .charAt (0 ) + idx );
424
- sepsIdx = (int ) (num % separators .length ());
425
- result .append (separators .charAt (sepsIdx ));
383
+ sepsIdx = (int ) (num % DEFAULT_SEPARATORS .length ());
384
+ result .append (DEFAULT_SEPARATORS .charAt (sepsIdx ));
426
385
}
427
386
}
428
387
@@ -463,7 +422,7 @@ private long[] doDecode(String hash, String alphabet) {
463
422
if (!hashBreakdown .isEmpty ()) {
464
423
final char lottery = hashBreakdown .charAt (0 );
465
424
hashBreakdown = hashBreakdown .substring (1 );
466
- hashBreakdown = hashBreakdown . replaceAll ("[" + separators + "]" , " " );
425
+ hashBreakdown = Pattern . compile ("[" + DEFAULT_SEPARATORS + "]" ). matcher ( hashBreakdown ). replaceAll ( " " );
467
426
hashArray = hashBreakdown .split (" " );
468
427
469
428
String buffer ;
0 commit comments