# Visual Basic > Visual Basic .NET >  MD5 Check Sum

## Tesla1886

I have been using the following code to get the MD5 Check Sum.



```
        Dim fct As Integer = 0
        Dim maxF As Long = 0
        Dim totF As Long = 0
        Dim md5Obj As New System.Security.Cryptography.MD5CryptoServiceProvider

        Dim bytesToHash() As Byte = File.ReadAllBytes(filename)
        fct += 1 'count
        If bytesToHash.Length > maxF Then maxF = bytesToHash.Length 'largest file
        totF += bytesToHash.Length 'total for average
        Dim hash() As Byte = md5Obj.ComputeHash(bytesToHash)
        Dim rv As String = BitConverter.ToString(hash).Replace("-", String.Empty)
        bytesToHash = Nothing
        hash = Nothing
        Return rv
```

It has been working just fine, until I attempted to use it on files larger then 2 gig now I am receiving the following error



```
System.IO.IOException: The file is too long. This operation is currently limited to supporting files less than 2 gigabytes in size.
```

Is there a way to to get the MD5 Check Sum that will work with not only files larger then 2 gig, but all file sizes.

----------


## PlausiblyDamp

There is an overload  that takes a stream rather than a byte array, that might work.

----------


## jmcilhinney

If you want help with a compilation error or a run-time exception then please tell us where it occurs. We shouldn't have to guess or work out something that you already know. Given that the error message mentions a file and the only place you're working with files is when calling File.ReadAllBytes, I'm guessing it's there.

The first thing I'd ask is what framework you're targeting. If you're targeting .NET 6 or later then you should not be using that MD5CryptoServiceProvider class, but rather calling MD5.Create to get an MD5 instance and using that.

Assuming that the MD5CryptoServiceProvider class is appropriate, it has an overload of ComputeHash that takes a Stream as an argument. I haven't tested but I would guess that you could create a FileStream and pass that. Failing that, I think that you'd have to read the file in chunks and call TransformBlock for each chunk. You would then call TransformFinalBlock and get the Hash property. You could test the integrity of the result by hashing a smaller amount of data in one go and in chunks and see whether you get the same result.

----------


## jmcilhinney

I just tested this code and it displayed the same hash for all three methods.

vb.net Code:
Imports System.IO
Imports System.Security.Cryptography
 Module Module1
     Private filePath As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "Test.bin")
    Private hasher As New MD5CryptoServiceProvider
     Sub Main()
        CreateFile()
        Console.WriteLine(GetHashFromBytes())
        Console.WriteLine(GetHashFromStream())
        Console.WriteLine(GetHashFromBlocks())
    End Sub
     Private Sub CreateFile()
        Dim rng As New Random
        Dim byteCount = 5 * 32
        Dim bytes(byteCount - 1) As Byte
         rng.NextBytes(bytes)
        File.WriteAllBytes(filePath, bytes)
    End Sub
     Private Function GetHashFromBytes() As String
        Dim bytes = File.ReadAllBytes(filePath)
        Dim hash = hasher.ComputeHash(bytes)
         Return Convert.ToBase64String(hash)
    End Function
     Private Function GetHashFromStream() As String
        Using stream = File.OpenRead(filePath)
            Dim hash = hasher.ComputeHash(stream)
             Return Convert.ToBase64String(hash)
        End Using
    End Function
     Private Function GetHashFromBlocks() As String
        Dim byteCount = 32
        Dim bytes(byteCount - 1) As Byte
         Using stream = File.OpenRead(filePath)
            Dim bytesRead = stream.Read(bytes, 0, byteCount)
             Do Until bytesRead = 0
                hasher.TransformBlock(bytes, 0, bytesRead, Nothing, 0)
                bytesRead = stream.Read(bytes, 0, byteCount)
            Loop
             hasher.TransformFinalBlock(bytes, 0, bytesRead)
        End Using
         Return Convert.ToBase64String(hasher.Hash)
    End Function
 End Module
If you're unable to use the first method because your file is too big, the second method will probably work and the third method definitely will. I would assume that the overload of ComputeHash that takes a Stream would do pretty much the same thing as the third method here, although it probably uses larger blocks. You could check the .NET source code online to confirm that, if you wanted to.

