Browse Source

Use the new SHA256/SHA384/SHA512.HashData() polyfills

pull/2409/head
Kévin Chalet 1 month ago
parent
commit
04e29a5298
  1. 18
      Directory.Packages.props
  2. 83
      shared/OpenIddict.Extensions/OpenIddictHelpers.cs
  3. 2
      src/OpenIddict.Client/OpenIddictClientModels.cs
  4. 2
      src/OpenIddict.Core/Managers/OpenIddictScopeManager.cs

18
Directory.Packages.props

@ -52,7 +52,7 @@
Note: OpenIddict uses Polyfill to dynamically generate polyfills for types that are not available on
some of the targeted TFMs (e.g Index, Range or nullable attributes on .NET Framework/.NET Standard).
-->
<GlobalPackageReference Include="Polyfill" Condition=" '$(IncludePolyfills)' != 'false' " Version="9.5.0" />
<GlobalPackageReference Include="Polyfill" Condition=" '$(IncludePolyfills)' != 'false' " Version="9.6.0" />
</ItemGroup>
<!--
@ -109,7 +109,7 @@
Note: OpenIddict uses Polyfill to dynamically generate polyfills for types that are not available on
some of the targeted TFMs (e.g Index, Range or nullable attributes on .NET Framework/.NET Standard).
-->
<GlobalPackageReference Include="Polyfill" Condition=" '$(IncludePolyfills)' != 'false' " Version="9.5.0" />
<GlobalPackageReference Include="Polyfill" Condition=" '$(IncludePolyfills)' != 'false' " Version="9.6.0" />
</ItemGroup>
<!--
@ -201,7 +201,7 @@
Note: OpenIddict uses Polyfill to dynamically generate polyfills for types that are not available on
some of the targeted TFMs (e.g Index, Range or nullable attributes on .NET Framework/.NET Standard).
-->
<GlobalPackageReference Include="Polyfill" Condition=" '$(IncludePolyfills)' != 'false' " Version="9.5.0" />
<GlobalPackageReference Include="Polyfill" Condition=" '$(IncludePolyfills)' != 'false' " Version="9.6.0" />
</ItemGroup>
<!--
@ -248,7 +248,7 @@
Note: OpenIddict uses Polyfill to dynamically generate polyfills for types that are not available on
some of the targeted TFMs (e.g Index, Range or nullable attributes on .NET Framework/.NET Standard).
-->
<GlobalPackageReference Include="Polyfill" Condition=" '$(IncludePolyfills)' != 'false' " Version="9.5.0" />
<GlobalPackageReference Include="Polyfill" Condition=" '$(IncludePolyfills)' != 'false' " Version="9.6.0" />
</ItemGroup>
<!--
@ -294,7 +294,7 @@
Note: OpenIddict uses Polyfill to dynamically generate polyfills for types that are not available on
some of the targeted TFMs (e.g Index, Range or nullable attributes on .NET Framework/.NET Standard).
-->
<GlobalPackageReference Include="Polyfill" Condition=" '$(IncludePolyfills)' != 'false' " Version="9.5.0" />
<GlobalPackageReference Include="Polyfill" Condition=" '$(IncludePolyfills)' != 'false' " Version="9.6.0" />
</ItemGroup>
<!--
@ -353,7 +353,7 @@
Note: OpenIddict uses Polyfill to dynamically generate polyfills for types that are not available on
some of the targeted TFMs (e.g Index, Range or nullable attributes on .NET Framework/.NET Standard).
-->
<GlobalPackageReference Include="Polyfill" Condition=" '$(IncludePolyfills)' != 'false' " Version="9.5.0" />
<GlobalPackageReference Include="Polyfill" Condition=" '$(IncludePolyfills)' != 'false' " Version="9.6.0" />
</ItemGroup>
<!--
@ -404,7 +404,7 @@
Note: OpenIddict uses Polyfill to dynamically generate polyfills for types that are not available on
some of the targeted TFMs (e.g Index, Range or nullable attributes on .NET Framework/.NET Standard).
-->
<GlobalPackageReference Include="Polyfill" Condition=" '$(IncludePolyfills)' != 'false' " Version="9.5.0" />
<GlobalPackageReference Include="Polyfill" Condition=" '$(IncludePolyfills)' != 'false' " Version="9.6.0" />
</ItemGroup>
<!--
@ -445,7 +445,7 @@
Note: OpenIddict uses Polyfill to dynamically generate polyfills for types that are not available on
some of the targeted TFMs (e.g Index, Range or nullable attributes on .NET Framework/.NET Standard).
-->
<GlobalPackageReference Include="Polyfill" Condition=" '$(IncludePolyfills)' != 'false' " Version="9.5.0" />
<GlobalPackageReference Include="Polyfill" Condition=" '$(IncludePolyfills)' != 'false' " Version="9.6.0" />
</ItemGroup>
<!--
@ -467,7 +467,7 @@
Note: OpenIddict uses Polyfill to dynamically generate pstring?[]olyfills for types that are not available on
some of the targeted TFMs (e.g Index, Range or nullable attributes on .NET Framework/.NET Standard).
-->
<GlobalPackageReference Include="Polyfill" Condition=" '$(IncludePolyfills)' != 'false' " Version="9.5.0" />
<GlobalPackageReference Include="Polyfill" Condition=" '$(IncludePolyfills)' != 'false' " Version="9.6.0" />
</ItemGroup>
</Project>

