use of com.pokegoapi.util.hash.crypto.Crypto in project PokeGOAPI-Java by Grover-c13.
the class Signature method setSignature.
/**
* Given a fully built request, set the signature correctly.
*
* @param api the api
* @param builder the RequestEnvelope builder
* @throws RequestFailedException if an invalid request is sent
*/
public static void setSignature(PokemonGo api, RequestEnvelope.Builder builder) throws RequestFailedException {
boolean usePtr8 = false;
byte[][] requestData = new byte[builder.getRequestsCount()][];
for (int i = 0; i < builder.getRequestsCount(); i++) {
requestData[i] = builder.getRequests(i).toByteArray();
RequestType requestType = builder.getRequests(i).getRequestType();
if (requestType == RequestType.GET_PLAYER) {
usePtr8 |= api.isFirstGP();
api.setFirstGP(false);
} else if (requestType == RequestType.GET_MAP_OBJECTS) {
usePtr8 |= !api.isFirstGMO();
api.setFirstGMO(false);
}
}
double latitude = api.getLatitude();
double longitude = api.getLongitude();
double accuracy = api.getAccuracy();
if (Double.isNaN(latitude)) {
latitude = 0.0;
}
if (Double.isNaN(longitude)) {
longitude = 0.0;
}
if (Double.isNaN(accuracy)) {
accuracy = 0.0;
}
byte[] authTicket;
if (builder.hasAuthTicket()) {
authTicket = builder.getAuthTicket().toByteArray();
} else {
authTicket = builder.getAuthInfo().toByteArray();
}
long currentTimeMillis = api.currentTimeMillis();
byte[] sessionHash = api.getSessionHash();
HashProvider provider = api.getHashProvider();
Hash hash = provider.provide(currentTimeMillis, latitude, longitude, accuracy, authTicket, sessionHash, requestData);
long timeSinceStart = currentTimeMillis - api.getStartTime();
SignatureOuterClass.Signature.Builder signatureBuilder = SignatureOuterClass.Signature.newBuilder().setLocationHash1(hash.getLocationAuthHash()).setLocationHash2(hash.getLocationHash()).setSessionHash(ByteString.copyFrom(sessionHash)).setTimestamp(currentTimeMillis).setTimestampSinceStart(timeSinceStart).setDeviceInfo(api.getDeviceInfo()).addAllLocationFix(LocationFixes.getDefault(api, builder, currentTimeMillis, RANDOM)).setActivityStatus(api.getActivitySignature(RANDOM)).setUnknown25(provider.getUNK25());
final SignatureOuterClass.Signature.SensorInfo sensorInfo = SensorInfo.getDefault(api, currentTimeMillis, RANDOM);
if (sensorInfo != null)
signatureBuilder.addSensorInfo(sensorInfo);
List<Long> requestHashes = hash.getRequestHashes();
for (int i = 0; i < builder.getRequestsCount(); i++) signatureBuilder.addRequestHash(requestHashes.get(i));
Crypto crypto = provider.getCrypto();
SignatureOuterClass.Signature signature = signatureBuilder.build();
byte[] signatureByteArray = signature.toByteArray();
byte[] encrypted = crypto.encrypt(signatureByteArray, timeSinceStart).toByteBuffer().array();
ByteString signatureBytes = SendEncryptedSignatureRequest.newBuilder().setEncryptedSignature(ByteString.copyFrom(encrypted)).build().toByteString();
RequestEnvelope.PlatformRequest signatureRequest = RequestEnvelope.PlatformRequest.newBuilder().setType(PlatformRequestType.SEND_ENCRYPTED_SIGNATURE).setRequestMessage(signatureBytes).build();
builder.addPlatformRequests(signatureRequest);
if (usePtr8) {
ByteString ptr8 = UnknownPtr8RequestOuterClass.UnknownPtr8Request.newBuilder().setMessage("90f6a704505bccac73cec99b07794993e6fd5a12").build().toByteString();
builder.addPlatformRequests(RequestEnvelope.PlatformRequest.newBuilder().setType(PlatformRequestType.UNKNOWN_PTR_8).setRequestMessage(ptr8).build());
}
}
Aggregations