Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
/MvcPublic archive

Commitedb5baf

Browse files
author
N. Taylor Mullen
committed
Change SHA256 algorithm to work on FIPS-compliant machines.
#5103
1 parent6e5fd4f commitedb5baf

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

‎src/Microsoft.AspNetCore.Mvc.TagHelpers/Cache/CacheTagKey.cs‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
usingSystem.Security.Cryptography;
77
usingSystem.Text;
88
usingMicrosoft.AspNetCore.Http;
9+
usingMicrosoft.AspNetCore.Mvc.TagHelpers.Internal;
910
usingMicrosoft.AspNetCore.Razor.TagHelpers;
1011
usingMicrosoft.AspNetCore.Routing;
1112
usingMicrosoft.Extensions.Internal;
@@ -151,10 +152,10 @@ public string GenerateHashedKey()
151152
// The key is typically too long to be useful, so we use a cryptographic hash
152153
// as the actual key (better randomization and key distribution, so small vary
153154
// values will generate dramatically different keys).
154-
using(varsha=SHA256.Create())
155+
using(varsha256=CryptographyAlgorithms.CreateSHA256())
155156
{
156157
varcontentBytes=Encoding.UTF8.GetBytes(key);
157-
varhashedBytes=sha.ComputeHash(contentBytes);
158+
varhashedBytes=sha256.ComputeHash(contentBytes);
158159
returnConvert.ToBase64String(hashedBytes);
159160
}
160161
}
@@ -183,14 +184,14 @@ public bool Equals(CacheTagKey other)
183184
AreSame(_headers,other._headers)&&
184185
AreSame(_queries,other._queries)&&
185186
AreSame(_routeValues,other._routeValues)&&
186-
_varyByUser==other._varyByUser&&
187+
_varyByUser==other._varyByUser&&
187188
(!_varyByUser||string.Equals(other._username,_username,StringComparison.Ordinal));
188189
}
189190

190191
/// <inheritdoc />
191192
publicoverrideintGetHashCode()
192193
{
193-
// The hashcode is intentionally not using the computed
194+
// The hashcode is intentionally not using the computed
194195
// stringified key in order to prevent string allocations
195196
// in the common case where it's not explicitly required.
196197

@@ -219,7 +220,7 @@ public override int GetHashCode()
219220

220221
return_hashcode.Value;
221222
}
222-
223+
223224
privatestaticIList<KeyValuePair<string,string>>ExtractCollection<TSourceCollection>(stringkeys,TSourceCollectioncollection,Func<TSourceCollection,string,string>accessor)
224225
{
225226
if(string.IsNullOrEmpty(keys))
@@ -244,7 +245,7 @@ private static IList<KeyValuePair<string, string>> ExtractCollection<TSourceColl
244245

245246
returnresult;
246247
}
247-
248+
248249
privatestaticvoidAddStringCollection(
249250
StringBuilderbuilder,
250251
stringcollectionName,
@@ -275,7 +276,7 @@ private static void AddStringCollection(
275276
.Append(CacheKeyTokenSeparator)
276277
.Append(item.Value);
277278
}
278-
279+
279280
builder.Append(")");
280281
}
281282

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
usingSystem.Security.Cryptography;
5+
6+
namespaceMicrosoft.AspNetCore.Mvc.TagHelpers.Internal
7+
{
8+
publicstaticclassCryptographyAlgorithms
9+
{
10+
#ifNETSTANDARD1_6
11+
publicstaticSHA256CreateSHA256()
12+
{
13+
varsha256=SHA256.Create();
14+
15+
returnsha256;
16+
}
17+
#else
18+
publicstaticSHA256CreateSHA256()
19+
{
20+
SHA256sha256;
21+
22+
try
23+
{
24+
sha256=SHA256.Create();
25+
}
26+
// SHA256.Create is documented to throw this exception on FIPS compliant machines.
27+
// See: https://msdn.microsoft.com/en-us/library/z08hz7ad%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
28+
catch(System.Reflection.TargetInvocationException)
29+
{
30+
// Fallback to a FIPS compliant SHA256 algorithm.
31+
sha256=newSHA256CryptoServiceProvider();
32+
}
33+
34+
returnsha256;
35+
}
36+
#endif
37+
}
38+
}

‎src/Microsoft.AspNetCore.Mvc.TagHelpers/Internal/FileVersionProvider.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public string AddFileVersionToPath(string path)
111111

112112
privatestaticstringGetHashForFile(IFileInfofileInfo)
113113
{
114-
using(varsha256=SHA256.Create())
114+
using(varsha256=CryptographyAlgorithms.CreateSHA256())
115115
{
116116
using(varreadStream=fileInfo.CreateReadStream())
117117
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp