生成在内存中安装Android应用程序文件哈希 [英] Generate a Hash from installed android application file in memory

查看:90
本文介绍了生成在内存中安装Android应用程序文件哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够从通过我的Andr​​oid应用程序本身具有以下code创建了一个示例文件(TextFile.txt的)生成散列。
        现在,我想读我目前的Andr​​oid应用程序的安装应用程序文件(假设它会被* .apk文件),从文件中读取字节数组,并产生散列值。

 是否可能,如何做same.Any帮助将是非常美联社preciated。
字符串文件名=TextFile.txt的;
    字符串strMsgToSave =内容检查;
    FileOutputStream中FOS;
    串outputTxt =;
    字符串哈希= NULL;
    字符串例外= NULL;
    尝试
    {
        FOS = this.openFileOutput(文件名,Context.MODE_PRIVATE);
        fos.write(strMsgToSave.getBytes());
        fos.close();        输入的FileInputStream = openFileInput(文件名);
        ByteArrayOutputStream输出=新ByteArrayOutputStream();
        字节[]缓冲区=新的字节[65536]
        INT升;
        而((1- = input.read(缓冲液))大于0)
            output.write(缓冲液,0,1);
        input.close();
        output.close();
        字节[]数据= output.toByteArray();
        消息摘要消化= MessageDigest.getInstance(SHA-1);
        字节[]字节=数据;
        digest.update(字节,0,bytes.length);
        字节= digest.digest();
        StringBuilder的SB =新的StringBuilder();
        对于(BYTE B:字节)
        {
            sb.append(的String.format(%02X,B));
        }
        散列= sb.toString();
        txtOutput.setText(散);    }赶上(例外五){        e.printStackTrace();
    }


解决方案

我已实现了以下code两个不同的Andr​​oid设备和相同的应用验证,发现所检索的值是一样的,不论到设备。以下是code为same.So我想我得到了我的答案。

 文件文件=新的文件(getApplicationContext()getPackage codePATH());
        输入的FileInputStream =新的FileInputStream(文件);
        ByteArrayOutputStream输出=新ByteArrayOutputStream();
        字节[]缓冲区=新的字节[65536]
        INT升;
        而((1- = input.read(缓冲液))大于0)
               output.write(缓冲液,0,1);
        input.close();
        output.close();
        字节[]数据= output.toByteArray();
        消息摘要消化= MessageDigest.getInstance(SHA-1);
        字节[]字节=数据;
        digest.update(字节,0,bytes.length);
        字节= digest.digest();
        StringBuilder的SB =新的StringBuilder();
        对于(BYTE B:字节)
           sb.append(的String.format(%02X,B));
        散列= sb.toString();

getApplicationContext()。getPackage codePATH()返回的完整路径,这种情况下的主要Android包。 Android包是一个ZIP文件,其中包含应用程序的主code和资产。
请让我知道如果这可以被看作是一个解决我的问题,或者有任何建议。

I am able to generate hash from a sample file (textFile.txt) which is created through my android application itself with the following code. Now I want to read the installed application file of my current android application(assuming it will be *.apk), read byte array from that file and produce the Hash value.

    Is it possible and how to do the same.Any help will be highly appreciated.


String FILENAME ="textFile.txt";
    String strMsgToSave = "content check";
    FileOutputStream fos;
    String outputTxt= "";
    String hash = null;
    String exception=null;
    try
    {
        fos = this.openFileOutput( FILENAME, Context.MODE_PRIVATE );
        fos.write( strMsgToSave.getBytes() );
        fos.close();

        FileInputStream input = openFileInput(FILENAME);
        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) );
        }
        hash = sb.toString();
        txtOutput.setText(hash);

    } catch (Exception e) {

        e.printStackTrace();
    }

解决方案

I have implemented the following code verified with two different android device and same application, found that the value that is retrieved is same irrespective to the devices. Following is the code for the same.So i guess I got my answer.

        File file=new File(getApplicationContext().getPackageCodePath());  
        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) );
        hash = sb.toString();

getApplicationContext().getPackageCodePath() Return the full path to this context's primary Android package. The Android package is a ZIP file which contains application's primary code and assets.
Please let me know if this can be considered as a resolution to my problem or if any suggestions.

这篇关于生成在内存中安装Android应用程序文件哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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