From c22ad8c049927d5189c16cfad241ee8ff834696d Mon Sep 17 00:00:00 2001 From: Kevin Jones Date: Sat, 25 Jan 2020 13:00:12 -0500 Subject: [PATCH] Initialize HashSizeValue for digest algorithms. The abstract types SHAx and MD5 did not set HashSizeValue, which would lead to derived types to have HashSize a value of 0. This initializes the field to match .NET Framework behavior. --- .../src/System/Security/Cryptography/MD5.cs | 5 +- .../src/System/Security/Cryptography/SHA1.cs | 5 +- .../System/Security/Cryptography/SHA256.cs | 5 +- .../System/Security/Cryptography/SHA384.cs | 5 +- .../System/Security/Cryptography/SHA512.cs | 5 +- .../tests/HashDerivedTests.cs | 88 +++++++++++++++++++ ...urity.Cryptography.Algorithms.Tests.csproj | 1 + 7 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 src/libraries/System.Security.Cryptography.Algorithms/tests/HashDerivedTests.cs diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/MD5.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/MD5.cs index 44544bea49a67c..6a2515e20c4926 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/MD5.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/MD5.cs @@ -14,7 +14,10 @@ namespace System.Security.Cryptography public abstract class MD5 : HashAlgorithm { - protected MD5() { } + protected MD5() + { + HashSizeValue = 128; + } public static new MD5 Create() => new Implementation(); diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA1.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA1.cs index 7de08ada0af265..4e82df026bb247 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA1.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA1.cs @@ -14,7 +14,10 @@ namespace System.Security.Cryptography public abstract class SHA1 : HashAlgorithm { - protected SHA1() { } + protected SHA1() + { + HashSizeValue = 160; + } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA5350", Justification = "This is the implementaton of SHA1")] public static new SHA1 Create() => new Implementation(); diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA256.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA256.cs index 0e8d2980b4c269..194d06d17a1b89 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA256.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA256.cs @@ -14,7 +14,10 @@ namespace System.Security.Cryptography public abstract class SHA256 : HashAlgorithm { - protected SHA256() { } + protected SHA256() + { + HashSizeValue = 256; + } public static new SHA256 Create() => new Implementation(); diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA384.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA384.cs index 242d7cfa9c17a8..467846ae9c159e 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA384.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA384.cs @@ -14,7 +14,10 @@ namespace System.Security.Cryptography public abstract class SHA384 : HashAlgorithm { - protected SHA384() { } + protected SHA384() + { + HashSizeValue = 384; + } public static new SHA384 Create() => new Implementation(); diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA512.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA512.cs index fcf19de9576462..cc640898e61b38 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA512.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA512.cs @@ -14,7 +14,10 @@ namespace System.Security.Cryptography public abstract class SHA512 : HashAlgorithm { - protected SHA512() { } + protected SHA512() + { + HashSizeValue = 512; + } public static new SHA512 Create() => new Implementation(); diff --git a/src/libraries/System.Security.Cryptography.Algorithms/tests/HashDerivedTests.cs b/src/libraries/System.Security.Cryptography.Algorithms/tests/HashDerivedTests.cs new file mode 100644 index 00000000000000..861637b61be63b --- /dev/null +++ b/src/libraries/System.Security.Cryptography.Algorithms/tests/HashDerivedTests.cs @@ -0,0 +1,88 @@ +// 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 Xunit; + +namespace System.Security.Cryptography.Hashing.Algorithms.Tests +{ + public static class HashDerivedTests + { + [Fact] + public static void HashSize_SetForDerived_SHA1() + { + using DerivedSHA1 sha = new DerivedSHA1(); + Assert.Equal(160, sha.HashSize); + } + + [Fact] + public static void HashSize_SetForDerived_SHA256() + { + using DerivedSHA256 sha = new DerivedSHA256(); + Assert.Equal(256, sha.HashSize); + } + + [Fact] + public static void HashSize_SetForDerived_SHA384() + { + using DerivedSHA384 sha = new DerivedSHA384(); + Assert.Equal(384, sha.HashSize); + } + + [Fact] + public static void HashSize_SetForDerived_SHA512() + { + using DerivedSHA512 sha = new DerivedSHA512(); + Assert.Equal(512, sha.HashSize); + } + + [Fact] + public static void HashSize_SetForDerived_MD5() + { + using DerivedMD5 sha = new DerivedMD5(); + Assert.Equal(128, sha.HashSize); + } + + private class DerivedSHA1 : SHA1 + { + public override void Initialize() => throw null; + protected override byte[] HashFinal() => throw null; + protected override void HashCore(byte[] array, int ibStart, int cbSize) => throw null; + } + + private class DerivedSHA256 : SHA256 + { + public override void Initialize() => throw null; + protected override byte[] HashFinal() => throw null; + protected override void HashCore(byte[] array, int ibStart, int cbSize) => throw null; + } + + private class DerivedSHA384 : SHA384 + { + public override void Initialize() => throw null; + protected override byte[] HashFinal() => throw null; + protected override void HashCore(byte[] array, int ibStart, int cbSize) => throw null; + } + + private class DerivedSHA512 : SHA512 + { + public override void Initialize() => throw null; + protected override byte[] HashFinal() => throw null; + protected override void HashCore(byte[] array, int ibStart, int cbSize) => throw null; + } + + private class DerivedMD5 : MD5 + { + public override void Initialize() => throw null; + protected override byte[] HashFinal() => throw null; + protected override void HashCore(byte[] array, int ibStart, int cbSize) => throw null; + } + + private class DerivedHMACMD5 : HMACMD5 + { + public override void Initialize() => throw null; + protected override byte[] HashFinal() => throw null; + protected override void HashCore(byte[] array, int ibStart, int cbSize) => throw null; + } + } +} diff --git a/src/libraries/System.Security.Cryptography.Algorithms/tests/System.Security.Cryptography.Algorithms.Tests.csproj b/src/libraries/System.Security.Cryptography.Algorithms/tests/System.Security.Cryptography.Algorithms.Tests.csproj index a8f210627b36d8..23ad602608c41a 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/tests/System.Security.Cryptography.Algorithms.Tests.csproj +++ b/src/libraries/System.Security.Cryptography.Algorithms/tests/System.Security.Cryptography.Algorithms.Tests.csproj @@ -188,6 +188,7 @@ +