forked from cake-build/cake
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cake-buildGH-2099: Also takes care of RFC-02.
This is the same as pull request 2650 but has been updated to match the change made in Cake version 1.0.0.
- Loading branch information
1 parent
d454dc2
commit b4d5851
Showing
6 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace Cake.Utilities | ||
{ | ||
/// <summary> | ||
/// Optimized hash generator. Using SHA1CryptoServiceProvider since it is FIPS compliant. | ||
/// </summary> | ||
internal static class FastHash | ||
{ | ||
/// <summary> | ||
/// Generates a hash of the passed byte arrays. | ||
/// </summary> | ||
/// <param name="input">The binary data to hash.</param> | ||
/// <returns>The hash value.</returns> | ||
public static string GenerateHash(byte[] input) | ||
{ | ||
using (var sha1 = new SHA1CryptoServiceProvider()) | ||
{ | ||
sha1.TransformBlock(input, 0, input.Length, input, 0); | ||
|
||
// Just finalize with empty bytes so we don't have to iterate over the enumerable multiple times | ||
sha1.TransformFinalBlock(Encoding.UTF8.GetBytes(string.Empty), 0, 0); | ||
// Convert to hex string; This method is supposedly faster than the usual StringBuilder approach | ||
return ConvertBits(sha1.Hash); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Generates a hash of the passed byte arrays. | ||
/// </summary> | ||
/// <param name="inputs">The binary data to hash.</param> | ||
/// <returns>The hash value.</returns> | ||
public static string GenerateHash(IEnumerable<byte[]> inputs) | ||
{ | ||
using (var sha1 = new SHA1CryptoServiceProvider()) | ||
{ | ||
foreach (var input in inputs) | ||
{ | ||
sha1.TransformBlock(input, 0, input.Length, input, 0); | ||
} | ||
|
||
// Just finalize with empty bytes so we don't have to iterate over the enumerable multiple times | ||
sha1.TransformFinalBlock(Encoding.UTF8.GetBytes(string.Empty), 0, 0); | ||
// Convert to hex string; This method is supposedly faster than the usual StringBuilder approach | ||
return ConvertBits(sha1.Hash); | ||
} | ||
} | ||
|
||
private static string ConvertBits(byte[] hash) | ||
{ | ||
return BitConverter.ToString(hash) | ||
// without dashes | ||
.Replace("-", string.Empty) | ||
// make lowercase | ||
.ToLower(); | ||
} | ||
} | ||
} |