如何在Logcat中获取Android应用程序的SHA-1/MD 5指纹? [英] How to get SHA-1/MD 5 fingerprint of android app in the logcat?

查看:83
本文介绍了如何在Logcat中获取Android应用程序的SHA-1/MD 5指纹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After making the app, I want the Logcat to print the SHA-1 key of the app in its logs.

代替运行

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

命令.

无论该应用处于调试/发布模式还是

Regardless the app is in debug/ release mode.

这是否可以在android studio中完成?

Is this possible to be done in android studio ?

当然,在测试之后,我将删除logcat行,以便其他人无法调试它.

Ofcourse after testing I will remove the logcat line so that others possible can't debug it.

推荐答案

我个人将使用SHA256而不是SHA1.这就是我所做的我的CWAC安全库中的SignatureUtils 中的nofollow>

Personally, I would use SHA256 instead of SHA1. That's what I did in SignatureUtils in my CWAC-Security library:

/***
  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[]转换为其他可打印输出.

If you really want SHA1, you should be able to change the MessageDigest.getInstance() call to suit. And, if colon-delimited pairs of hex digits isn't your desired output format (I chose it to match keytool), you can convert the byte[] into a printable output in some other fashion if you wish.

这篇关于如何在Logcat中获取Android应用程序的SHA-1/MD 5指纹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