Performance of SHA-1 Checksum from Android 2.2 to 2.3 and Higher
- by sbrichards
In testing the performance of:
package com.srichards.sha;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import com.srichards.sha.R;
public class SHAHashActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = new TextView(this);
String shaVal = this.getString(R.string.sha);
long systimeBefore = System.currentTimeMillis();
String result = shaCheck(shaVal);
long systimeResult = System.currentTimeMillis() - systimeBefore;
tv.setText("\nRunTime: " + systimeResult + "\nHas been modified? | Hash Value: " + result);
setContentView(tv);
}
public String shaCheck(String shaVal){
try{
String resultant = "null";
MessageDigest digest = MessageDigest.getInstance("SHA1");
ZipFile zf = null;
try {
zf = new ZipFile("/data/app/com.blah.android-1.apk"); // /data/app/com.blah.android-2.apk
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ZipEntry ze = zf.getEntry("classes.dex");
InputStream file = zf.getInputStream(ze);
byte[] dataBytes = new byte[32768]; //65536 32768
int nread = 0;
while ((nread = file.read(dataBytes)) != -1) {
digest.update(dataBytes, 0, nread);
}
byte [] rbytes = digest.digest();
StringBuffer sb = new StringBuffer("");
for (int i = 0; i< rbytes.length; i++)
{
sb.append(Integer.toString((rbytes[i] & 0xff) + 0x100, 16).substring(1));
}
if (shaVal.equals(sb.toString()))
{
resultant = ("\nFalse : " + "\nFound:\n" + sb.toString() + "|" + "\nHave:\n" + shaVal);
}
else
{
resultant = ("\nTrue : " + "\nFound:\n" + sb.toString() + "|" + "\nHave:\n" + shaVal);
}
return resultant;
}
catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
}
On a 2.2 Device I get average runtime of ~350ms, while on newer devices I get runtimes of 26-50ms which is substantially lower. I'm keeping in mind these devices are newer and have better hardware but am also wondering if the platform and the implementation affect performance much and if there is anything that could reduce runtimes on 2.2 devices. Note, the classes.dex of the .apk being accessed is roughly 4MB.
Thanks!