未验证 提交 42cef019 编写于 作者: A Alexander Köplinger 提交者: GitHub

Implement CryptoConfig.CreateFromName() for wasm (#51750)

Hardcodes the mapping for the SHA* hashes supported on WebAssembly from https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptoconfig?view=net-5.0#remarks

Fixes https://github.com/dotnet/runtime/issues/44996
上级 5f8f7977
......@@ -101,13 +101,17 @@ public partial class CryptoConfig
{
public CryptoConfig() { }
public static bool AllowOnlyFipsAlgorithms { get { throw null; } }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public static void AddAlgorithm(System.Type algorithm, params string[] names) { }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public static void AddOID(string oid, params string[] names) { }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead.")]
public static object? CreateFromName(string name) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead.")]
public static object? CreateFromName(string name, params object?[]? args) { throw null; }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public static byte[] EncodeOID(string str) { throw null; }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public static string? MapNameToOID(string name) { throw null; }
}
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
......
T:System.Security.Cryptography.CryptoConfig
T:System.Security.Cryptography.RandomNumberGenerator
T:System.Security.Cryptography.IncrementalHash
T:System.Security.Cryptography.SHA1
......
......@@ -675,6 +675,7 @@
<Compile Include="Internal\Cryptography\RandomNumberGeneratorImplementation.cs" />
<Compile Include="Internal\Cryptography\RandomNumberGeneratorImplementation.Browser.cs" />
<Compile Include="Internal\Cryptography\SHAHashProvider.Browser.cs" />
<Compile Include="System\Security\Cryptography\CryptoConfig.Browser.cs" />
<Compile Include="System\Security\Cryptography\RandomNumberGenerator.cs" />
<Compile Include="System\Security\Cryptography\IncrementalHash.cs" />
<Compile Include="System\Security\Cryptography\SHA1.cs" />
......
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Runtime.Versioning;
namespace System.Security.Cryptography
{
public partial class CryptoConfig
{
[UnsupportedOSPlatform("browser")]
public static void AddAlgorithm(Type algorithm, params string[] names) => throw new PlatformNotSupportedException(SR.SystemSecurityCryptographyAlgorithms_PlatformNotSupported);
[UnsupportedOSPlatform("browser")]
public static void AddOID(string oid, params string[] names) => throw new PlatformNotSupportedException(SR.SystemSecurityCryptographyAlgorithms_PlatformNotSupported);
[UnsupportedOSPlatform("browser")]
public static string? MapNameToOID(string name) => throw new PlatformNotSupportedException(SR.SystemSecurityCryptographyAlgorithms_PlatformNotSupported);
[UnsupportedOSPlatform("browser")]
public static byte[] EncodeOID(string str) => throw new PlatformNotSupportedException(SR.SystemSecurityCryptographyAlgorithms_PlatformNotSupported);
[RequiresUnreferencedCode("The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead.")]
public static object? CreateFromName(string name, params object?[]? args)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
switch (name)
{
// hardcode mapping for SHA* algorithm names from https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptoconfig?view=net-5.0#remarks
case "SHA":
case "SHA1":
case "System.Security.Cryptography.SHA1":
return new SHA1Managed();
case "SHA256":
case "SHA-256":
case "System.Security.Cryptography.SHA256":
return new SHA256Managed();
case "SHA384":
case "SHA-384":
case "System.Security.Cryptography.SHA384":
return new SHA384Managed();
case "SHA512":
case "SHA-512":
case "System.Security.Cryptography.SHA512":
return new SHA512Managed();
}
return null;
}
[RequiresUnreferencedCode(CreateFromNameUnreferencedCodeMessage)]
public static object? CreateFromName(string name)
{
return CreateFromName(name, null);
}
}
}
......@@ -6,5 +6,8 @@ namespace System.Security.Cryptography
public partial class CryptoConfig
{
internal const string CreateFromNameUnreferencedCodeMessage = "The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead.";
// .NET Core does not support AllowOnlyFipsAlgorithms
public static bool AllowOnlyFipsAlgorithms => false;
}
}
......@@ -7,6 +7,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Runtime.Versioning;
namespace System.Security.Cryptography
{
......@@ -37,9 +38,6 @@ public partial class CryptoConfig
private static readonly ConcurrentDictionary<string, Type> appNameHT = new ConcurrentDictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
private static readonly ConcurrentDictionary<string, string> appOidHT = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
// .NET Core does not support AllowOnlyFipsAlgorithms
public static bool AllowOnlyFipsAlgorithms => false;
private static Dictionary<string, string> DefaultOidHT
{
get
......@@ -294,6 +292,7 @@ public partial class CryptoConfig
}
}
[UnsupportedOSPlatform("browser")]
public static void AddAlgorithm(Type algorithm, params string[] names)
{
if (algorithm == null)
......@@ -450,6 +449,7 @@ public static void AddAlgorithm(Type algorithm, params string[] names)
return CreateFromName(name, null);
}
[UnsupportedOSPlatform("browser")]
public static void AddOID(string oid, params string[] names)
{
if (oid == null)
......@@ -477,6 +477,7 @@ public static void AddOID(string oid, params string[] names)
}
}
[UnsupportedOSPlatform("browser")]
public static string? MapNameToOID(string name)
{
if (name == null)
......@@ -497,6 +498,7 @@ public static void AddOID(string oid, params string[] names)
return oidName;
}
[UnsupportedOSPlatform("browser")]
public static byte[] EncodeOID(string str)
{
if (str == null)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册