【问题标题】:How to get SHA-1/MD 5 fingerprint of android app in the logcat?如何在 logcat 中获取 android 应用程序的 SHA-1/MD 5 指纹?
【发布时间】:2016-01-01 06:00:17
【问题描述】:

制作应用程序后,我希望 Logcat 在其日志中打印应用程序的 SHA-1 密钥。

而不是运行

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

命令。

无论应用处于调试/发布模式。

这可以在android studio中完成吗?

当然在测试后我会删除logcat这一行,这样其他人可能无法调试它。

【问题讨论】:

    标签: android android-studio sha1


    【解决方案1】:

    就我个人而言,我会使用 SHA256 而不是 SHA1。这就是我在my CWAC-Security library 中所做的in 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[] 转换为可打印的输出。

    【讨论】:

    • 很荣幸得到您的答复@CommonsWare :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 2015-03-30
    相关资源
    最近更新 更多