-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: See D19583338 for context Reviewed By: jingping2015 Differential Revision: D19584725 fbshipit-source-id: 0d31a7a989a57d717930c37bc7aa54e6c7f537ae
- Loading branch information
1 parent
fcc91ad
commit f5bd6a6
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
facebook-core/src/main/java/com/facebook/internal/security/CertificateUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.facebook.internal.security; | ||
|
||
import android.content.Context; | ||
import android.content.pm.PackageManager; | ||
import android.content.pm.Signature; | ||
import android.util.Base64; | ||
|
||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
/* | ||
This would need to be changed if we change the docs for how to submit the certificate hashes | ||
https://developers.facebook.com/docs/android/getting-started/ | ||
*/ | ||
public class CertificateUtil { | ||
|
||
private CertificateUtil() {} | ||
|
||
private static final String DELIMITER = ":"; //not part of valid characters for base64 | ||
|
||
/** | ||
* @return String of concatenated signatures, since there can be more than one | ||
*/ | ||
public static String getCertificateHash(Context ctx) { | ||
try { | ||
Signature[] signatures = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), PackageManager.GET_SIGNATURES).signatures; | ||
StringBuilder sb = new StringBuilder(); | ||
MessageDigest md = MessageDigest.getInstance("SHA1"); | ||
for (Signature signature : signatures) { | ||
md.update(signature.toByteArray()); | ||
sb.append(Base64.encodeToString(md.digest(), Base64.DEFAULT)); | ||
sb.append(DELIMITER); | ||
} | ||
|
||
if (sb.length() > 0) { | ||
sb.setLength(sb.length() - 1); //remove last delimiter | ||
} | ||
|
||
return sb.toString(); | ||
} catch (PackageManager.NameNotFoundException e) { | ||
//do nothing | ||
} catch (NoSuchAlgorithmException e) { | ||
//do nothing | ||
} | ||
return ""; | ||
} | ||
} |