Debian Codegraph @

src/main/java/com/akdeniz/googleplaycrawler/Utils.java

raw
1 package com.akdeniz.googleplaycrawler;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.math.BigInteger;
7 import java.security.KeyFactory;
8 import java.security.KeyManagementException;
9 import java.security.MessageDigest;
10 import java.security.NoSuchAlgorithmException;
11 import java.security.PublicKey;
12 import java.security.spec.RSAPublicKeySpec;
13 import java.util.Arrays;
14 import java.util.Date;
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.StringTokenizer;
18
19 import javax.crypto.Cipher;
20 import javax.net.ssl.SSLContext;
21 import javax.net.ssl.TrustManager;
22
23 import org.apache.http.conn.scheme.Scheme;
24 import org.apache.http.conn.ssl.SSLSocketFactory;
25
26 import com.akdeniz.googleplaycrawler.GooglePlay.AndroidBuildProto;
27 import com.akdeniz.googleplaycrawler.GooglePlay.AndroidCheckinProto;
28 import com.akdeniz.googleplaycrawler.GooglePlay.AndroidCheckinRequest;
29 import com.akdeniz.googleplaycrawler.GooglePlay.DeviceConfigurationProto;
30 import com.akdeniz.googleplaycrawler.misc.Base64;
31 import com.akdeniz.googleplaycrawler.misc.DummyX509TrustManager;
32
33 /**
34 *
35 * @author akdeniz
36 *
37 */
38 public class Utils {
39
40 private static final String GOOGLE_PUBLIC_KEY = "AAAAgMom/1a/v0lblO2Ubrt60J2gcuXSljGFQXgcyZWveWLEwo6prwgi3"
41 + "iJIZdodyhKZQrNWp5nKJ3srRXcUW+F1BD3baEVGcmEgqaLZUNBjm057pKRI16kB0YppeGx5qIQ5QjKzsR8ETQbKLNWgRY0Q"
42 + "RNVz34kMJR3P/LgHax/6rmf5AAAAAwEAAQ==";
43
44 /**
45 * Parses key-value response into map.
46 */
47 public static Map<String, String> parseResponse(String response) {
48
49 Map<String, String> keyValueMap = new HashMap<String, String>();
50 StringTokenizer st = new StringTokenizer(response, "\n\r");
51
52 while (st.hasMoreTokens()) {
53 String[] keyValue = st.nextToken().split("=");
54 keyValueMap.put(keyValue[0], keyValue[1]);
55 }
56
57 return keyValueMap;
58 }
59
60 private static PublicKey createKey(byte[] keyByteArray) throws Exception {
61
62 int modulusLength = readInt(keyByteArray, 0);
63 byte[] modulusByteArray = new byte[modulusLength];
64 System.arraycopy(keyByteArray, 4, modulusByteArray, 0, modulusLength);
65 BigInteger modulus = new BigInteger(1, modulusByteArray);
66
67 int exponentLength = readInt(keyByteArray, modulusLength + 4);
68 byte[] exponentByteArray = new byte[exponentLength];
69 System.arraycopy(keyByteArray, modulusLength + 8, exponentByteArray, 0, exponentLength);
70 BigInteger publicExponent = new BigInteger(1, exponentByteArray);
71
72 return KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(modulus, publicExponent));
73 }
74
75 /**
76 * Encrypts given string with Google Public Key.
77 *
78 */
79 public static String encryptString(String str2Encrypt) throws Exception {
80
81 byte[] keyByteArray = Base64.decode(GOOGLE_PUBLIC_KEY, Base64.DEFAULT);
82
83 byte[] header = new byte[5];
84 byte[] digest = MessageDigest.getInstance("SHA-1").digest(keyByteArray);
85 header[0] = 0;
86 System.arraycopy(digest, 0, header, 1, 4);
87
88 PublicKey publicKey = createKey(keyByteArray);
89
90 Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA1ANDMGF1PADDING");
91 byte[] bytes2Encrypt = str2Encrypt.getBytes("UTF-8");
92 int len = ((bytes2Encrypt.length - 1) / 86) + 1;
93 byte[] cryptedBytes = new byte[len * 133];
94
95 for (int j = 0; j < len; j++) {
96 cipher.init(1, publicKey);
97 byte[] arrayOfByte4 = cipher.doFinal(bytes2Encrypt, j * 86, (bytes2Encrypt.length - j * 86));
98 System.arraycopy(header, 0, cryptedBytes, j * 133, header.length);
99 System.arraycopy(arrayOfByte4, 0, cryptedBytes, j * 133 + header.length, arrayOfByte4.length);
100 }
101 return Base64.encodeToString(cryptedBytes, 10);
102 }
103
104 private static int readInt(byte[] data, int offset) {
105 return (0xFF & data[offset]) << 24 | (0xFF & data[(offset + 1)]) << 16 | (0xFF & data[(offset + 2)]) << 8
106 | (0xFF & data[(offset + 3)]);
107 }
108
109 /**
110 * Reads all contents of the input stream.
111 *
112 */
113 public static byte[] readAll(InputStream inputStream) throws IOException {
114
115 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
116 byte[] buffer = new byte[1024];
117
118 int k = 0;
119 for (; (k = inputStream.read(buffer)) != -1;) {
120 outputStream.write(buffer, 0, k);
121 }
122
123 return outputStream.toByteArray();
124 }
125
126 public static String bytesToHex(byte[] bytes) {
127 final char[] hexArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
128 char[] hexChars = new char[bytes.length * 2];
129 int v;
130 for (int j = 0; j < bytes.length; j++) {
131 v = bytes[j] & 0xFF;
132 hexChars[j * 2] = hexArray[v >>> 4];
133 hexChars[j * 2 + 1] = hexArray[v & 0x0F];
134 }
135 return new String(hexChars);
136 }
137
138 public static byte[] hexToBytes(String s) {
139 int len = s.length();
140 byte[] data = new byte[len / 2];
141 for (int i = 0; i < len; i += 2) {
142 data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
143 }
144 return data;
145 }
146
147 public static Scheme getMockedScheme() throws NoSuchAlgorithmException, KeyManagementException {
148 SSLContext sslcontext = SSLContext.getInstance("TLS");
149
150 sslcontext.init(null, new TrustManager[] { new DummyX509TrustManager() }, null);
151 SSLSocketFactory sf = new SSLSocketFactory(sslcontext);
152 Scheme https = new Scheme("https", 443, sf);
153
154 return https;
155 }
156
157 /**
158 * Generates android checkin request with properties of "Galaxy S3".
159 *
160 * <a href=
161 * "http://www.glbenchmark.com/phonedetails.jsp?benchmark=glpro25&D=Samsung+GT-I9300+Galaxy+S+III&testgroup=system"
162 * > http://www.glbenchmark.com/phonedetails.jsp?benchmark=glpro25&D=Samsung
163 * +GT-I9300+Galaxy+S+III&testgroup=system </a>
164 */
165 public static AndroidCheckinRequest generateAndroidCheckinRequest() {
166
167 return AndroidCheckinRequest
168 .newBuilder()
169 .setId(0)
170 .setCheckin(
171 AndroidCheckinProto
172 .newBuilder()
173 .setBuild(
174 AndroidBuildProto.newBuilder()
175 .setId("samsung/m0xx/m0:4.0.4/IMM76D/I9300XXALF2:user/release-keys")
176 .setProduct("smdk4x12").setCarrier("Google").setRadio("I9300XXALF2")
177 .setBootloader("PRIMELA03").setClient("android-google")
178 .setTimestamp(new Date().getTime() / 1000).setGoogleServices(16).setDevice("m0")
179 .setSdkVersion(21).setModel("GT-I9300").setManufacturer("Samsung")
180 .setBuildProduct("m0xx").setOtaInstalled(false)).setLastCheckinMsec(0)
181 .setCellOperator("310260").setSimOperator("310260").setRoaming("mobile-notroaming")
182 .setUserNumber(0)).setLocale("en_US").setTimeZone("Europe/Istanbul").setVersion(3)
183 .setDeviceConfiguration(getDeviceConfigurationProto()).setFragment(0).build();
184 }
185
186 public static DeviceConfigurationProto getDeviceConfigurationProto() {
187 return DeviceConfigurationProto
188 .newBuilder()
189 .setTouchScreen(3)
190 .setKeyboard(1)
191 .setNavigation(1)
192 .setScreenLayout(2)
193 .setHasHardKeyboard(false)
194 .setHasFiveWayNavigation(false)
195 .setScreenDensity(320)
196 .setGlEsVersion(131072)
197 .addAllSystemSharedLibrary(
198 Arrays.asList("android.test.runner", "com.android.future.usb.accessory", "com.android.location.provider",
199 "com.android.nfc_extras", "com.google.android.maps", "com.google.android.media.effects",
200 "com.google.widevine.software.drm", "javax.obex"))
201 .addAllSystemAvailableFeature(
202 Arrays.asList("android.hardware.bluetooth", "android.hardware.camera",
203 "android.hardware.camera.autofocus", "android.hardware.camera.flash",
204 "android.hardware.camera.front", "android.hardware.faketouch", "android.hardware.location",
205 "android.hardware.location.gps", "android.hardware.location.network",
206 "android.hardware.microphone", "android.hardware.nfc", "android.hardware.screen.landscape",
207 "android.hardware.screen.portrait", "android.hardware.sensor.accelerometer",
208 "android.hardware.sensor.barometer", "android.hardware.sensor.compass",
209 "android.hardware.sensor.gyroscope", "android.hardware.sensor.light",
210 "android.hardware.sensor.proximity", "android.hardware.telephony",
211 "android.hardware.telephony.gsm", "android.hardware.touchscreen",
212 "android.hardware.touchscreen.multitouch", "android.hardware.touchscreen.multitouch.distinct",
213 "android.hardware.touchscreen.multitouch.jazzhand", "android.hardware.usb.accessory",
214 "android.hardware.usb.host", "android.hardware.wifi", "android.hardware.wifi.direct",
215 "android.software.live_wallpaper", "android.software.sip", "android.software.sip.voip",
216 "com.cyanogenmod.android", "com.cyanogenmod.nfc.enhanced",
217 "com.google.android.feature.GOOGLE_BUILD", "com.nxp.mifare", "com.tmobile.software.themes"))
218 .addAllNativePlatform(Arrays.asList("armeabi-v7a", "armeabi"))
219 .setScreenWidth(720)
220 .setScreenHeight(1184)
221 .addAllSystemSupportedLocale(
222 Arrays.asList("af", "af_ZA", "am", "am_ET", "ar", "ar_EG", "bg", "bg_BG", "ca", "ca_ES", "cs", "cs_CZ",
223 "da", "da_DK", "de", "de_AT", "de_CH", "de_DE", "de_LI", "el", "el_GR", "en", "en_AU", "en_CA",
224 "en_GB", "en_NZ", "en_SG", "en_US", "es", "es_ES", "es_US", "fa", "fa_IR", "fi", "fi_FI", "fr",
225 "fr_BE", "fr_CA", "fr_CH", "fr_FR", "hi", "hi_IN", "hr", "hr_HR", "hu", "hu_HU", "in", "in_ID",
226 "it", "it_CH", "it_IT", "iw", "iw_IL", "ja", "ja_JP", "ko", "ko_KR", "lt", "lt_LT", "lv",
227 "lv_LV", "ms", "ms_MY", "nb", "nb_NO", "nl", "nl_BE", "nl_NL", "pl", "pl_PL", "pt", "pt_BR",
228 "pt_PT", "rm", "rm_CH", "ro", "ro_RO", "ru", "ru_RU", "sk", "sk_SK", "sl", "sl_SI", "sr",
229 "sr_RS", "sv", "sv_SE", "sw", "sw_TZ", "th", "th_TH", "tl", "tl_PH", "tr", "tr_TR", "ug",
230 "ug_CN", "uk", "uk_UA", "vi", "vi_VN", "zh_CN", "zh_TW", "zu", "zu_ZA"))
231 .addAllGlExtension(
232 Arrays.asList("GL_EXT_debug_marker", "GL_EXT_discard_framebuffer", "GL_EXT_multi_draw_arrays",
233 "GL_EXT_shader_texture_lod", "GL_EXT_texture_format_BGRA8888",
234 "GL_IMG_multisampled_render_to_texture", "GL_IMG_program_binary", "GL_IMG_read_format",
235 "GL_IMG_shader_binary", "GL_IMG_texture_compression_pvrtc", "GL_IMG_texture_format_BGRA8888",
236 "GL_IMG_texture_npot", "GL_IMG_vertex_array_object", "GL_OES_EGL_image",
237 "GL_OES_EGL_image_external", "GL_OES_blend_equation_separate", "GL_OES_blend_func_separate",
238 "GL_OES_blend_subtract", "GL_OES_byte_coordinates", "GL_OES_compressed_ETC1_RGB8_texture",
239 "GL_OES_compressed_paletted_texture", "GL_OES_depth24", "GL_OES_depth_texture",
240 "GL_OES_draw_texture", "GL_OES_egl_sync", "GL_OES_element_index_uint",
241 "GL_OES_extended_matrix_palette", "GL_OES_fixed_point", "GL_OES_fragment_precision_high",
242 "GL_OES_framebuffer_object", "GL_OES_get_program_binary", "GL_OES_mapbuffer",
243 "GL_OES_matrix_get", "GL_OES_matrix_palette", "GL_OES_packed_depth_stencil",
244 "GL_OES_point_size_array", "GL_OES_point_sprite", "GL_OES_query_matrix", "GL_OES_read_format",
245 "GL_OES_required_internalformat", "GL_OES_rgb8_rgba8", "GL_OES_single_precision",
246 "GL_OES_standard_derivatives", "GL_OES_stencil8", "GL_OES_stencil_wrap",
247 "GL_OES_texture_cube_map", "GL_OES_texture_env_crossbar", "GL_OES_texture_float",
248 "GL_OES_texture_half_float", "GL_OES_texture_mirrored_repeat", "GL_OES_vertex_array_object",
249 "GL_OES_vertex_half_float")).build();
250 }
251 }