Ummm....yeah, EVERYTHING is wrong with that code. I'm not trying to be rude, but what you posted is a fine example of something that someone coming in behind you to maintain would burn to the ground and rewrite from scratch.
Methods should do one thing and do that one thing is a short amount of code. You have to go through a ton of crap just to switch out the hashing algorithm. That's bad.
You should have a method that just takes a filepath and returns a byte array, the hash result. That way, its easy to change out the hashing algorithm and it's all done in a single place.
I've done something similar, but I've written a class that wraps a filepath and exposes a couple of methods that generates multiple hashes for that file. It just so happens that it generates both an MD5 and an SHA1 hash for the file. Something like this, (of course, this is C#. I'm too lazy to convert it to VB.NET):
public class FileHash
{
public string Filepath { get; private set; }
public string Filename
{
get
{
return Path.GetFileName(Filepath);
}
}
public byte[] MD5Hash { get; private set; }
public byte[] SHA1Hash { get; private set; }
private FileHash()
{
}
public FileHash(string filepath)
{
if (string.IsNullOrWhiteSpace(filepath))
{
throw new ArgumentNullException(nameof(filepath));
}
Filepath = filepath;
}
public void ComputeHashes()
{
using (MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider())
{
MD5Hash = GenerateHash(md5Provider, Filepath);
}
using (SHA1CryptoServiceProvider sha1Provider = new SHA1CryptoServiceProvider())
{
SHA1Hash = GenerateHash(sha1Provider, Filepath);
}
}
private byte[] GenerateHash(HashAlgorithm cryptoProvider, string filepath)
{
if (cryptoProvider == null)
{
throw new ArgumentNullException(nameof(cryptoProvider));
}
if (string.IsNullOrWhiteSpace(filepath) || !File.Exists(filepath))
{
throw new ArgumentNullException(nameof(filepath));
}
byte[] result;
using (FileStream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
result = cryptoProvider.ComputeHash(stream);
}
return result;
}
}
This is the stripped down version of the class. I removed all the Async stuff for clarity.