Implement HashAlgorithmName.FromOid and TryFromOid.
authorKevin Jones <kevin@vcsjones.com>
Wed, 25 Sep 2019 21:09:27 +0000 (17:09 -0400)
committerJeremy Barton <jbarton@microsoft.com>
Wed, 25 Sep 2019 21:09:27 +0000 (14:09 -0700)
Commit migrated from https://github.com/dotnet/corefx/commit/eda554cfbcceda02db621ab8233d1b53e653681a

src/libraries/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.cs
src/libraries/System.Security.Cryptography.Primitives/src/Resources/Strings.resx
src/libraries/System.Security.Cryptography.Primitives/src/System.Security.Cryptography.Primitives.csproj
src/libraries/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/HashAlgorithmName.cs
src/libraries/System.Security.Cryptography.Primitives/tests/HashAlgorithmNameTests.cs [new file with mode: 0644]
src/libraries/System.Security.Cryptography.Primitives/tests/System.Security.Cryptography.Primitives.Tests.csproj

index 31ab6da..51281a4 100644 (file)
@@ -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
     {
index 455ec14..a1b8604 100644 (file)
   <data name="Cryptography_InvalidPaddingMode" xml:space="preserve">
     <value>Specified padding mode is not valid for this algorithm.</value>
   </data>
+  <data name="Cryptography_InvalidHashAlgorithmOid" xml:space="preserve">
+    <value>The specified OID ({0}) does not represent a known hash algorithm.</value>
+  </data>
   <data name="NotSupported_SubclassOverride" xml:space="preserve">
     <value>Method not supported. Derived class must override.</value>
   </data>
index b534ffc..a0d092d 100644 (file)
@@ -35,6 +35,9 @@
     <Compile Include="$(CommonPath)\System\Security\Cryptography\KeySizeHelpers.cs">
       <Link>Common\System\Security\Cryptography\KeySizeHelpers.cs</Link>
     </Compile>
+    <Compile Include="$(CommonPath)\System\Security\Cryptography\Oids.cs">
+      <Link>Common\System\Security\Cryptography\Oids.cs</Link>
+    </Compile>
     <Compile Include="$(CommonPath)\CoreLib\System\Threading\Tasks\TaskToApm.cs">
       <Link>Common\CoreLib\System\Threading\Tasks\TaskToApm.cs</Link>
     </Compile>
index e65fd01..fa802eb 100644 (file)
@@ -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 (file)
index 0000000..e94b259
--- /dev/null
@@ -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<ArgumentNullException>(() => HashAlgorithmName.FromOid(null));
+        }
+
+        [Fact]
+        public static void FromOid_ThrowsForInvalidInput()
+        {
+            CryptographicException exception = Assert.Throws<CryptographicException>(() => HashAlgorithmName.FromOid("1.2.3.4"));
+            Assert.Contains("1.2.3.4", exception.Message);
+        }
+
+        [Fact]
+        public static void TryFromOid_ThrowsForNullInput()
+        {
+            Assert.Throws<ArgumentNullException>(() => 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<object[]> 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 };
+            }
+        }
+    }
+}
index db863d9..9ad368c 100644 (file)
@@ -11,6 +11,7 @@
     <Compile Include="CryptoConfigTests.cs" />
     <Compile Include="CryptoStream.cs" />
     <Compile Include="CryptographicException.cs" />
+    <Compile Include="HashAlgorithmNameTests.cs" />
     <Compile Include="HmacAlgorithmTest.cs" />
     <Compile Include="KeyedHashAlgorithmTests.cs" />
     <Compile Include="Length32Hash.cs" />
@@ -26,4 +27,4 @@
     <Compile Include="PbeParametersTests.cs" />
     <Compile Include="ZeroMemoryTests.cs" />
   </ItemGroup>
-</Project>
\ No newline at end of file
+</Project>