Skip to content

Commit

Permalink
(GH-1047) Add additional hash stream options to Providers
Browse files Browse the repository at this point in the history
  • Loading branch information
RichiCoder1 authored and ferventcoder committed Mar 22, 2017
1 parent e210168 commit a8aafc1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/chocolatey/infrastructure/adapters/HashAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace chocolatey.infrastructure.adapters
{
using System.IO;
using cryptography;

public sealed class HashAlgorithm : IHashAlgorithm
Expand All @@ -15,5 +16,10 @@ public byte[] ComputeHash(byte[] buffer)
{
return _algorithm.ComputeHash(buffer);
}

public byte[] ComputeHash(Stream stream)
{
return _algorithm.ComputeHash(stream);
}
}
}
3 changes: 3 additions & 0 deletions src/chocolatey/infrastructure/adapters/IHashAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
namespace chocolatey.infrastructure.adapters
{
using System.IO;
// ReSharper disable InconsistentNaming

public interface IHashAlgorithm
{
byte[] ComputeHash(byte[] buffer);

byte[] ComputeHash(Stream stream);
}

// ReSharper restore InconsistentNaming
Expand Down
16 changes: 15 additions & 1 deletion src/chocolatey/infrastructure/cryptography/CryptoHashProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2011 - Present RealDimensions Software, LLC
// 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.
Expand Down Expand Up @@ -107,6 +107,20 @@ public string hash_file(string filePath)
}
}

public string hash_byte_array(byte[] buffer)
{
var hash = _hashAlgorithm.ComputeHash(buffer);

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

public string hash_stream(Stream inputStream)
{
var hash = _hashAlgorithm.ComputeHash(inputStream);

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

private static bool file_is_locked(Exception exception)
{
var errorCode = 0;
Expand Down
18 changes: 17 additions & 1 deletion src/chocolatey/infrastructure/cryptography/IHashProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2011 - Present RealDimensions Software, LLC
// 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.
Expand All @@ -13,6 +13,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.IO;

namespace chocolatey.infrastructure.cryptography
{
/// <summary>
Expand All @@ -32,5 +34,19 @@ public interface IHashProvider
/// <param name="filePath">The file path.</param>
/// <returns>A computed hash of the file, based on the contents.</returns>
string hash_file(string filePath);

/// <summary>
/// Returns a hash of the specified stream.
/// </summary>
/// <param name="inputStream">The stream.</param>
/// <returns>A computed hash of the stream, based on the contents.</returns>
string hash_stream(Stream inputStream);

/// <summary>
/// Returns a hash of the specified byte array.
/// </summary>
/// <param name="buffer">The byte array.</param>
/// <returns>A computed hash of the array, based on the contents.</returns>
string hash_byte_array(byte[] buffer);
}
}

0 comments on commit a8aafc1

Please sign in to comment.