【问题标题】:Generate a Hash from installed apk file in memory从内存中已安装的 apk 文件生成哈希
【发布时间】:2014-01-22 12:23:20
【问题描述】:

我正在使用此代码,但它显示的是同一应用程序的哈希代码。 请帮帮我。

File file = new File(getApplicationContext().getPackageCodePath());
     String outputTxt= "";
        String hashcode = null;

    try {

        FileInputStream input = new FileInputStream(file);

        ByteArrayOutputStream output = new ByteArrayOutputStream ();
        byte [] buffer = new byte [65536];
        int l;


            while ((l = input.read (buffer)) > 0)
                output.write (buffer, 0, l);

            input.close ();
            output.close ();

            byte [] data = output.toByteArray ();

            MessageDigest digest = MessageDigest.getInstance( "SHA-1" ); 

            byte[] bytes = data;

            digest.update(bytes, 0, bytes.length);
            bytes = digest.digest();

            StringBuilder sb = new StringBuilder();

            for( byte b : bytes )
            {
                sb.append( String.format("%02X", b) );
            }

            hashcode = sb.toString();





    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

现在我想读取我当前的android应用程序的已安装应用程序文件(假设它将是*.apk),从该文件中读取字节数组并生成哈希值。

提前致谢。

【问题讨论】:

    标签: android sha1 hashcode android-install-apk


    【解决方案1】:

    这是最好的解决方案。

    首先你必须传递 .apk 文件的路径。

              private CharSequence getHash(String sourceDir)  {
              // TODO Auto-generated method stub
    
              File file = new File(packageInfo.applicationInfo.sourceDir);
             String outputTxt= "";
               String hashcode = null;
    
            try {
    
            FileInputStream input = new FileInputStream(file);
    
            ByteArrayOutputStream output = new ByteArrayOutputStream ();
            byte [] buffer = new byte [65536];
            int l;
    
    
                  while ((l = input.read (buffer)) > 0)
                      output.write (buffer, 0, l);
    
                      input.close ();
                  output.close ();
    
                    byte [] data = output.toByteArray ();
    
                    MessageDigest digest = MessageDigest.getInstance( "SHA-1" ); 
    
                byte[] bytes = data;
    
                digest.update(bytes, 0, bytes.length);
                bytes = digest.digest();
    
                StringBuilder sb = new StringBuilder();
    
                for( byte b : bytes )
                {
                    sb.append( String.format("%02X", b) );
                }
    
                hashcode = sb.toString();
    
    
             } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
             } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
             } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           }
    
    
    
              return hashcode;
             }
    

    【讨论】:

      【解决方案2】:
      public void buttonAppClick() {
          final PackageManager pm = getActivity().getPackageManager();
          //get a list of installed apps.
          List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
          outputTextView.setText("");
          for (ApplicationInfo packageInfo : packages) {
              try {
                  String packageName = packageInfo.packageName;
                  outputTextView.append("Apk Path : " + packageInfo.sourceDir + "\n");
                  PackageInfo pi = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
      
                  Signature sig = pi.signatures[0];
                  String md5Fingerprint = doFingerprint(sig.toByteArray(), "MD5");
                  Log.d(TAG_HOME, "MD5 : " + packageInfo.sourceDir + md5Fingerprint);
                  outputTextView.append("MD5 : " + md5Fingerprint + "\n");
                  outputTextView.append("\n");
              }
              catch (Exception e) {
                  Log.e(TAG_HOME, e.getMessage());
              }
          }
      }
      
      protected static String doFingerprint(byte[] certificateBytes, String algorithm)
              throws Exception {
          MessageDigest md = MessageDigest.getInstance(algorithm);
          md.update(certificateBytes);
          byte[] digest = md.digest();
      
          String toRet = "";
          for (int i = 0; i < digest.length; i++) {
              if (i != 0)
                  toRet += ":";
              int b = digest[i] & 0xff;
              String hex = Integer.toHexString(b);
              if (hex.length() == 1)
                  toRet += "0";
              toRet += hex;
          }
          return toRet;
      }
      

      首先我从 PackageInfo 获得签名 将该签名转换为字节数组,然后使用 doFringerPrint 函数获取 android 应用的 MD5。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-17
        • 2012-11-19
        • 2019-04-28
        • 2016-07-16
        • 1970-01-01
        • 2022-11-24
        • 2020-12-13
        • 1970-01-01
        相关资源
        最近更新 更多