This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change SHA256 algorithm to work on FIPS-compliant machines.
- Loading branch information
1 parent
a44275b
commit cdf84eb
Showing
3 changed files
with
40 additions
and
2 deletions.
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
38 changes: 38 additions & 0 deletions
38
src/Microsoft.AspNetCore.Antiforgery/Internal/CryptographyAlgorithms.cs
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,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.Antiforgery.Internal | ||
{ | ||
public static class CryptographyAlgorithms | ||
{ | ||
#if NETSTANDARD1_3 | ||
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 | ||
} | ||
} |