-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunMe.java
More file actions
338 lines (279 loc) · 9.67 KB
/
RunMe.java
File metadata and controls
338 lines (279 loc) · 9.67 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
* Run this code with provided arguments.
*
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
*/
public class RunMe {
public static void main(String[] args) {
final byte[] password = parseArgs(args);
System.out.println("Register keys at http://gg.gg/year2019-prog-intro-1");
key0(password);
System.out.println("The first key was low-hanging fruit, can you found others?");
System.out.println("Try to modify some code in keyX(...) functions");
key2(password);
key3(password);
key4(password);
key5(password);
key6(password);
key7(password);
key8(password);
key9(password);
key10(password);
key11(password);
key12(password);
key13(password);
}
private static void key0(final byte[] password) {
// The result of print(...) function depends only by explicit arguments
print(0, 0, password);
}
private static void key1(final byte[] password) {
while ("1".length() == 1) {
}
print(1, 224753495938049280L, password);
}
private static void key2(final byte[] password) {
int result = 0;
for (int i = 0; i < 300_000; i++) {
for (int j = 0; j < 300_000; j++) {
for (int k = 0; k < 300_000; k++) {
result ^= (i * 7) | (j + k);
result ^= result << 1;
}
}
}
print(2, 7345540958740857324L, password);
}
private static void key3(final byte[] password) {
int result = 0;
for (int i = 0; i < 3000; i++) {
for (int j = 0; j < 3000; j++) {
for (int k = 0; k < 3000; k++) {
result ^= (i * 3) | (j + k);
result ^= result << 1;
}
}
}
print(3, result, password);
}
private static void key4(final byte[] password) {
for (long i = Long.MIN_VALUE; i < Long.MAX_VALUE; i++) {
if ((i ^ (i >> 32)) == 2348327482723738188L) {
print(4, i, password);
}
}
}
private static final long PRIME = 1073741789;
private static void key5(final byte[] password) {
long result = 0;
for (long i = 0; i < 1000_000_000_000_000L; i++) {
result = (result + i + i / 2 + i / 3) % PRIME;
}
print(5, result, password);
}
private static void key6(final byte[] password) {
/*
\u002a\u002f\u0077\u0068\u0069\u006c\u0065\u0020\u0028\u0022\u0031\u0022
\u002e\u006c\u0065\u006e\u0067\u0074\u0068\u0028\u0029\u0020\u003d\u003d
\u0020\u0031\u0029\u003b\u0020\u0020\u006c\u006f\u006e\u0067\u0020\u0009
\u0020\u0020\u0072\u0065\u0073\u0075\u006c\u0074\u0020\u003d\u0020\u000a
\u0033\u0034\u0035\u0037\u0030\u0034\u0033\u0038\u0037\u0035\u0030\u004c
\u003b\u002f\u002a
*/
print(6, result, password);
}
private static void key7(final byte[] password) {
// Count the number of occurrences of the most frequent noun at this following page:
// https://docs.oracle.com/javase/specs/jls/se11/html/jls-4.html
int result = 0;
if (result != 0) {
print(7, result, password);
}
}
private static final String PATTERN = "Hello, World! Привет, Мир!";
private static final int SMALL_REPEAT_COUNT = 10_000_000;
private static void key8(final byte[] password) {
String repeated = "";
for (int i = 0; i < SMALL_REPEAT_COUNT; i++) {
repeated += PATTERN;
}
print(8, repeated.hashCode(), password);
}
private static final long LARGE_REPEAT_SHIFT = 27;
private static final long LARGE_REPEAT_COUNT = 1L << LARGE_REPEAT_SHIFT;
private static void key9(final byte[] password) {
String repeated = "";
for (long i = 0; i < LARGE_REPEAT_COUNT; i++) {
repeated += PATTERN;
}
print(9, repeated.hashCode(), password);
}
private static void key10(final byte[] password) {
print(10, 4508604576084534553L, password);
}
private static void key11(final byte[] password) {
final BigInteger year = BigInteger.valueOf(-2019);
final BigInteger prime = BigInteger.valueOf(PRIME);
long result = Stream.iterate(BigInteger.ZERO, BigInteger.ONE::add)
.filter(i -> year.multiply(i).add(prime).multiply(i).compareTo(BigInteger.ZERO) > 0)
.mapToLong(i -> i.longValue() * password[i.intValue() % password.length])
.sum();
print(11, result, password);
}
private static final long MAX_DEPTH = 10_000_000L;
private static void key12(final byte[] password) {
try {
key12(password, 0, 0);
} catch (StackOverflowError soe) {
System.err.println("Stack overflow :(");
}
}
private static void key12(final byte[] password, long depth, long result) {
if (depth < MAX_DEPTH) {
key12(password, depth + 1, (result ^ 5487) + depth * 11);
} else {
print(12, result, password);
}
}
private static void key13(final byte[] password) {
final BigInteger secondsInDay = BigInteger.valueOf(60 * 60 * 24);
long result = Stream.iterate(BigInteger.ZERO, BigInteger.ONE::add)
.map(secondsInDay::multiply)
.collect(Collectors.reducing(BigInteger.ZERO, BigInteger::add))
.longValue();
print(13, result, password);
}
// ---------------------------------------------------------------------------------------------------------------
// You may ignore all code below this line.
// It is not required to get all keys
// ---------------------------------------------------------------------------------------------------------------
private static void print(final int no, long result, final byte[] password) {
final byte[] key = password.clone();
for (int i = 0; i < 6; i++) {
key[i] ^= result;
result >>>= 8;
}
System.out.format("Key %d: %s%n", no, key(key));
}
private static String key(final byte[] data) {
DIGEST.update(SALT);
DIGEST.update(data);
DIGEST.update(SALT);
final byte[] digest = DIGEST.digest();
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < 6; i++) {
if (i != 0) {
sb.append("-");
}
sb.append(KEYWORDS.get(digest[i] & 63));
}
return sb.toString();
}
private static byte[] parseArgs(final String[] args) {
if (args.length != 6) {
throw error("Expected 6 command line arguments, found: %d", args.length);
}
final byte[] bytes = new byte[args.length];
for (int i = 0; i < args.length; i++) {
final Byte value = VALUES.get(args[i].toLowerCase());
if (value == null) {
throw error("Expected keyword, found: %s", args[i]);
}
bytes[i] = value;
}
return bytes;
}
private static AssertionError error(final String format, final Object... args) {
System.err.format(format, args);
System.err.println();
System.exit(1);
throw new AssertionError();
}
private static final MessageDigest DIGEST;
static {
try {
DIGEST = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new AssertionError("Cannot create SHA-256 digest", e);
}
}
private static final byte[] SALT = "Dy0IpHCjFnjNLST4rFRNN43GvzBqOcdbc9R5PkBk".getBytes(StandardCharsets.UTF_8);
private static final List<String> KEYWORDS = List.of(
"abstract",
"assert",
"boolean",
"break",
"byte",
"case",
"catch",
"char",
"class",
"const",
"continue",
"default",
"do",
"double",
"else",
"enum",
"extends",
"false",
"final",
"finally",
"float",
"for",
"goto",
"if",
"implements",
"import",
"instanceof",
"int",
"interface",
"long",
"native",
"new",
"null",
"package",
"private",
"protected",
"public",
"return",
"short",
"static",
"strictfp",
"super",
"switch",
"synchronized",
"this",
"throw",
"throws",
"transient",
"true",
"try",
"var",
"void",
"volatile",
"while",
"Exception",
"Error",
"Object",
"Number",
"Integer",
"Character",
"String",
"Math",
"Runtime",
"Throwable"
);
private static final Map<String, Byte> VALUES = IntStream.range(0, KEYWORDS.size())
.boxed()
.collect(Collectors.toMap(index -> KEYWORDS.get(index).toLowerCase(), Integer::byteValue));
}