返回顶部

收藏

Android 下获取MD5编码

更多
private String GetMd5(String FilePath)
{
    char hexdigits[] =
    {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f'};
    FileInputStream fis = null;
    String sString;
    char str[] = new char[16 * 2];
    int k = 0;
    try
    {
        MessageDigest md = MessageDigest.getInstance("MD5");
        fis = new FileInputStream(FilePath);
        byte[] buffer = new byte[2048];
        int length = -1;
        // long s = System.currentTimeMillis();
        while ((length = fis.read(buffer)) != -1)
        {
            md.update(buffer, 0, length);
        }
        byte[] b = md.digest();

        for (int i = 0; i < 16; i++)
        {
            byte byte0 = b[i];
            str[k++] = hexdigits[byte0 >>> 4 & 0xf];
            str[k++] = hexdigits[byte0 & 0xf];
        }
        fis.close();
        sString = new String(str);

        return sString;
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
        return null;
    }
}

标签:MD5,Android,Java

收藏

0人收藏

支持

0

反对

0

发表评论