Skip to content

Commit

Permalink
(chocolateyGH-121) Add hash provider
Browse files Browse the repository at this point in the history
To compute hashes in files, add a hash provider that can determine
hashes of files.
  • Loading branch information
ferventcoder committed May 3, 2015
1 parent 2da7306 commit 31d23f2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@
<Compile Include="infrastructure\commandline\InteractivePrompt.cs" />
<Compile Include="infrastructure\commands\ICommandExecutor.cs" />
<Compile Include="infrastructure\commands\PowershellExecutor.cs" />
<Compile Include="infrastructure\cryptography\IHashProvider.cs" />
<Compile Include="infrastructure\cryptography\Md5HashProvider.cs" />
<Compile Include="infrastructure\guards\Ensure.cs" />
<Compile Include="infrastructure\information\ProcessInformation.cs" />
<Compile Include="infrastructure\logging\ChocolateyLoggers.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace chocolatey.infrastructure.app.registration
using SimpleInjector;
using adapters;
using commands;
using cryptography;
using events;
using filesystem;
using infrastructure.commands;
Expand All @@ -28,6 +29,7 @@ namespace chocolatey.infrastructure.app.registration
using nuget;
using services;
using IFileSystem = filesystem.IFileSystem;
using IHashProvider = cryptography.IHashProvider;

// ReSharper disable InconsistentNaming

Expand All @@ -54,6 +56,7 @@ public void RegisterComponents(Container container)
container.Register<IShimGenerationService, ShimGenerationService>(Lifestyle.Singleton);
container.Register<IRegistryService, RegistryService>(Lifestyle.Singleton);
container.Register<IFilesService, FilesService>(Lifestyle.Singleton);
container.Register<IHashProvider, Md5HashProvider>(Lifestyle.Singleton);
container.Register<ITemplateService, TemplateService>(Lifestyle.Singleton);
container.Register<IChocolateyConfigSettingsService, ChocolateyConfigSettingsService>(Lifestyle.Singleton);
container.Register<IChocolateyPackageService, ChocolateyPackageService>(Lifestyle.Singleton);
Expand Down
30 changes: 30 additions & 0 deletions src/chocolatey/infrastructure/cryptography/IHashProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright © 2011 - Present RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace chocolatey.infrastructure.cryptography
{
/// <summary>
/// A hash provider for hashing files
/// </summary>
public interface IHashProvider
{
/// <summary>
/// Returns a hash of the specified file path.
/// </summary>
/// <param name="filePath">The file path.</param>
/// <returns>A computed hash of the file, based on the contents.</returns>
string hash_file(string filePath);
}
}
42 changes: 42 additions & 0 deletions src/chocolatey/infrastructure/cryptography/Md5HashProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright © 2011 - Present RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace chocolatey.infrastructure.cryptography
{
using System;
using System.Security.Cryptography;
using filesystem;

public sealed class Md5HashProvider : IHashProvider
{
private readonly IFileSystem _fileSystem;
private readonly MD5 _cryptoProvider;

public Md5HashProvider(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
_cryptoProvider = MD5.Create();
}

public string hash_file(string filePath)
{
if (!_fileSystem.file_exists(filePath)) return string.Empty;

var hash = _cryptoProvider.ComputeHash(_fileSystem.read_file_bytes(filePath));

return BitConverter.ToString(hash).Replace("-", string.Empty);
}
}
}

0 comments on commit 31d23f2

Please sign in to comment.