Android Study

Android HashKey 구하기 (feat.Java)

85chong 2022. 2. 15. 14:50
728x90
반응형
SMALL
    private void getHashKey(){
        Signature[] signatures = null;
        try {
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
                signatures = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNING_CERTIFICATES).signingInfo.getApkContentsSigners();
            }else{
                signatures = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES).signatures;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        try {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signatures[0].toByteArray());
            m_hashKey = Base64.encodeToString(md.digest(), Base64.DEFAULT).trim();
        } catch (NoSuchAlgorithmException e) {
            //defualt = ""
        }
   }

P 버전 이상 이하에 대한 처리 됨