Android SHA1 Hash Create getSHA1CertFingerprint(Context ctx)

Here you can find the source of getSHA1CertFingerprint(Context ctx)

Description

get SHA Cert Fingerprint

License

Apache License

Declaration

static String getSHA1CertFingerprint(Context ctx) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.Signature;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    static String getSHA1CertFingerprint(Context ctx) {
        try {/*w  ww .ja v a  2 s.c o  m*/
            Signature[] sigs = ctx.getPackageManager().getPackageInfo(
                    ctx.getPackageName(), PackageManager.GET_SIGNATURES).signatures;
            if (sigs.length == 0) {
                return "ERROR: NO SIGNATURE.";
            } else if (sigs.length > 1) {
                return "ERROR: MULTIPLE SIGNATURES";
            }
            byte[] digest = MessageDigest.getInstance("SHA1").digest(
                    sigs[0].toByteArray());
            StringBuilder hexString = new StringBuilder();
            for (int i = 0; i < digest.length; ++i) {
                if (i > 0) {
                    hexString.append(":");
                }
                byteToString(hexString, digest[i]);
            }
            return hexString.toString();

        } catch (PackageManager.NameNotFoundException ex) {
            ex.printStackTrace();
            return "(ERROR: package not found)";
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
            return "(ERROR: SHA1 algorithm not found)";
        }
    }

    static void byteToString(StringBuilder sb, byte b) {
        int unsigned_byte = b < 0 ? b + 256 : b;
        int hi = unsigned_byte / 16;
        int lo = unsigned_byte % 16;
        sb.append("0123456789ABCDEF".substring(hi, hi + 1));
        sb.append("0123456789ABCDEF".substring(lo, lo + 1));
    }
}

Related

  1. sha1(String ori)
  2. sha1(String s)
  3. sha1(String s)
  4. computeHashSHA1(final String text)
  5. computeSHAHash(String password)
  6. SHA1(String text)
  7. shaByte(String in)
  8. shaByte(byte[] in)
  9. calculateSHA1(byte[] data)