安卓获取文件MD5值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓获取文件MD5值相关的知识,希望对你有一定的参考价值。

参考技术A public static String getFileMD5(File file)

        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);

   

如何用vba获取文件的MD5 Hash值?

MD5 可以作为文件的指纹。

MD5就可以为任何文件(不管其大小、格式、数量)产生一个同样独一无二的“数字指纹”,如果任何人对文件做了任何改动,其MD5值也就是对应的“数字指纹”都会发生变化。

如果要获取任意文件的MD5值,可以使用这些API函数直接获取MD5Init ,MD5UpdateMD5Final 。

可以使用如下的代码:

Option Base 0

Public Type MD5_CTX
    i(1) As Long
    buf(3) As Long
    inc(63) As Byte
    digest(15) As Byte
End Type

Public Declare Sub MD5Init Lib "Cryptdll.dll" (ByVal pContex As Long)
Public Declare Sub MD5Final Lib "Cryptdll.dll" (ByVal pContex As Long)
Public Declare Sub MD5Update Lib "Cryptdll.dll" (ByVal pContex As Long, ByVal lPtr As Long, ByVal nSize As Long)

Public Function ConvBytesToBinaryString(bytesIn() As Byte) As String
    Dim i As Long
    Dim nSize As Long
    Dim strRet As String
    
    nSize = UBound(bytesIn)
    For i = 0 To nSize
         strRet = strRet & Right$("0" & Hex(bytesIn(i)), 2)
    Next
    ConvBytesToBinaryString = strRet
End Function

Public Function GetMD5Hash(bytesIn() As Byte) As Byte()
    Dim ctx As MD5_CTX
    Dim nSize As Long
    
    nSize = UBound(bytesIn) + 1
    
    MD5Init VarPtr(ctx)
    MD5Update ByVal VarPtr(ctx), ByVal VarPtr(bytesIn(0)), nSize
    MD5Final VarPtr(ctx)
    
    GetMD5Hash = ctx.digest
End Function

Public Function GetMD5Hash_Bytes(bytesIn() As Byte) As String
    GetMD5Hash_Bytes = ConvBytesToBinaryString(GetMD5Hash(bytesIn))
End Function

Public Function GetMD5Hash_String(ByVal strIn As String) As String
    GetMD5Hash_String = GetMD5Hash_Bytes(StrConv(strIn, vbFromUnicode))
End Function

Public Function GetMD5Hash_File(ByVal strFile As String) As String
    Dim lFile As Long
    Dim bytes() As Byte
    Dim lSize As Long
    
    lSize = FileLen(strFile)
    If (lSize) Then
        lFile = FreeFile
        ReDim bytes(lSize - 1)
        Open strFile For Binary As lFile
        Get lFile, , bytes
        Close lFile
        GetMD5Hash_File = GetMD5Hash_Bytes(bytes)
    End If
End Function

 

以上是关于安卓获取文件MD5值的主要内容,如果未能解决你的问题,请参考以下文章

shell脚本获取MD5值

获取文件的md5值

iOS开发之获取文件的md5值

如何用vba获取文件的MD5 Hash值?

获取txt md5值上传文件完整性校验

获取文件的MD5值