Refactor Utils.getSHA1 to produce SHA1 output instead of bool

This commit is contained in:
misaochan 2016-12-14 16:05:23 +13:00
parent 70417d6a39
commit d2982845ff
2 changed files with 43 additions and 2 deletions

View file

@ -45,6 +45,43 @@ public class Utils {
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) {
MessageDigest digest;
try {