Skip to content

Instantly share code, notes, and snippets.

@ssmereka
Created October 11, 2013 17:49
Show Gist options
  • Save ssmereka/6939066 to your computer and use it in GitHub Desktop.
Save ssmereka/6939066 to your computer and use it in GitHub Desktop.
Get SHA1 digital footprint from android application based on its package name.
public static final String tag = "Security";
private static final boolean debug = true;
/**
* Gets an application's SHA1 footprint(s) from their digital signature(s).
* @param packageName is the package name of the application you want to retrieve
* the SHA1 footprints from.
* @return a list of SHA1 footprints or null.
*/
private ArrayList<String> getFootprints(String packageName) {
Signature[] sigs = null;
ArrayList<String> footprints = new ArrayList<String>();
// Get the application's digital signatures.
try {
sigs = getBaseContext().getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES).signatures;
} catch(Exception ex) {
if(debug) {
Log.e(tag, ex.toString());
}
}
// If there was an error then return nothing.
if(sigs == null) {
return null;
}
// Extract the SHA1 footprint from each signature.
for(Signature sig : sigs) {
byte[] hexBytes = sig.toByteArray();
MessageDigest digest = null;
// Get the SHA1 footprint
try {
digest = MessageDigest.getInstance("SHA1");
} catch(Exception ex) {
if(debug) {
Log.e(tag, ex.toString());
}
}
// Check for a valid digest
if(digest == null) {
return null;
}
byte[] sha1digest = new byte[0];
if(digest != null) {
sha1digest = digest.digest(hexBytes);
StringBuilder sb = new StringBuilder();
for(int i = 0; i < sha1digest.length; ++i) {
sb.append((Integer.toHexString((sha1digest[i] & 0xFF) | 0x100)).substring(1, 3));
}
footprints.add(sb.toString());
}
}
return footprints;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment