NekoX/TMessagesProj/src/main/java/org/telegram/messenger/Utilities.java

432 lines
16 KiB
Java
Raw Normal View History

2013-10-25 17:19:00 +02:00
/*
2015-05-21 23:27:27 +02:00
* This is the source code of Telegram for Android v. 2.x.x.
2013-10-25 17:19:00 +02:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2015-05-21 23:27:27 +02:00
* Copyright Nikolai Kudashov, 2013-2015.
2013-10-25 17:19:00 +02:00
*/
package org.telegram.messenger;
import android.content.Context;
2013-12-24 23:25:13 +01:00
import android.content.SharedPreferences;
2014-10-30 22:27:41 +01:00
import android.graphics.Bitmap;
2015-01-02 23:15:07 +01:00
import android.graphics.BitmapFactory;
2013-12-24 23:25:13 +01:00
import android.util.Base64;
2013-10-25 17:19:00 +02:00
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.PublicKey;
import java.security.SecureRandom;
2013-10-25 17:19:00 +02:00
import java.security.spec.RSAPublicKeySpec;
2013-12-24 23:25:13 +01:00
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2013-10-25 17:19:00 +02:00
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
2013-10-25 17:19:00 +02:00
import javax.crypto.Cipher;
public class Utilities {
2015-05-21 23:27:27 +02:00
public static Pattern pattern = Pattern.compile("[0-9]+");
public static SecureRandom random = new SecureRandom();
private static byte[] decompressBuffer;
private static ByteArrayOutputStreamExpand decompressStream;
2015-01-02 23:15:07 +01:00
public static ArrayList<String> goodPrimes = new ArrayList<>();
2013-12-23 00:47:35 +01:00
2013-10-25 17:19:00 +02:00
public static class TPFactorizedValue {
public long p, q;
}
public static volatile DispatchQueue stageQueue = new DispatchQueue("stageQueue");
public static volatile DispatchQueue globalQueue = new DispatchQueue("globalQueue");
2014-06-13 12:42:21 +02:00
public static volatile DispatchQueue searchQueue = new DispatchQueue("searchQueue");
public static volatile DispatchQueue phoneBookQueue = new DispatchQueue("photoBookQueue");
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
static {
try {
File URANDOM_FILE = new File("/dev/urandom");
FileInputStream sUrandomIn = new FileInputStream(URANDOM_FILE);
byte[] buffer = new byte[1024];
sUrandomIn.read(buffer);
sUrandomIn.close();
random.setSeed(buffer);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("primes", Context.MODE_PRIVATE);
String primes = preferences.getString("primes", null);
if (primes == null) {
goodPrimes.add("C71CAEB9C6B1C9048E6C522F70F13F73980D40238E3E21C14934D037563D930F48198A0AA7C14058229493D22530F4DBFA336F6E0AC925139543AED44CCE7C3720FD51F69458705AC68CD4FE6B6B13ABDC9746512969328454F18FAF8C595F642477FE96BB2A941D5BCD1D4AC8CC49880708FA9B378E3C4F3A9060BEE67CF9A4A4A695811051907E162753B56B0F6B410DBA74D8A84B2A14B3144E0EF1284754FD17ED950D5965B4B9DD46582DB1178D169C6BC465B0D6FF9CA3928FEF5B9AE4E418FC15E83EBEA0F87FA9FF5EED70050DED2849F47BF959D956850CE929851F0D8115F635B105EE2E4E15D04B2454BF6F4FADF034B10403119CD8E3B92FCC5B");
} else {
try {
byte[] bytes = Base64.decode(primes, Base64.DEFAULT);
if (bytes != null) {
SerializedData data = new SerializedData(bytes);
int count = data.readInt32(false);
for (int a = 0; a < count; a++) {
goodPrimes.add(data.readString(false));
}
2015-02-27 20:57:58 +01:00
data.cleanup();
}
} catch (Exception e) {
FileLog.e("tmessages", e);
goodPrimes.clear();
goodPrimes.add("C71CAEB9C6B1C9048E6C522F70F13F73980D40238E3E21C14934D037563D930F48198A0AA7C14058229493D22530F4DBFA336F6E0AC925139543AED44CCE7C3720FD51F69458705AC68CD4FE6B6B13ABDC9746512969328454F18FAF8C595F642477FE96BB2A941D5BCD1D4AC8CC49880708FA9B378E3C4F3A9060BEE67CF9A4A4A695811051907E162753B56B0F6B410DBA74D8A84B2A14B3144E0EF1284754FD17ED950D5965B4B9DD46582DB1178D169C6BC465B0D6FF9CA3928FEF5B9AE4E418FC15E83EBEA0F87FA9FF5EED70050DED2849F47BF959D956850CE929851F0D8115F635B105EE2E4E15D04B2454BF6F4FADF034B10403119CD8E3B92FCC5B");
}
}
}
2013-10-25 17:19:00 +02:00
public native static long doPQNative(long _what);
2015-05-21 23:27:27 +02:00
2014-11-19 02:23:46 +01:00
public native static void loadBitmap(String path, Bitmap bitmap, int scale, int width, int height, int stride);
2015-05-21 23:27:27 +02:00
2015-04-09 20:00:14 +02:00
public native static int pinBitmap(Bitmap bitmap);
2015-05-21 23:27:27 +02:00
public native static void blurBitmap(Object bitmap, int radius, int unpin);
2015-05-21 23:27:27 +02:00
public native static void calcCDT(ByteBuffer hsvBuffer, int width, int height, ByteBuffer buffer);
2015-05-21 23:27:27 +02:00
2015-01-02 23:15:07 +01:00
public native static Bitmap loadWebpImage(ByteBuffer buffer, int len, BitmapFactory.Options options);
2015-05-21 23:27:27 +02:00
public native static int convertVideoFrame(ByteBuffer src, ByteBuffer dest, int destFormat, int width, int height, int padding, int swap);
2015-05-21 23:27:27 +02:00
private native static void aesIgeEncryption(ByteBuffer buffer, byte[] key, byte[] iv, boolean encrypt, int offset, int length);
2013-10-25 17:19:00 +02:00
public static void aesIgeEncryption(ByteBuffer buffer, byte[] key, byte[] iv, boolean encrypt, boolean changeIv, int offset, int length) {
2014-12-04 21:27:06 +01:00
aesIgeEncryption(buffer, key, changeIv ? iv : iv.clone(), encrypt, offset, length);
}
public static Integer parseInt(String value) {
if (value == null) {
return 0;
}
Integer val = 0;
try {
Matcher matcher = pattern.matcher(value);
if (matcher.find()) {
String num = matcher.group(0);
val = Integer.parseInt(num);
}
} catch (Exception e) {
FileLog.e("tmessages", e);
}
return val;
}
public static String parseIntToString(String value) {
Matcher matcher = pattern.matcher(value);
if (matcher.find()) {
return matcher.group(0);
}
return null;
}
2013-12-23 00:47:35 +01:00
public static String bytesToHex(byte[] bytes) {
2015-04-09 20:00:14 +02:00
if (bytes == null) {
return "";
}
2013-12-23 00:47:35 +01:00
char[] hexChars = new char[bytes.length * 2];
int v;
for (int j = 0; j < bytes.length; j++) {
2013-12-23 00:47:35 +01:00
v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public static byte[] hexToBytes(String hex) {
2015-04-09 20:00:14 +02:00
if (hex == null) {
return null;
}
int len = hex.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16));
}
return data;
}
2013-12-23 12:05:57 +01:00
public static boolean isGoodPrime(byte[] prime, int g) {
if (!(g >= 2 && g <= 7)) {
return false;
}
2013-12-24 23:25:13 +01:00
if (prime.length != 256 || prime[0] >= 0) {
return false;
}
BigInteger dhBI = new BigInteger(1, prime);
if (g == 2) { // p mod 8 = 7 for g = 2;
BigInteger res = dhBI.mod(BigInteger.valueOf(8));
if (res.intValue() != 7) {
return false;
}
} else if (g == 3) { // p mod 3 = 2 for g = 3;
BigInteger res = dhBI.mod(BigInteger.valueOf(3));
if (res.intValue() != 2) {
return false;
}
} else if (g == 5) { // p mod 5 = 1 or 4 for g = 5;
BigInteger res = dhBI.mod(BigInteger.valueOf(5));
int val = res.intValue();
if (val != 1 && val != 4) {
return false;
}
} else if (g == 6) { // p mod 24 = 19 or 23 for g = 6;
BigInteger res = dhBI.mod(BigInteger.valueOf(24));
int val = res.intValue();
if (val != 19 && val != 23) {
return false;
}
} else if (g == 7) { // p mod 7 = 3, 5 or 6 for g = 7.
BigInteger res = dhBI.mod(BigInteger.valueOf(7));
int val = res.intValue();
if (val != 3 && val != 5 && val != 6) {
return false;
}
2013-12-23 00:47:35 +01:00
}
String hex = bytesToHex(prime);
for (String cached : goodPrimes) {
if (cached.equals(hex)) {
return true;
}
}
2013-12-23 00:47:35 +01:00
BigInteger dhBI2 = dhBI.subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(2));
if (!dhBI.isProbablePrime(30) || !dhBI2.isProbablePrime(30)) {
return false;
}
2013-12-24 23:25:13 +01:00
goodPrimes.add(hex);
globalQueue.postRunnable(new Runnable() {
@Override
public void run() {
try {
SerializedData data = new SerializedData();
data.writeInt32(goodPrimes.size());
for (String pr : goodPrimes) {
data.writeString(pr);
}
byte[] bytes = data.toByteArray();
2015-02-27 20:57:58 +01:00
data.cleanup();
2013-12-24 23:25:13 +01:00
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("primes", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("primes", Base64.encodeToString(bytes, Base64.DEFAULT));
editor.commit();
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
});
2013-12-23 00:47:35 +01:00
return true;
}
2013-12-23 12:54:31 +01:00
public static boolean isGoodGaAndGb(BigInteger g_a, BigInteger p) {
return !(g_a.compareTo(BigInteger.valueOf(1)) != 1 || g_a.compareTo(p.subtract(BigInteger.valueOf(1))) != -1);
2013-12-23 12:05:57 +01:00
}
2013-10-25 17:19:00 +02:00
public static TPFactorizedValue getFactorizedValue(long what) {
long g = doPQNative(what);
if (g > 1 && g < what) {
long p1 = g;
long p2 = what / g;
if (p1 > p2) {
long tmp = p1;
p1 = p2;
p2 = tmp;
}
TPFactorizedValue result = new TPFactorizedValue();
result.p = p1;
result.q = p2;
return result;
} else {
2013-12-20 20:25:49 +01:00
FileLog.e("tmessages", String.format("**** Factorization failed for %d", what));
2013-10-25 17:19:00 +02:00
TPFactorizedValue result = new TPFactorizedValue();
result.p = 0;
result.q = 0;
return result;
}
}
public static boolean arraysEquals(byte[] arr1, int offset1, byte[] arr2, int offset2) {
2015-06-29 19:12:11 +02:00
if (arr1 == null || arr2 == null || offset1 < 0 || offset2 < 0 || arr1.length - offset1 != arr2.length - offset2 || arr1.length - offset1 < 0 || arr2.length - offset2 < 0) {
return false;
}
2015-05-21 23:27:27 +02:00
boolean result = true;
for (int a = offset1; a < arr1.length; a++) {
if (arr1[a + offset1] != arr2[a + offset2]) {
2015-05-21 23:27:27 +02:00
result = false;
}
}
2015-05-21 23:27:27 +02:00
return result;
}
2013-12-24 23:25:13 +01:00
public static byte[] computeSHA1(byte[] convertme, int offset, int len) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(convertme, offset, len);
return md.digest();
} catch (Exception e) {
FileLog.e("tmessages", e);
}
return null;
}
public static byte[] computeSHA1(ByteBuffer convertme, int offset, int len) {
int oldp = convertme.position();
int oldl = convertme.limit();
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
convertme.position(offset);
convertme.limit(len);
md.update(convertme);
return md.digest();
} catch (Exception e) {
FileLog.e("tmessages", e);
} finally {
convertme.limit(oldl);
convertme.position(oldp);
}
2015-05-21 23:27:27 +02:00
return new byte[0];
}
public static byte[] computeSHA1(ByteBuffer convertme) {
return computeSHA1(convertme, 0, convertme.limit());
}
2013-10-25 17:19:00 +02:00
public static byte[] computeSHA1(byte[] convertme) {
return computeSHA1(convertme, 0, convertme.length);
2013-10-25 17:19:00 +02:00
}
2015-01-02 23:15:07 +01:00
public static byte[] computeSHA256(byte[] convertme, int offset, int len) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(convertme, offset, len);
return md.digest();
} catch (Exception e) {
FileLog.e("tmessages", e);
}
return null;
}
2013-10-25 17:19:00 +02:00
public static byte[] encryptWithRSA(BigInteger[] key, byte[] data) {
try {
KeyFactory fact = KeyFactory.getInstance("RSA");
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(key[0], key[1]);
PublicKey publicKey = fact.generatePublic(keySpec);
final Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
} catch (Exception e) {
2013-12-20 20:25:49 +01:00
FileLog.e("tmessages", e);
2013-10-25 17:19:00 +02:00
}
return null;
}
public static long bytesToLong(byte[] bytes) {
return ((long) bytes[7] << 56) + (((long) bytes[6] & 0xFF) << 48) + (((long) bytes[5] & 0xFF) << 40) + (((long) bytes[4] & 0xFF) << 32)
+ (((long) bytes[3] & 0xFF) << 24) + (((long) bytes[2] & 0xFF) << 16) + (((long) bytes[1] & 0xFF) << 8) + ((long) bytes[0] & 0xFF);
2013-10-25 17:19:00 +02:00
}
public static TLObject decompress(byte[] data, TLObject parentObject, boolean exception) {
final int BUFFER_SIZE = 16384;
2013-10-25 17:19:00 +02:00
ByteArrayInputStream is = new ByteArrayInputStream(data);
GZIPInputStream gis;
SerializedData stream = null;
2013-10-25 17:19:00 +02:00
try {
if (decompressBuffer == null) {
decompressBuffer = new byte[BUFFER_SIZE];
decompressStream = new ByteArrayOutputStreamExpand(BUFFER_SIZE);
}
decompressStream.reset();
2013-10-25 17:19:00 +02:00
gis = new GZIPInputStream(is, BUFFER_SIZE);
int bytesRead;
while ((bytesRead = gis.read(decompressBuffer)) != -1) {
decompressStream.write(decompressBuffer, 0, bytesRead);
2013-10-25 17:19:00 +02:00
}
2015-02-27 20:57:58 +01:00
try {
gis.close();
} catch (Exception e) {
FileLog.e("tmessages", e);
}
try {
is.close();
} catch (Exception e) {
FileLog.e("tmessages", e);
}
stream = new SerializedData(decompressStream.toByteArray());
2013-10-25 17:19:00 +02:00
} catch (IOException e) {
2013-12-20 20:25:49 +01:00
FileLog.e("tmessages", e);
2013-10-25 17:19:00 +02:00
}
if (stream != null) {
TLObject object = ConnectionsManager.getInstance().deserialize(parentObject, stream, exception);
stream.cleanup();
return object;
}
2013-10-25 17:19:00 +02:00
return null;
}
public static byte[] compress(byte[] data) {
if (data == null) {
return null;
}
byte[] packedData = null;
ByteArrayOutputStream bytesStream = new ByteArrayOutputStream();
try {
GZIPOutputStream zip = new GZIPOutputStream(bytesStream);
zip.write(data);
zip.close();
packedData = bytesStream.toByteArray();
} catch (IOException e) {
FileLog.e("tmessages", e);
2015-02-27 20:57:58 +01:00
} finally {
try {
bytesStream.close();
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
return packedData;
}
2013-10-25 17:19:00 +02:00
public static String MD5(String md5) {
if (md5 == null) {
return null;
}
2013-10-25 17:19:00 +02:00
try {
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
byte[] array = md.digest(md5.getBytes());
StringBuilder sb = new StringBuilder();
for (byte anArray : array) {
sb.append(Integer.toHexString((anArray & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString();
} catch (java.security.NoSuchAlgorithmException e) {
2013-12-20 20:25:49 +01:00
FileLog.e("tmessages", e);
2013-10-25 17:19:00 +02:00
}
return null;
}
}