mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Refactor Utils.getSHA1 to produce SHA1 output instead of bool
This commit is contained in:
parent
70417d6a39
commit
d2982845ff
2 changed files with 43 additions and 2 deletions
|
|
@ -45,6 +45,43 @@ public class Utils {
|
||||||
|
|
||||||
private static final String TAG = Utils.class.getName();
|
private static final String TAG = Utils.class.getName();
|
||||||
|
|
||||||
|
public static String getSHA1(InputStream is) {
|
||||||
|
|
||||||
|
MessageDigest digest;
|
||||||
|
try {
|
||||||
|
digest = MessageDigest.getInstance("SHA1");
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
Log.e(TAG, "Exception while getting Digest", e);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] buffer = new byte[8192];
|
||||||
|
int read;
|
||||||
|
try {
|
||||||
|
while ((read = is.read(buffer)) > 0) {
|
||||||
|
digest.update(buffer, 0, read);
|
||||||
|
}
|
||||||
|
byte[] md5sum = digest.digest();
|
||||||
|
BigInteger bigInt = new BigInteger(1, md5sum);
|
||||||
|
String output = bigInt.toString(16);
|
||||||
|
// Fill to 40 chars
|
||||||
|
output = String.format("%40s", output).replace(' ', '0');
|
||||||
|
Log.i(TAG, "Generated: " + output);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.e(TAG, "IO Exception", e);
|
||||||
|
return "";
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
is.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.e(TAG, "Exception on closing MD5 input stream", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean testSHA1(String sha1, InputStream is) {
|
public static boolean testSHA1(String sha1, InputStream is) {
|
||||||
MessageDigest digest;
|
MessageDigest digest;
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -249,8 +249,12 @@ public class ShareActivity
|
||||||
|
|
||||||
//FIXME: Replace hardcoded string with call to Commons API instead (use TitleCategories.java as template)
|
//FIXME: Replace hardcoded string with call to Commons API instead (use TitleCategories.java as template)
|
||||||
// https://commons.wikimedia.org/w/api.php?action=query&list=allimages&aisha1=801957214aba50cb63bb6eb1b0effa50188900ba
|
// https://commons.wikimedia.org/w/api.php?action=query&list=allimages&aisha1=801957214aba50cb63bb6eb1b0effa50188900ba
|
||||||
boolean sha1Bool = Utils.testSHA1("801957214aba50cb63bb6eb1b0effa50188900ba", inputStream);
|
|
||||||
Log.d(TAG, "SHA1Bool returns " + sha1Bool);
|
String fileSHA1 = Utils.getSHA1(inputStream);
|
||||||
|
String debugSHA1 = "801957214aba50cb63bb6eb1b0effa50188900ba";
|
||||||
|
|
||||||
|
boolean fileSHA1Found = fileSHA1.equals(debugSHA1);
|
||||||
|
Log.d(TAG, "SHA1Bool returns " + fileSHA1Found);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.d(TAG, "IO Exception: ", e);
|
Log.d(TAG, "IO Exception: ", e);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue