Consolidate hex conversion functions

This commit is contained in:
Henrik Tunedal 2011-03-28 02:33:29 +02:00
parent 37732255b2
commit 33fa2debf9
3 changed files with 52 additions and 54 deletions

View File

@ -283,7 +283,8 @@ public class AppDetails extends ListActivity {
PackageInfo pi = pm.getPackageInfo(appid,
PackageManager.GET_SIGNATURES);
mInstalledSignature = pi.signatures[0];
Hasher hash = new Hasher("MD5", mInstalledSignature);
Hasher hash = new Hasher("MD5", mInstalledSignature
.toCharsString().getBytes());
mInstalledSigID = hash.getHash();
} catch (NameNotFoundException e) {
Log.d("FDroid", "Failed to get installed signature");

View File

@ -22,28 +22,25 @@ package org.fdroid.fdroid;
import java.io.File;
import java.io.FileInputStream;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import android.content.pm.Signature;
public class Hasher {
private MessageDigest digest;
private File f;
private Signature s;
private File file;
private byte[] array;
private String hashCache;
public Hasher(String type, File f) throws NoSuchAlgorithmException {
init(type);
this.f = f;
this.file = f;
}
public Hasher(String type, Signature s) throws NoSuchAlgorithmException {
public Hasher(String type, byte[] a) throws NoSuchAlgorithmException {
init(type);
this.s = s;
this.array = a;
}
private void init(String type) throws NoSuchAlgorithmException {
@ -61,40 +58,22 @@ public class Hasher {
public String getHash() {
if (hashCache != null)
return hashCache;
String hash = null;
else if (file != null) {
byte[] buffer = new byte[1024];
int read = 0;
try {
InputStream is;
if (s == null)
is = new FileInputStream(f);
else
is = new ByteArrayInputStream(s.toCharsString().getBytes());
InputStream is = new FileInputStream(file);
while ((read = is.read(buffer)) > 0) {
digest.update(buffer, 0, read);
}
byte[] checksum = digest.digest();
BigInteger bigInt = new BigInteger(1, checksum);
hash = bigInt.toString(16).toLowerCase();
// Add leading zeros
int targetLength = digest.getDigestLength() * 2;
if (hash.length() < targetLength) {
StringBuilder sb = new StringBuilder(targetLength);
for (int i = hash.length(); i < targetLength; i++) {
sb.append('0');
}
sb.append(hash);
hash = sb.toString();
}
is.close();
} catch (Exception e) {
return hashCache = "";
}
return hashCache = hash;
} else {
digest.update(array);
}
return hashCache = hex(digest.digest());
}
// Compare the calculated hash to another string, ignoring case,
@ -112,4 +91,34 @@ public class Hasher {
digest.reset();
}
public static String hex(byte[] sig) {
byte[] csig = new byte[sig.length * 2];
for (int j = 0; j < sig.length; j++) {
byte v = sig[j];
int d = (v >> 4) & 0xf;
csig[j * 2] = (byte) (d >= 10 ? ('a' + d - 10) : ('0' + d));
d = v & 0xf;
csig[j * 2 + 1] = (byte) (d >= 10 ? ('a' + d - 10) : ('0' + d));
}
return new String(csig);
}
public static byte[] unhex(String data) {
byte[] rawdata = new byte[data.length() / 2];
for (int i=0; i < data.length(); i++) {
char halfbyte = data.charAt(i);
int value;
if ('0' <= halfbyte && halfbyte <= '9')
value = halfbyte - '0';
else if ('a' <= halfbyte && halfbyte <= 'f')
value = halfbyte - 'a' + 10;
else if ('A' <= halfbyte && halfbyte <= 'F')
value = halfbyte - 'A' + 10;
else
throw new IllegalArgumentException("Bad hex digit");
rawdata[i/2] += (byte)(i % 2 == 0 ? value << 4 : value);
}
return rawdata;
}
}

View File

@ -307,20 +307,8 @@ public class RepoXMLHandler extends DefaultHandler {
boolean match = false;
for (Certificate cert : certs) {
byte[] sig = cert.getEncoded();
byte[] csig = new byte[sig.length * 2];
for (int j = 0; j < sig.length; j++) {
byte v = sig[j];
int d = (v >> 4) & 0xf;
csig[j * 2] = (byte) (d >= 10
? ('a' + d - 10)
: ('0' + d));
d = v & 0xf;
csig[j * 2 + 1] = (byte) (d >= 10
? ('a' + d - 10)
: ('0' + d));
}
if (repo.pubkey.equals(new String(csig))) {
String certdata = Hasher.hex(cert.getEncoded());
if (repo.pubkey.equals(certdata)) {
match = true;
break;
}