Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Change SHA256 algorithm to work on FIPS-compliant machines.
Browse files Browse the repository at this point in the history
  • Loading branch information
NTaylorMullen committed Aug 17, 2016
1 parent 6e5fd4f commit edb5baf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
15 changes: 8 additions & 7 deletions src/Microsoft.AspNetCore.Mvc.TagHelpers/Cache/CacheTagKey.cs
Expand Up @@ -6,6 +6,7 @@
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.TagHelpers.Internal;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Internal;
Expand Down Expand Up @@ -151,10 +152,10 @@ public string GenerateHashedKey()
// The key is typically too long to be useful, so we use a cryptographic hash
// as the actual key (better randomization and key distribution, so small vary
// values will generate dramatically different keys).
using (var sha = SHA256.Create())
using (var sha256 = CryptographyAlgorithms.CreateSHA256())
{
var contentBytes = Encoding.UTF8.GetBytes(key);
var hashedBytes = sha.ComputeHash(contentBytes);
var hashedBytes = sha256.ComputeHash(contentBytes);
return Convert.ToBase64String(hashedBytes);
}
}
Expand Down Expand Up @@ -183,14 +184,14 @@ public bool Equals(CacheTagKey other)
AreSame(_headers, other._headers) &&
AreSame(_queries, other._queries) &&
AreSame(_routeValues, other._routeValues) &&
_varyByUser == other._varyByUser &&
_varyByUser == other._varyByUser &&
(!_varyByUser || string.Equals(other._username, _username, StringComparison.Ordinal));
}

/// <inheritdoc />
public override int GetHashCode()
{
// The hashcode is intentionally not using the computed
// The hashcode is intentionally not using the computed
// stringified key in order to prevent string allocations
// in the common case where it's not explicitly required.

Expand Down Expand Up @@ -219,7 +220,7 @@ public override int GetHashCode()

return _hashcode.Value;
}

private static IList<KeyValuePair<string, string>> ExtractCollection<TSourceCollection>(string keys, TSourceCollection collection, Func<TSourceCollection, string, string> accessor)
{
if (string.IsNullOrEmpty(keys))
Expand All @@ -244,7 +245,7 @@ public override int GetHashCode()

return result;
}

private static void AddStringCollection(
StringBuilder builder,
string collectionName,
Expand Down Expand Up @@ -275,7 +276,7 @@ public override int GetHashCode()
.Append(CacheKeyTokenSeparator)
.Append(item.Value);
}

builder.Append(")");
}

Expand Down
@@ -0,0 +1,38 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Security.Cryptography;

namespace Microsoft.AspNetCore.Mvc.TagHelpers.Internal
{
public static class CryptographyAlgorithms
{
#if NETSTANDARD1_6
public static SHA256 CreateSHA256()
{
var sha256 = SHA256.Create();

return sha256;
}
#else
public static SHA256 CreateSHA256()
{
SHA256 sha256;

try
{
sha256 = SHA256.Create();
}
// SHA256.Create is documented to throw this exception on FIPS compliant machines.
// See: https://msdn.microsoft.com/en-us/library/z08hz7ad%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
catch (System.Reflection.TargetInvocationException)
{
// Fallback to a FIPS compliant SHA256 algorithm.
sha256 = new SHA256CryptoServiceProvider();
}

return sha256;
}
#endif
}
}
Expand Up @@ -111,7 +111,7 @@ public string AddFileVersionToPath(string path)

private static string GetHashForFile(IFileInfo fileInfo)
{
using (var sha256 = SHA256.Create())
using (var sha256 = CryptographyAlgorithms.CreateSHA256())
{
using (var readStream = fileInfo.CreateReadStream())
{
Expand Down

0 comments on commit edb5baf

Please sign in to comment.