forked from fxnn/readable-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadableHash.java
More file actions
64 lines (44 loc) · 1.73 KB
/
ReadableHash.java
File metadata and controls
64 lines (44 loc) · 1.73 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package de.fxnn.readablehash;
import java.util.Random;
import javax.annotation.Nullable;
import static java.lang.Character.toLowerCase;
public class ReadableHash {
static final char[] CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ".toCharArray();
static final char[] VOWELS = "AEIOUY".toCharArray();
static final int MIN_SYLLABLE_COUNT_PER_WORD = 1;
static final int MAX_SYLLABLE_COUNT_PER_WORD = 6;
private final HashFunctions hashFunctions = new HashFunctions();
@Nullable
public String hashAsReadableString(@Nullable String input) {
return toReadableString(hashFunctions.computeSha256AsLong(input));
}
@Nullable
public String toReadableString(@Nullable Long value) {
return toReadableString(value, MIN_SYLLABLE_COUNT_PER_WORD, MAX_SYLLABLE_COUNT_PER_WORD);
}
@Nullable
public String toReadableString(@Nullable Long value, int minSyllableCountPerWord, int maxSyllableCountPerWord) {
if (value != null) {
StringBuilder resultBuilder = new StringBuilder();
Random rng = new Random(value);
int syllableCount =
rng.nextInt(maxSyllableCountPerWord - minSyllableCountPerWord + 1) + minSyllableCountPerWord;
for (int syllable = 0; syllable < syllableCount; syllable++) {
char consonant = getRandomConsonant(rng);
if (syllable > 0) {
consonant = toLowerCase(consonant);
}
char vowel = toLowerCase(getRandomVowel(rng));
resultBuilder.append(consonant).append(vowel);
}
return resultBuilder.toString();
}
return null;
}
private char getRandomConsonant(Random rng) {
return CONSONANTS[rng.nextInt(CONSONANTS.length)];
}
private char getRandomVowel(Random rng) {
return VOWELS[rng.nextInt(VOWELS.length)];
}
}