----------


## Tesla1886

> If you want help with a compilation error or a run-time exception then please tell us where it occurs. We shouldn't have to guess or work out something that you already know. Given that the error message mentions a file and the only place you're working with files is when calling File.ReadAllBytes, I'm guessing it's there.


Apologies. I did not specify where I was getting the error because I figured since it would not work on files larger then 2 gig, it would all need to be replaced. The line I am getting an error on is below.



```
Dim bytesToHash() As Byte = File.ReadAllBytes(filename)
```





> The first thing I'd ask is what framework you're targeting. If you're targeting .NET 6 or later then you should not be using that MD5CryptoServiceProvider class, but rather calling MD5.Create to get an MD5 instance and using that.


To be honest it doesn't matter to me which version I use. I currently only have Version 5 installed and using Visual Studio 2019. If Version 6 is better and faster then 5, which apparently it is, then I have no problem installing 6. I will be the only one using this program.

----------


## jmcilhinney

> To be honest it doesn't matter to me which version I use. I currently only have Version 5 installed and using Visual Studio 2019. If Version 6 is better and faster then 5, which apparently it is, then I have no problem installing 6. I will be the only one using this program.


.NET 5 is no longer supported. You can use it but you probably shouldn't, and definitely not for production code. That said, I think that .NET 6 requires VS 2022, so you'd need to upgrade your VS version too. That's not such a bad thing though. You can have both installed but it's probably not worth doing so. Anything you created in VS 2019 will work in VS 2022.

----------


## Tesla1886

I am currently using GetHashFromStream().

----------


## Tesla1886

> .NET 5 is no longer supported. You can use it but you probably shouldn't, and definitely not for production code. That said, I think that .NET 6 requires VS 2022, so you'd need to upgrade your VS version too. That's not such a bad thing though. You can have both installed but it's probably not worth doing so. Anything you created in VS 2019 will work in VS 2022.


Thank You I was not aware 5 was no longer support, nor that 6 required 2022. I will upgrade as soon as I can.

It is hard to keep track of what still is and what is no longer supported.

----------


## PlausiblyDamp

> Thank You I was not aware 5 was no longer support, nor that 6 required 2022. I will upgrade as soon as I can.
> 
> It is hard to keep track of what still is and what is no longer supported.


https://dotnet.microsoft.com/en-us/p...t-core#cadence is pretty handy for understanding what the current support policy looks like. Generally though odd numbered versions are what is called STS (Standard Term Support) and have a shorter supported lifetime than even numbered versions which are LTS (Long Term Support)

----------


## wqweto

> https://dotnet.microsoft.com/en-us/p...t-core#cadence is pretty handy for understanding what the current support policy looks like. Generally though odd numbered versions are what is called STS (Standard Term Support) and have a shorter supported lifetime than even numbered versions which are LTS (Long Term Support)


STS vs LTS makes no sense to me. Can't they just support *every* major version for *10* years and stop playing games with roadmaps about "enabling developers" and more corporate BS?

This or if not possible tell everyone to go use Java and be done with it if it so hard for them to support all the BS they produce.

cheers,
</wqw>

----------


## PlausiblyDamp

> STS vs LTS makes no sense to me. Can't they just support *every* major version for *10* years and stop playing games with roadmaps about "enabling developers" and more corporate BS?
> 
> This or if not possible tell everyone to go use Java and be done with it if it so hard for them to support all the BS they produce.
> 
> cheers,
> </wqw>


The only realistic way they could support every release for 10 years would be to reduce the frequency of releases, this would slow down the release of new features and other improvements. Having a STS releases allows for the quicker introduction of new features - if you need these features then you can make a decision to switch to the STS now and know that you will need to upgrade soon-ish, or wait till the next LTS that will incorporate these changes and provide a longer support lifetime.

Given that upgrades to newer releases are normally straightforward anyway then keeping reasonably current isn't that difficult. Also a version going out of support doesn't mean it won't continue to run, just that it will no longer be receiving updates and fixes. You can still run .Net framework 1.0 apps on windows and that was released about 15 years ago.

Not sure why you would suggest moving to Java when you refer to 10 year supported versions as it only has LTS support for 5 years and it has interim build with a supported lifetime of around 6 months.

----------

