From: Kevin Jones Date: Wed, 25 Sep 2019 21:09:27 +0000 (-0400) Subject: Implement HashAlgorithmName.FromOid and TryFromOid. X-Git-Tag: submit/tizen/20210909.063632~11031^2~407 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=953407fc3b1e7be15733df3fc5e05655dac97660;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Implement HashAlgorithmName.FromOid and TryFromOid. Commit migrated from https://github.com/dotnet/corefx/commit/eda554cfbcceda02db621ab8233d1b53e653681a --- diff --git a/src/libraries/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.cs b/src/libraries/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.cs index 31ab6da..51281a4 100644 --- a/src/libraries/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.cs +++ b/src/libraries/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.cs @@ -135,10 +135,12 @@ namespace System.Security.Cryptography public static System.Security.Cryptography.HashAlgorithmName SHA512 { get { throw null; } } public override bool Equals(object obj) { throw null; } public bool Equals(System.Security.Cryptography.HashAlgorithmName other) { throw null; } + public static System.Security.Cryptography.HashAlgorithmName FromOid(string oidValue) { throw null; } public override int GetHashCode() { throw null; } public static bool operator ==(System.Security.Cryptography.HashAlgorithmName left, System.Security.Cryptography.HashAlgorithmName right) { throw null; } public static bool operator !=(System.Security.Cryptography.HashAlgorithmName left, System.Security.Cryptography.HashAlgorithmName right) { throw null; } public override string ToString() { throw null; } + public static bool TryFromOid(string oidValue, out System.Security.Cryptography.HashAlgorithmName value) { throw null; } } public abstract partial class HMAC : System.Security.Cryptography.KeyedHashAlgorithm { diff --git a/src/libraries/System.Security.Cryptography.Primitives/src/Resources/Strings.resx b/src/libraries/System.Security.Cryptography.Primitives/src/Resources/Strings.resx index 455ec14..a1b8604 100644 --- a/src/libraries/System.Security.Cryptography.Primitives/src/Resources/Strings.resx +++ b/src/libraries/System.Security.Cryptography.Primitives/src/Resources/Strings.resx @@ -105,6 +105,9 @@ Specified padding mode is not valid for this algorithm. + + The specified OID ({0}) does not represent a known hash algorithm. + Method not supported. Derived class must override. diff --git a/src/libraries/System.Security.Cryptography.Primitives/src/System.Security.Cryptography.Primitives.csproj b/src/libraries/System.Security.Cryptography.Primitives/src/System.Security.Cryptography.Primitives.csproj index b534ffc..a0d092d 100644 --- a/src/libraries/System.Security.Cryptography.Primitives/src/System.Security.Cryptography.Primitives.csproj +++ b/src/libraries/System.Security.Cryptography.Primitives/src/System.Security.Cryptography.Primitives.csproj @@ -35,6 +35,9 @@ Common\System\Security\Cryptography\KeySizeHelpers.cs + + Common\System\Security\Cryptography\Oids.cs + Common\CoreLib\System\Threading\Tasks\TaskToApm.cs diff --git a/src/libraries/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/HashAlgorithmName.cs b/src/libraries/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/HashAlgorithmName.cs index e65fd01..fa802eb 100644 --- a/src/libraries/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/HashAlgorithmName.cs +++ b/src/libraries/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/HashAlgorithmName.cs @@ -104,5 +104,45 @@ namespace System.Security.Cryptography { return !(left == right); } + + public static bool TryFromOid(string oidValue, out HashAlgorithmName value) + { + if (oidValue is null) + { + throw new ArgumentNullException(nameof(oidValue)); + } + + switch (oidValue) + { + case Oids.Md5: + value = MD5; + return true; + case Oids.Sha1: + value = SHA1; + return true; + case Oids.Sha256: + value = SHA256; + return true; + case Oids.Sha384: + value = SHA384; + return true; + case Oids.Sha512: + value = SHA512; + return true; + default: + value = default; + return false; + } + } + + public static HashAlgorithmName FromOid(string oidValue) + { + if (TryFromOid(oidValue, out HashAlgorithmName value)) + { + return value; + } + + throw new CryptographicException(SR.Format(SR.Cryptography_InvalidHashAlgorithmOid, oidValue)); + } } } diff --git a/src/libraries/System.Security.Cryptography.Primitives/tests/HashAlgorithmNameTests.cs b/src/libraries/System.Security.Cryptography.Primitives/tests/HashAlgorithmNameTests.cs new file mode 100644 index 0000000..e94b259 --- /dev/null +++ b/src/libraries/System.Security.Cryptography.Primitives/tests/HashAlgorithmNameTests.cs @@ -0,0 +1,67 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Xunit; + +namespace System.Security.Cryptography.Primitives.Tests +{ + public static class HashAlgorithmNameTests + { + [Theory] + [MemberData(nameof(ValidInputs))] + public static void FromOid_ValidInput(string oid, HashAlgorithmName expected) + { + Assert.Equal(expected, HashAlgorithmName.FromOid(oid)); + } + + [Fact] + public static void FromOid_ThrowsForNullInput() + { + Assert.Throws(() => HashAlgorithmName.FromOid(null)); + } + + [Fact] + public static void FromOid_ThrowsForInvalidInput() + { + CryptographicException exception = Assert.Throws(() => HashAlgorithmName.FromOid("1.2.3.4")); + Assert.Contains("1.2.3.4", exception.Message); + } + + [Fact] + public static void TryFromOid_ThrowsForNullInput() + { + Assert.Throws(() => HashAlgorithmName.TryFromOid(null, out _)); + } + + [Theory] + [MemberData(nameof(ValidInputs))] + public static void TryFromOid_ValidInput(string oid, HashAlgorithmName expected) + { + Assert.True(HashAlgorithmName.TryFromOid(oid, out HashAlgorithmName actual)); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData("1.2.3.4")] + [InlineData("SHA1")] + [InlineData("1.2.840.113549.1.1.5")] + public static void TryFromOid_ReturnsFalseForInvalidInput(string oidValue) + { + Assert.False(HashAlgorithmName.TryFromOid(oidValue, out _)); + } + + public static IEnumerable ValidInputs + { + get + { + yield return new object[] { "1.2.840.113549.2.5", HashAlgorithmName.MD5 }; + yield return new object[] { "1.3.14.3.2.26", HashAlgorithmName.SHA1 }; + yield return new object[] { "2.16.840.1.101.3.4.2.1", HashAlgorithmName.SHA256 }; + yield return new object[] { "2.16.840.1.101.3.4.2.2", HashAlgorithmName.SHA384 }; + yield return new object[] { "2.16.840.1.101.3.4.2.3", HashAlgorithmName.SHA512 }; + } + } + } +} diff --git a/src/libraries/System.Security.Cryptography.Primitives/tests/System.Security.Cryptography.Primitives.Tests.csproj b/src/libraries/System.Security.Cryptography.Primitives/tests/System.Security.Cryptography.Primitives.Tests.csproj index db863d9..9ad368c 100644 --- a/src/libraries/System.Security.Cryptography.Primitives/tests/System.Security.Cryptography.Primitives.Tests.csproj +++ b/src/libraries/System.Security.Cryptography.Primitives/tests/System.Security.Cryptography.Primitives.Tests.csproj @@ -11,6 +11,7 @@ + @@ -26,4 +27,4 @@ - \ No newline at end of file +