android中获取文件的md5值时首位出现0被省略,而造成了只有31位字符的解决办法。

来源:互联网 发布:java基础论坛 编辑:程序博客网 时间:2024/05/17 01:16

使用BigInteger 会出现首位为0时MD5值出现31位的现象。

//获取单个文件MD5  其中一种操作方式
    public static String getFileMD5(File file)   
    {  
        // TODO Auto-generated method stub  
        if(!file.isFile())  
        {  
            return null;  
        }  
        MessageDigest digest=null;  
        FileInputStream in=null;  
        byte buffer[]=new byte[1024];  
        int len;  
        try  
        {  
            digest=MessageDigest.getInstance("MD5");  
            in=new FileInputStream(file);  
            while((len=in.read(buffer, 0, 1024))!=-1)  
            {  
                digest.update(buffer, 0, len);  
            }  
            in.close();  
        }  
        catch(Exception e)  
        {  
            e.printStackTrace();  
            return null;  
        }  
          
        BigInteger bigint=new BigInteger(1,digest.digest());  
        return bigint.toString(16);  
    }  

//获取单个文件MD5  另一种操作方式
public String getMd5(File file){
        String value = null;
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(byteBuffer);
            BigInteger bi = new BigInteger(1, md5.digest());
            value = bi.toString(16);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return value;
    }


上面的两种方法都是使用了BigInteger 来保存后,再转化成字符串,都会出现首位为0被忽略的现象。

下面的方法可以解决上述的问题

public static String getFileMD5(File file) {    StringBuffer stringbuffer=null;    try {        char hexDigits[] = { '0', '1', '2','3', '4','5', '6','7','8', '9', 'a','b' ,'c', 'd','e', 'f' };        FileInputStream in = new FileInputStream(file);        FileChannel ch = in.getChannel();        MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,file.length());        MessageDigest messagedigest = MessageDigest.getInstance("MD5");        messagedigest.update(byteBuffer);        byte[] bytes=messagedigest.digest();        int n=bytes.length;        stringbuffer = new StringBuffer(2 * n);        for (int l = 0; l < n; l++) {            byte bt=bytes[l];            char c0 = hexDigits[(bt & 0xf0) >> 4];            char c1 = hexDigits[bt & 0xf];            stringbuffer.append(c0);            stringbuffer.append(c1);        }    } catch (Exception e) {        e.printStackTrace();    }    return stringbuffer.toString();    }}

0 0
原创粉丝点击