Skip to content

Commit 63af502

Browse files
committed
Extract hash functions to util
1 parent 7a02a45 commit 63af502

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

Adjust/adjust/src/main/java/com/adjust/sdk/DeviceInfo.java

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,15 @@
1111
import android.os.Build;
1212
import android.util.DisplayMetrics;
1313

14-
import java.math.BigInteger;
15-
import java.security.MessageDigest;
1614
import java.util.Locale;
1715
import java.util.Map;
1816

19-
import static com.adjust.sdk.Constants.ENCODING;
2017
import static com.adjust.sdk.Constants.HIGH;
2118
import static com.adjust.sdk.Constants.LARGE;
2219
import static com.adjust.sdk.Constants.LONG;
2320
import static com.adjust.sdk.Constants.LOW;
24-
import static com.adjust.sdk.Constants.MD5;
2521
import static com.adjust.sdk.Constants.MEDIUM;
2622
import static com.adjust.sdk.Constants.NORMAL;
27-
import static com.adjust.sdk.Constants.SHA1;
2823
import static com.adjust.sdk.Constants.SMALL;
2924
import static com.adjust.sdk.Constants.XLARGE;
3025

@@ -214,7 +209,7 @@ private String getMacSha1(String macAddress) {
214209
if (macAddress == null) {
215210
return null;
216211
}
217-
String macSha1 = sha1(macAddress);
212+
String macSha1 = Util.sha1(macAddress);
218213

219214
return macSha1;
220215
}
@@ -224,7 +219,7 @@ private String getMacShortMd5(String macAddress) {
224219
return null;
225220
}
226221
String macShort = macAddress.replaceAll(":", "");
227-
String macShortMd5 = md5(macShort);
222+
String macShortMd5 = Util.md5(macShort);
228223

229224
return macShortMd5;
230225
}
@@ -237,33 +232,6 @@ private String getAndroidId(Context context, boolean isGooglePlayServicesAvailab
237232
}
238233
}
239234

240-
private String sha1(final String text) {
241-
return hash(text, SHA1);
242-
}
243-
244-
private String md5(final String text) {
245-
return hash(text, MD5);
246-
}
247-
248-
private String hash(final String text, final String method) {
249-
String hashString = null;
250-
try {
251-
final byte[] bytes = text.getBytes(ENCODING);
252-
final MessageDigest mesd = MessageDigest.getInstance(method);
253-
mesd.update(bytes, 0, bytes.length);
254-
final byte[] hash = mesd.digest();
255-
hashString = convertToHex(hash);
256-
} catch (Exception e) {
257-
}
258-
return hashString;
259-
}
260-
261-
private static String convertToHex(final byte[] bytes) {
262-
final BigInteger bigInt = new BigInteger(1, bytes);
263-
final String formatString = "%0" + (bytes.length << 1) + "x";
264-
return String.format(Locale.US, formatString, bigInt);
265-
}
266-
267235
private String getFacebookAttributionId(final Context context) {
268236
try {
269237
final ContentResolver contentResolver = context.getContentResolver();

Adjust/adjust/src/main/java/com/adjust/sdk/Util.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@
3131
import java.io.NotSerializableException;
3232
import java.io.ObjectInputStream;
3333
import java.io.ObjectOutputStream;
34+
import java.math.BigInteger;
35+
import java.security.MessageDigest;
3436
import java.text.SimpleDateFormat;
3537
import java.util.Locale;
3638
import java.util.Map;
3739
import java.util.UUID;
3840
import java.util.regex.Matcher;
3941
import java.util.regex.Pattern;
4042

43+
import static com.adjust.sdk.Constants.ENCODING;
44+
import static com.adjust.sdk.Constants.MD5;
45+
import static com.adjust.sdk.Constants.SHA1;
46+
4147
/**
4248
* Collects utility functions used by Adjust.
4349
*/
@@ -322,4 +328,31 @@ public static int hashMap(Map value) {
322328
}
323329
return value.entrySet().hashCode();
324330
}
331+
332+
public static String sha1(final String text) {
333+
return hash(text, SHA1);
334+
}
335+
336+
public static String md5(final String text) {
337+
return hash(text, MD5);
338+
}
339+
340+
public static String hash(final String text, final String method) {
341+
String hashString = null;
342+
try {
343+
final byte[] bytes = text.getBytes(ENCODING);
344+
final MessageDigest mesd = MessageDigest.getInstance(method);
345+
mesd.update(bytes, 0, bytes.length);
346+
final byte[] hash = mesd.digest();
347+
hashString = convertToHex(hash);
348+
} catch (Exception e) {
349+
}
350+
return hashString;
351+
}
352+
353+
public static String convertToHex(final byte[] bytes) {
354+
final BigInteger bigInt = new BigInteger(1, bytes);
355+
final String formatString = "%0" + (bytes.length << 1) + "x";
356+
return String.format(Locale.US, formatString, bigInt);
357+
}
325358
}

0 commit comments

Comments
 (0)