2015-10-04 45 views
1

製作應用程序後,我希望Logcat在其日誌中打印應用程序的SHA-1密鑰。如何在logcat中獲取android應用程序的SHA-1/MD 5指紋?

而不是運行

keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

命令。

無論應用程序處於調試/發佈模式。

這是可能在android studio中完成的嗎?

當然,測試後我會刪除logcat行,以便其他人可能無法調試它。

回答

1

就我個人而言,我會使用SHA256而不是SHA1。這就是我在做my CWAC-Security libraryin SignatureUtils

/*** 
    Copyright (c) 2014 CommonsWare, LLC 

    Licensed under the Apache License, Version 2.0 (the "License"); you may 
    not use this file except in compliance with the License. You may obtain 
    a copy of the License at 
    http://www.apache.org/licenses/LICENSE-2.0 
    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License. 
*/ 

package com.commonsware.cwac.security; 

import android.content.Context; 
import android.content.pm.PackageManager; 
import android.content.pm.PackageManager.NameNotFoundException; 
import android.content.pm.Signature; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 

public class SignatureUtils { 
    public static String getOwnSignatureHash(Context ctxt) 
                 throws NameNotFoundException, 
                 NoSuchAlgorithmException { 
    return(getSignatureHash(ctxt, ctxt.getPackageName())); 
    } 

    public static String getSignatureHash(Context ctxt, String packageName) 
                     throws NameNotFoundException, 
                     NoSuchAlgorithmException { 
    MessageDigest md=MessageDigest.getInstance("SHA-256"); 
    Signature sig= 
     ctxt.getPackageManager() 
      .getPackageInfo(packageName, PackageManager.GET_SIGNATURES).signatures[0]; 

    return(toHexStringWithColons(md.digest(sig.toByteArray()))); 
    } 

    // based on https://stackoverflow.com/a/2197650/115145 

    public static String toHexStringWithColons(byte[] bytes) { 
    char[] hexArray= 
     { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 
      'C', 'D', 'E', 'F' }; 
    char[] hexChars=new char[(bytes.length * 3) - 1]; 
    int v; 

    for (int j=0; j < bytes.length; j++) { 
     v=bytes[j] & 0xFF; 
     hexChars[j * 3]=hexArray[v/16]; 
     hexChars[j * 3 + 1]=hexArray[v % 16]; 

     if (j < bytes.length - 1) { 
     hexChars[j * 3 + 2]=':'; 
     } 
    } 

    return new String(hexChars); 
    } 
} 

如果你真的想SHA1,你應該能夠改變MessageDigest.getInstance()調用,以適應。而且,如果冒號分隔的十六進制數字對不是您想要的輸出格式(我選擇匹配keytool),您可以按照您的意願將byte[]轉換爲可打印的輸出格式。

+0

很榮幸能從您那裏得到答案@CommonsWare :-) – penta

相關問題