/
AnuQuantumServiceHandler.java
78 lines (66 loc) · 2.62 KB
/
AnuQuantumServiceHandler.java
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
/*
* Copyright 2017 Patrick Favre-Bulle
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package at.favre.tools.dice.service.anuquantum;
import at.favre.lib.bytes.Bytes;
import at.favre.tools.dice.service.AServiceHandler;
import okhttp3.OkHttpClient;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import java.net.UnknownHostException;
/**
* ANU Quantum Random Numbers Server
* Quantum true random number generator from Australian university
* <p>
* See https://qrng.anu.edu.au/
*/
public final class AnuQuantumServiceHandler extends AServiceHandler {
static final int ENTROPY_SEED_LENGTH_BYTE = 24;
public AnuQuantumServiceHandler(boolean debug) {
super(debug);
}
@Override
public Result<AnuQuantumResponse> getRandom() {
long startTime = System.currentTimeMillis();
OkHttpClient client = createClient();
Exception error = null;
String errMsg = null;
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://qrng.anu.edu.au/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
AnuQuantumService service = retrofit.create(AnuQuantumService.class);
try {
Response<AnuQuantumResponse> response = service.getRandom(createHeaderMap(), ENTROPY_SEED_LENGTH_BYTE).execute();
if (response != null && response.isSuccessful() && response.body() != null) {
byte[] rawResponse = Bytes.parseHex(response.body().data.get(0)).array();
return new Result<>(getName(), rawResponse, response.body(), System.currentTimeMillis() - startTime);
}
} catch (UnknownHostException e) {
error = e;
errMsg = "Cannot resolve host. Is the device offline?";
} catch (Exception e) {
error = e;
errMsg = "Error during http request: " + e.getMessage();
}
return new Result<>(getName(), error, errMsg);
}
@Override
public String getName() {
return "ANU Quantum";
}
}