Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#747) Switch CryptoHashProvider to filestream for hashing files #2714

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion src/chocolatey.tests/chocolatey.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@
<Compile Include="infrastructure\commands\CommandExecutorSpecs.cs" />
<Compile Include="infrastructure\commands\PowershellExecutorSpecs.cs" />
<Compile Include="infrastructure\configuration\ConfigSpecs.cs" />
<Compile Include="infrastructure\cryptography\CryptoHashProviderSpecs.cs" />
<Compile Include="infrastructure\events\context\FakeEvent.cs" />
<Compile Include="infrastructure\events\context\FakeSubscriber.cs" />
<Compile Include="infrastructure\events\EventSubscriptionManagerSpecs.cs" />
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ public static class Environment
public static readonly string ConfigFileTransformExtension = ".install.xdt";
public static readonly string[] ShimDirectorFileExtensions = new string[] { ".gui", ".ignore" };

public static readonly string HashProviderFileTooBig = "UnableToDetectChanges_FileTooBig";
public static readonly string HashProviderFileLocked = "UnableToDetectChanges_FileLocked";

/// <summary>
Expand Down
14 changes: 8 additions & 6 deletions src/chocolatey/infrastructure/cryptography/CryptoHashProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,23 @@ public string ComputeFileHash(string filePath)

try
{
var hash = _hashAlgorithm.ComputeHash(_fileSystem.ReadFileBytes(filePath));

return BitConverter.ToString(hash).Replace("-", string.Empty);
using (var fileStream = _fileSystem.open_file_readonly(filePath))
{
var hash = _hashAlgorithm.ComputeHash(fileStream);
return BitConverter.ToString(hash).Replace("-", string.Empty);
}
}
catch (IOException ex)
{
this.Log().Warn(() => "Error computing hash for '{0}'{1} Hash will be special code for locked file or file too big instead.{1} Captured error:{1} {2}".FormatWith(filePath, Environment.NewLine, ex.Message));
this.Log().Warn(() => "Error computing hash for '{0}'{1} Hash will be special code for locked file {1} Captured error:{1} {2}".FormatWith(filePath, Environment.NewLine, ex.Message));

if (IsFileLocked(ex))
{
return ApplicationParameters.HashProviderFileLocked;
}

//IO.IO_FileTooLong2GB (over Int32.MaxValue)
return ApplicationParameters.HashProviderFileTooBig;
//Rethrow if file is not locked
throw;
}
}

Expand Down