From 31d23f226d073d142b10e290ac8ec68e872d7b4e Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Sun, 3 May 2015 15:43:12 -0500 Subject: [PATCH] (GH-121) Add hash provider To compute hashes in files, add a hash provider that can determine hashes of files. --- src/chocolatey/chocolatey.csproj | 2 + .../registration/ContainerBinding.cs | 3 ++ .../cryptography/IHashProvider.cs | 30 +++++++++++++ .../cryptography/Md5HashProvider.cs | 42 +++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 src/chocolatey/infrastructure/cryptography/IHashProvider.cs create mode 100644 src/chocolatey/infrastructure/cryptography/Md5HashProvider.cs diff --git a/src/chocolatey/chocolatey.csproj b/src/chocolatey/chocolatey.csproj index 18b9ede0fa..fcb6693aec 100644 --- a/src/chocolatey/chocolatey.csproj +++ b/src/chocolatey/chocolatey.csproj @@ -166,6 +166,8 @@ + + diff --git a/src/chocolatey/infrastructure.app/registration/ContainerBinding.cs b/src/chocolatey/infrastructure.app/registration/ContainerBinding.cs index 28fde79390..762607914c 100644 --- a/src/chocolatey/infrastructure.app/registration/ContainerBinding.cs +++ b/src/chocolatey/infrastructure.app/registration/ContainerBinding.cs @@ -20,6 +20,7 @@ namespace chocolatey.infrastructure.app.registration using SimpleInjector; using adapters; using commands; + using cryptography; using events; using filesystem; using infrastructure.commands; @@ -28,6 +29,7 @@ namespace chocolatey.infrastructure.app.registration using nuget; using services; using IFileSystem = filesystem.IFileSystem; + using IHashProvider = cryptography.IHashProvider; // ReSharper disable InconsistentNaming @@ -54,6 +56,7 @@ public void RegisterComponents(Container container) container.Register(Lifestyle.Singleton); container.Register(Lifestyle.Singleton); container.Register(Lifestyle.Singleton); + container.Register(Lifestyle.Singleton); container.Register(Lifestyle.Singleton); container.Register(Lifestyle.Singleton); container.Register(Lifestyle.Singleton); diff --git a/src/chocolatey/infrastructure/cryptography/IHashProvider.cs b/src/chocolatey/infrastructure/cryptography/IHashProvider.cs new file mode 100644 index 0000000000..5356a05097 --- /dev/null +++ b/src/chocolatey/infrastructure/cryptography/IHashProvider.cs @@ -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 +{ + /// + /// A hash provider for hashing files + /// + public interface IHashProvider + { + /// + /// Returns a hash of the specified file path. + /// + /// The file path. + /// A computed hash of the file, based on the contents. + string hash_file(string filePath); + } +} \ No newline at end of file diff --git a/src/chocolatey/infrastructure/cryptography/Md5HashProvider.cs b/src/chocolatey/infrastructure/cryptography/Md5HashProvider.cs new file mode 100644 index 0000000000..e15004bc57 --- /dev/null +++ b/src/chocolatey/infrastructure/cryptography/Md5HashProvider.cs @@ -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); + } + } +} \ No newline at end of file