Android Question different checksums on local file vs. same file uploaded to Google Drive?

Dave O

Well-Known Member
Licensed User
Longtime User
Hi all,

Why am I getting different MD5 checksums on a local data file vs. the same file uploaded to Google Drive?

Long version:

I'm using the Google Drive API to upload backups of my app's data files. I only want to upload a file if it's actually different from the existing Google version, so I'm comparing the MD5 checksums of the local and Google versions of the file. In my testing so far, the local text file is 476 bytes, and the same file on Google Drive is 478 bytes. Hmmm.

Here's how I calculate the local checksum (based on this old post):

B4X:
Sub getLocalChecksum(fileNameArg As String) As String
    Dim iStream As InputStream = File.OpenInput(File.DirInternal, fileNameArg)
    Dim buffer(File.Size(File.DirInternal,fileNameArg)) As Byte
    Dim count As Int = iStream.ReadBytes(buffer, 0, buffer.length)
    Dim data(buffer.Length) As Byte
    Dim md As MessageDigest
    data = md.GetMessageDigest(buffer, "MD5")
    Dim bc As ByteConverter
    Dim checksum As String = bc.HexFromBytes(data)
    Return checksum
End Sub

...and here's how I'm getting the checksum from Google Drive, based on this addition to GoogleDrive.bas:

B4X:
Sub getCloudChecksum(fileId As String)
    c.GD.getFileMetadata(fileId)
    wait for GD_GotFileMetadata(fileMap As Map)
    If fileMap.ContainsKey("error") Then
        cloudChecksum = ""
    Else
        cloudChecksum = fileMap.Get("md5Checksum")
    End If
End Sub

One thing I noticed is that, in getLocalChecksum, the ReadBytes call returns 476, but the docs say that it should return -1 if there are no more bytes to read. I'm not sure why that's happening, and if it's the culprit. Or perhaps because the file systems are different?

Ideas? Thanks!
 
Top