string hash;
using (var hasher = MD5.Create())
- using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
+ using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 65536, true))
{
var hashBytes = hasher.ComputeHash(stream);
hash = hashBytes.ToHashString();
return shb.ToString().ToLower();
}
- public static TreeHash CalculateTreeHash(FileInfo fileInfo, int blockSize, string algorithm)
+ public static TreeHash CalculateTreeHash(FileSystemInfo fileInfo, int blockSize, string algorithm)
{
- if (fileInfo==null)
+ if (fileInfo == null)
throw new ArgumentNullException("fileInfo");
if (String.IsNullOrWhiteSpace(fileInfo.FullName))
- throw new ArgumentException("fileInfo.FullName is empty","fileInfo");
+ throw new ArgumentException("fileInfo.FullName is empty", "fileInfo");
if (blockSize <= 0)
throw new ArgumentOutOfRangeException("blockSize", "blockSize must be a value greater than zero ");
if (String.IsNullOrWhiteSpace(algorithm))
throw new ArgumentNullException("algorithm");
Contract.EndContractBlock();
+ if (fileInfo is DirectoryInfo || !fileInfo.Exists)
+ return TreeHash.Empty;
+
return CalculateTreeHash(fileInfo.FullName, blockSize, algorithm);
}
throw new ArgumentNullException("algorithm");
Contract.EndContractBlock();
+ if (Log.IsDebugEnabled)
+ Log.DebugFormat("Calc Signature [{0}]",filePath);
+
//DON'T calculate hashes for folders
if (Directory.Exists(filePath))
return new TreeHash(algorithm);
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, blockSize, true))
{
//Calculate the blocks asyncrhonously
- var hashes = await BlockHashAlgorithms.CalculateBlockHashesAgentAsync(stream, blockSize, algorithm, parallelism);
+ var hashes = await BlockHashAlgorithms.CalculateBlockHashesInPlacePFor(stream, blockSize, algorithm, parallelism);
//And then proceed with creating and returning a TreeHash
var length = stream.Length;
{
Bytes = length,
BlockSize = blockSize,
- Hashes = list
+ Hashes = list,
};
+ string fileHash;
+ var hasher = HashAlgorithm.Create("MD5");
+ stream.Position = 0;
+ treeHash.MD5= hasher.ComputeHash(stream).ToHashString();
+
return treeHash;
}
}