83
shared/OpenIddict.Extensions/OpenIddictHelpers.cs

@ -527,33 +527,14 @@ internal static class OpenIddictHelpers
/// </exception>
public static byte[] ComputeSha256Hash(byte[] data)
{
var algorithm = GetAlgorithmFromConfig() switch
using var algorithm = GetAlgorithmFromConfig() switch
{
SHA256 result => result,
null => null,
var result => throw new CryptographicException(SR.FormatID0351(result.GetType().FullName))
};
// If no custom algorithm was registered, use either the static/one-shot HashData() API
// on platforms that support it or create a default instance provided by the BCL.
if (algorithm is null)
{
#if SUPPORTS_ONE_SHOT_HASHING_METHODS
return SHA256.HashData(data);
#else
algorithm = SHA256.Create();
#endif
}
try
{
return algorithm.ComputeHash(data);
}
finally
{
algorithm.Dispose();
}
return algorithm is not null ? algorithm.ComputeHash(data) : SHA256.HashData(data);
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "The default implementation is always used when no custom algorithm was registered.")]
@ -570,33 +551,14 @@ internal static class OpenIddictHelpers
/// </exception>
public static byte[] ComputeSha384Hash(byte[] data)
{
var algorithm = GetAlgorithmFromConfig() switch
using var algorithm = GetAlgorithmFromConfig() switch
{
SHA384 result => result,
null => null,
var result => throw new CryptographicException(SR.FormatID0351(result.GetType().FullName))
};
// If no custom algorithm was registered, use either the static/one-shot HashData() API
// on platforms that support it or create a default instance provided by the BCL.
if (algorithm is null)
{
#if SUPPORTS_ONE_SHOT_HASHING_METHODS
return SHA384.HashData(data);
#else
algorithm = SHA384.Create();
#endif
}
try
{
return algorithm.ComputeHash(data);
}
finally
{
algorithm.Dispose();
}
return algorithm is not null ? algorithm.ComputeHash(data) : SHA384.HashData(data);
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "The default implementation is always used when no custom algorithm was registered.")]
@ -613,33 +575,14 @@ internal static class OpenIddictHelpers
/// </exception>
public static byte[] ComputeSha512Hash(byte[] data)
{
var algorithm = GetAlgorithmFromConfig() switch
using var algorithm = GetAlgorithmFromConfig() switch
{
SHA512 result => result,
null => null,
var result => throw new CryptographicException(SR.FormatID0351(result.GetType().FullName))
};
// If no custom algorithm was registered, use either the static/one-shot HashData() API
// on platforms that support it or create a default instance provided by the BCL.
if (algorithm is null)
{
#if SUPPORTS_ONE_SHOT_HASHING_METHODS
return SHA512.HashData(data);
#else
algorithm = SHA512.Create();
#endif
}
try
{
return algorithm.ComputeHash(data);
}
finally
{
algorithm.Dispose();
}
return algorithm is not null ? algorithm.ComputeHash(data) : SHA512.HashData(data);
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "The default implementation is always used when no custom algorithm was registered.")]
@ -663,17 +606,15 @@ internal static class OpenIddictHelpers
var result => throw new CryptographicException(SR.FormatID0351(result.GetType().FullName))
};
// If no custom random number generator was registered, use either the static GetBytes() or
// Fill() APIs on platforms that support them or create a default instance provided by the BCL.
if (algorithm is null)
if (algorithm is not null)
{
return RandomNumberGenerator.GetBytes(size / 8);
}
var array = new byte[size / 8];
algorithm.GetBytes(array);
var array = new byte[size / 8];
algorithm.GetBytes(array);
return array;
}
return array;
return RandomNumberGenerator.GetBytes(size / 8);
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "The default implementation is always used when no custom algorithm was registered.")]

2
src/OpenIddict.Client/OpenIddictClientModels.cs

@ -26,7 +26,7 @@ public static class OpenIddictClientModels
public CancellationToken CancellationToken { get; init; }
/// <summary>
/// Gets or sets the nonce that was returned during the challenge operation.
/// Gets or sets the nonce that was returned during the challenge or sign-out operation.
/// </summary>
public required string Nonce { get; init; }

2
src/OpenIddict.Core/Managers/OpenIddictScopeManager.cs

@ -849,7 +849,7 @@ public class OpenIddictScopeManager<TScope> : IOpenIddictScopeManager where TSco
yield return new ValidationResult(SR.GetResourceString(SR.ID2044));
}
else if (name!.Contains(Separators.Space[0]))
else if (name.Contains(Separators.Space[0]))
{
yield return new ValidationResult(SR.GetResourceString(SR.ID2045));
}

Loading…
Cancel
Save