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, PackageInfo pi = pm.getPackageInfo(appid,
PackageManager.GET_SIGNATURES); PackageManager.GET_SIGNATURES);
mInstalledSignature = pi.signatures[0]; mInstalledSignature = pi.signatures[0];
Hasher hash = new Hasher("MD5", mInstalledSignature); Hasher hash = new Hasher("MD5", mInstalledSignature
.toCharsString().getBytes());
mInstalledSigID = hash.getHash(); mInstalledSigID = hash.getHash();
} catch (NameNotFoundException e) { } catch (NameNotFoundException e) {
Log.d("FDroid", "Failed to get installed signature"); 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.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.ByteArrayInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import android.content.pm.Signature;
public class Hasher { public class Hasher {
private MessageDigest digest; private MessageDigest digest;
private File f; private File file;
private Signature s; private byte[] array;
private String hashCache; private String hashCache;
public Hasher(String type, File f) throws NoSuchAlgorithmException { public Hasher(String type, File f) throws NoSuchAlgorithmException {
init(type); 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); init(type);
this.s = s; this.array = a;
} }
private void init(String type) throws NoSuchAlgorithmException { private void init(String type) throws NoSuchAlgorithmException {
@ -61,40 +58,22 @@ public class Hasher {
public String getHash() { public String getHash() {
if (hashCache != null) if (hashCache != null)
return hashCache; return hashCache;
else if (file != null) {
String hash = null;
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int read = 0; int read = 0;
try { try {
InputStream is; InputStream is = new FileInputStream(file);
if (s == null)
is = new FileInputStream(f);
else
is = new ByteArrayInputStream(s.toCharsString().getBytes());
while ((read = is.read(buffer)) > 0) { while ((read = is.read(buffer)) > 0) {
digest.update(buffer, 0, read); digest.update(buffer, 0, read);
} }
byte[] checksum = digest.digest(); is.close();
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();
}
} catch (Exception e) { } catch (Exception e) {
return hashCache = ""; return hashCache = "";
} }
} else {
return hashCache = hash; digest.update(array);
}
return hashCache = hex(digest.digest());
} }
// Compare the calculated hash to another string, ignoring case, // Compare the calculated hash to another string, ignoring case,
@ -112,4 +91,34 @@ public class Hasher {
digest.reset(); 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; boolean match = false;
for (Certificate cert : certs) { for (Certificate cert : certs) {
byte[] sig = cert.getEncoded(); String certdata = Hasher.hex(cert.getEncoded());
byte[] csig = new byte[sig.length * 2]; if (repo.pubkey.equals(certdata)) {
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))) {
match = true; match = true;
break; break;
} }