-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCAPTCHAUtil.java
More file actions
154 lines (114 loc) · 6.4 KB
/
CAPTCHAUtil.java
File metadata and controls
154 lines (114 loc) · 6.4 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package us..view.util;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
public class CAPTCHAUtil {
public CAPTCHAUtil() {
super();
}
private static String[] alphabet = { "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z" };
private static String[] positionNames = { "1st", "2nd", "3rd", "4th",
"5th", "6th" };
private static String[] words = { "RED", "HORSE", "CAT", "BLUE", "ORANGE" };
public static void main(String[] args) throws NoSuchAlgorithmException {
List<String> myCAPTCHA = CAPTCHAUtil.getQuestionAndAnswer();
//System.out.println("CAPTCHAUtil:Q: " + myCAPTCHA.toArray()[0]);
//System.out.println("CAPTCHAUtil:A: " + myCAPTCHA.toArray()[1]);
myCAPTCHA = CAPTCHAUtil.getQuestionAndAnswer();
//System.out.println("CAPTCHAUtil:Q: " + myCAPTCHA.toArray()[0]);
//System.out.println("CAPTCHAUtil:A: " + myCAPTCHA.toArray()[1]);
myCAPTCHA = CAPTCHAUtil.getQuestionAndAnswer();
//System.out.println("CAPTCHAUtil:Q: " + myCAPTCHA.toArray()[0]);
// System.out.println("CAPTCHAUtil:A: " + myCAPTCHA.toArray()[1]);
myCAPTCHA = CAPTCHAUtil.getQuestionAndAnswer();
//System.out.println("CAPTCHAUtil:Q: " + myCAPTCHA.toArray()[0]);
//System.out.println("CAPTCHAUtil:A: " + myCAPTCHA.toArray()[1]);
myCAPTCHA = CAPTCHAUtil.getQuestionAndAnswer();
// System.out.println("CAPTCHAUtil:Q: " + myCAPTCHA.toArray()[0]);
// System.out.println("CAPTCHAUtil:A: " + myCAPTCHA.toArray()[1]);
}
/**
* Return a randomly selected question and answer
*
* @return a List with the question and answer
*/
public static List<String> getQuestionAndAnswer() {
SecureRandom randomGenerator;
try {
randomGenerator = SecureRandom.getInstance("SHA1PRNG");
Integer i = randomGenerator.nextInt(5);
switch (i) {
case 0:
return numberAfter();
case 1:
return numberBefore();
case 2:
return letterAfter();
case 3:
return letterBefore();
case 4:
return letterPosition();
case 5:
return wordLength();
}
} catch (NoSuchAlgorithmException e) {
// don't do anything because SHA1PRNG is always valid
}
return (null);
}
private static List<String> numberAfter() throws NoSuchAlgorithmException {
SecureRandom randomGenerator = SecureRandom.getInstance("SHA1PRNG");
Integer i = randomGenerator.nextInt(18) + 1;
List<String> r = new ArrayList<String>();
r.add("What number comes after " + i + "?");
r.add("" + (i + 1));
return (r);
}
private static List<String> numberBefore() throws NoSuchAlgorithmException {
SecureRandom randomGenerator = SecureRandom.getInstance("SHA1PRNG");
Integer i = randomGenerator.nextInt(18) + 1;
List<String> r = new ArrayList<String>();
r.add("What number comes before " + (i + 1) + "?");
r.add("" + i);
return (r);
}
private static List<String> letterAfter() throws NoSuchAlgorithmException {
SecureRandom randomGenerator = SecureRandom.getInstance("SHA1PRNG");
Integer i = randomGenerator.nextInt(24) + 1;
List<String> r = new ArrayList<String>();
r.add("What letter comes after " + alphabet[i] + " in the alphabet?");
r.add(alphabet[i + 1]);
return (r);
}
private static List<String> letterBefore() throws NoSuchAlgorithmException {
SecureRandom randomGenerator = SecureRandom.getInstance("SHA1PRNG");
Integer i = randomGenerator.nextInt(24) + 1;
List<String> r = new ArrayList<String>();
r.add("What letter comes before " + alphabet[(i + 1)]
+ " in the alphabet?");
r.add(alphabet[i]);
return (r);
}
private static List<String> letterPosition()
throws NoSuchAlgorithmException {
SecureRandom randomGenerator = SecureRandom.getInstance("SHA1PRNG");
Integer w = randomGenerator.nextInt(5);
Integer p = randomGenerator.nextInt(words[w].length());
List<String> r = new ArrayList<String>();
r.add("What is the " + positionNames[p] + " letter in the word "
+ words[w] + "?");
r.add("" + words[w].substring(p, p + 1));
return (r);
}
private static List<String> wordLength() throws NoSuchAlgorithmException {
SecureRandom randomGenerator = SecureRandom.getInstance("SHA1PRNG");
Integer i = randomGenerator.nextInt(5);
List<String> r = new ArrayList<String>();
r.add("How many letters are in the word " + words[i] + "?");
r.add("" + words[i].length());
return (r);
}
}