From 0febe51f2ce991ab1a97715a7a00959ce69e1967 Mon Sep 17 00:00:00 2001 From: Wz Chua <13881045+wzchua@users.noreply.github.com> Date: Sun, 7 Feb 2021 10:05:59 +0800 Subject: [PATCH 1/3] Update to use HashData method --- .../System/Net/Http/HttpClientHandlerTest.cs | 2 +- .../tests/System/Net/Http/TestHelper.cs | 5 +-- .../DSA/DSASignatureFormatter.cs | 6 +-- .../RSA/SignVerify.cs | 42 +++---------------- .../X509Certificates/CertificateAuthority.cs | 5 +-- .../tests/mono/System.Drawing/BitmapTests.cs | 8 ++-- .../tests/WebClientTest.cs | 11 ++--- .../Net/WebSockets/WebSocketHandle.Managed.cs | 9 ++-- .../tests/LoopbackHelper.cs | 3 +- .../tests/RsaCngTests.cs | 2 +- .../tests/DSACryptoServiceProviderTests.cs | 30 +++---------- .../RSACryptoServiceProviderBackCompat.cs | 5 +-- .../tests/RSACryptoServiceProviderTests.cs | 18 ++------ .../tests/Rfc3161/TimestampTokenTests.cs | 19 ++++----- .../SignedCms/SignedCmsWholeDocumentTests.cs | 18 +++----- 15 files changed, 46 insertions(+), 137 deletions(-) diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs index 885a202a229ec..65006aa10c26e 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs @@ -569,7 +569,7 @@ await LoopbackServerFactory.CreateClientAndServerAsync(async uri => request.Headers.Connection.Add("close"); request.Headers.Add("Cookie", "$Version=1; Skin=new"); request.Content.Headers.ContentLength = contentArray.Length; - request.Content.Headers.ContentMD5 = MD5.Create().ComputeHash(contentArray); + request.Content.Headers.ContentMD5 = MD5.HashData(contentArray); request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); request.Headers.Date = DateTimeOffset.Parse("Tue, 15 Nov 1994 08:12:31 GMT"); request.Headers.Expect.Add(new NameValueWithParametersHeaderValue("100-continue")); diff --git a/src/libraries/Common/tests/System/Net/Http/TestHelper.cs b/src/libraries/Common/tests/System/Net/Http/TestHelper.cs index d0f0d368a79f4..bc03dfacc554e 100644 --- a/src/libraries/Common/tests/System/Net/Http/TestHelper.cs +++ b/src/libraries/Common/tests/System/Net/Http/TestHelper.cs @@ -78,10 +78,7 @@ public static byte[] ComputeMD5Hash(string data) public static byte[] ComputeMD5Hash(byte[] data) { - using (MD5 md5 = MD5.Create()) - { - return md5.ComputeHash(data); - } + return MD5.HashData(data); } public static Task WhenAllCompletedOrAnyFailed(params Task[] tasks) diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/DSA/DSASignatureFormatter.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/DSA/DSASignatureFormatter.cs index 867e87c34dfa6..22c3a02648a57 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/DSA/DSASignatureFormatter.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/DSA/DSASignatureFormatter.cs @@ -59,11 +59,7 @@ public static void VerifyKnownSignature() DSAParameters dsaParameters; DSATestData.GetDSA1024_186_2(out dsaParameters, out signature, out data); - byte[] hash; - using (SHA1 alg = SHA1.Create()) - { - hash = alg.ComputeHash(data); - } + byte[] hash = SHA1.HashData(data); dsa.ImportParameters(dsaParameters); var deformatter = new DSASignatureDeformatter(dsa); diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/SignVerify.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/SignVerify.cs index afeddd9be65de..37bb5bb15fbac 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/SignVerify.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/SignVerify.cs @@ -645,12 +645,7 @@ public void ExpectedHashSignature_SHA1_2048() 0xAE, 0xBD, 0xA5, 0x57, 0x2C, 0xEC, 0x3D, 0x0D, }; - byte[] dataHash; - - using (HashAlgorithm hash = SHA1.Create()) - { - dataHash = hash.ComputeHash(TestData.HelloBytes); - } + byte[] dataHash = SHA1.HashData(TestData.HelloBytes); ExpectHashSignature(expectedHashSignature, dataHash, "SHA1", TestData.RSA2048Params); } @@ -678,12 +673,7 @@ public void ExpectedHashSignature_SHA256_1024() 0x70, 0x95, 0x25, 0x49, 0x8A, 0x1A, 0xD7, 0xFC, }; - byte[] dataHash; - - using (HashAlgorithm hash = SHA256.Create()) - { - dataHash = hash.ComputeHash(TestData.HelloBytes); - } + byte[] dataHash = SHA256.HashData(TestData.HelloBytes); ExpectHashSignature(expectedHashSignature, dataHash, "SHA256", TestData.RSA1024Params); } @@ -727,12 +717,7 @@ public void ExpectedHashSignature_SHA256_2048() 0x6B, 0x9B, 0x97, 0x2D, 0x8C, 0xB2, 0x1C, 0x16, }; - byte[] dataHash; - - using (HashAlgorithm hash = SHA256.Create()) - { - dataHash = hash.ComputeHash(TestData.HelloBytes); - } + byte[] dataHash = SHA256.HashData(TestData.HelloBytes); ExpectHashSignature(expectedHashSignature, dataHash, "SHA256", TestData.RSA2048Params); } @@ -776,12 +761,7 @@ public void VerifyHashSignature_SHA1_2048() 0xAE, 0xBD, 0xA5, 0x57, 0x2C, 0xEC, 0x3D, 0x0D, }; - byte[] dataHash; - - using (HashAlgorithm hash = SHA1.Create()) - { - dataHash = hash.ComputeHash(TestData.HelloBytes); - } + byte[] dataHash = SHA1.HashData(TestData.HelloBytes); VerifyHashSignature(hashSignature, dataHash, "SHA1", TestData.RSA2048Params); } @@ -809,12 +789,7 @@ public void VerifyHashSignature_SHA256_1024() 0x70, 0x95, 0x25, 0x49, 0x8A, 0x1A, 0xD7, 0xFC, }; - byte[] dataHash; - - using (HashAlgorithm hash = SHA256.Create()) - { - dataHash = hash.ComputeHash(TestData.HelloBytes); - } + byte[] dataHash = SHA256.HashData(TestData.HelloBytes); VerifyHashSignature(hashSignature, dataHash, "SHA256", TestData.RSA1024Params); } @@ -858,12 +833,7 @@ public void VerifyHashSignature_SHA256_2048() 0x6B, 0x9B, 0x97, 0x2D, 0x8C, 0xB2, 0x1C, 0x16, }; - byte[] dataHash; - - using (HashAlgorithm hash = SHA256.Create()) - { - dataHash = hash.ComputeHash(TestData.HelloBytes); - } + byte[] dataHash = SHA256.HashData(TestData.HelloBytes); VerifyHashSignature(hashSignature, dataHash, "SHA256", TestData.RSA2048Params); } diff --git a/src/libraries/Common/tests/System/Security/Cryptography/X509Certificates/CertificateAuthority.cs b/src/libraries/Common/tests/System/Security/Cryptography/X509Certificates/CertificateAuthority.cs index a2227ddd1a448..1257b8a0db8d2 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/X509Certificates/CertificateAuthority.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/X509Certificates/CertificateAuthority.cs @@ -642,10 +642,7 @@ private CertStatus CheckRevocation(ReadOnlyMemory certId, ref DateTimeOffs if (_dnHash == null) { - using (HashAlgorithm hash = SHA1.Create()) - { - _dnHash = hash.ComputeHash(_cert.SubjectName.RawData); - } + _dnHash = SHA1.HashData(_cert.SubjectName.RawData); } if (!idReader.TryReadPrimitiveOctetString(out ReadOnlyMemory reqDn)) diff --git a/src/libraries/System.Drawing.Common/tests/mono/System.Drawing/BitmapTests.cs b/src/libraries/System.Drawing.Common/tests/mono/System.Drawing/BitmapTests.cs index 273425380cb73..8546f46001279 100644 --- a/src/libraries/System.Drawing.Common/tests/mono/System.Drawing/BitmapTests.cs +++ b/src/libraries/System.Drawing.Common/tests/mono/System.Drawing/BitmapTests.cs @@ -423,7 +423,7 @@ public string RotateBmp(Bitmap src, RotateFlipType rotate) } } - hash = MD5.Create().ComputeHash(pixels); + hash = MD5.HashData(pixels); return ByteArrayToString(hash); } } @@ -482,7 +482,7 @@ public string RotateIndexedBmp(Bitmap src, RotateFlipType type) if (pixel_data == null) return "--ERROR--"; - byte[] hash = MD5.Create().ComputeHash(pixel_data); + byte[] hash = MD5.HashData(pixel_data); return ByteArrayToString(hash); } } @@ -574,7 +574,7 @@ private byte[] HashPixels(Bitmap bmp) pixels[index++] = clr.B; } } - return MD5.Create().ComputeHash(pixels); + return MD5.HashData(pixels); } private byte[] HashLock(Bitmap bmp, int width, int height, PixelFormat fmt, ImageLockMode mode) @@ -620,7 +620,7 @@ private byte[] HashLock(Bitmap bmp, int width, int height, PixelFormat fmt, Imag { bmp.UnlockBits(bd); } - return MD5.Create().ComputeHash(pixels); + return MD5.HashData(pixels); } // Tests the LockBitmap functions. Makes a hash of the block of pixels that it returns diff --git a/src/libraries/System.Net.WebClient/tests/WebClientTest.cs b/src/libraries/System.Net.WebClient/tests/WebClientTest.cs index b727562f22a14..f168d4a9eb071 100644 --- a/src/libraries/System.Net.WebClient/tests/WebClientTest.cs +++ b/src/libraries/System.Net.WebClient/tests/WebClientTest.cs @@ -709,13 +709,10 @@ public async Task UploadValues_Success(Uri echoServer) private static void AddMD5Header(WebClient wc, string data) { - using (MD5 md5 = MD5.Create()) - { - // Compute MD5 hash of the data that will be uploaded. We convert the string to UTF-8 since - // that is the encoding used by WebClient when serializing the data on the wire. - string headerValue = Convert.ToBase64String(md5.ComputeHash(Encoding.UTF8.GetBytes(data))); - wc.Headers.Add("Content-MD5", headerValue); - } + // Compute MD5 hash of the data that will be uploaded. We convert the string to UTF-8 since + // that is the encoding used by WebClient when serializing the data on the wire. + string headerValue = Convert.ToBase64String(MD5.HashData(Encoding.UTF8.GetBytes(data))); + wc.Headers.Add("Content-MD5", headerValue); } } diff --git a/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.Managed.cs b/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.Managed.cs index 92732ab643aee..a378566af65ca 100644 --- a/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.Managed.cs +++ b/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.Managed.cs @@ -243,12 +243,9 @@ private static void AddWebSocketHeaders(HttpRequestMessage request, string secKe private static KeyValuePair CreateSecKeyAndSecWebSocketAccept() { string secKey = Convert.ToBase64String(Guid.NewGuid().ToByteArray()); - using (SHA1 sha = SHA1.Create()) - { - return new KeyValuePair( - secKey, - Convert.ToBase64String(sha.ComputeHash(Encoding.ASCII.GetBytes(secKey + WSServerGuid)))); - } + return new KeyValuePair( + secKey, + Convert.ToBase64String(SHA1.HashData(Encoding.ASCII.GetBytes(secKey + WSServerGuid)))); } private static void ValidateHeader(HttpHeaders headers, string name, string expectedValue) diff --git a/src/libraries/System.Net.WebSockets.Client/tests/LoopbackHelper.cs b/src/libraries/System.Net.WebSockets.Client/tests/LoopbackHelper.cs index 82ae0bf901be0..5726326c6ab8f 100644 --- a/src/libraries/System.Net.WebSockets.Client/tests/LoopbackHelper.cs +++ b/src/libraries/System.Net.WebSockets.Client/tests/LoopbackHelper.cs @@ -56,8 +56,7 @@ private static string ComputeWebSocketHandshakeSecurityAcceptValue(string secWeb string combinedKey = secWebSocketKey + Rfc6455Guid; // Use of SHA1 hash is required by RFC 6455. - SHA1 sha1Provider = new SHA1CryptoServiceProvider(); - byte[] sha1Hash = sha1Provider.ComputeHash(Encoding.UTF8.GetBytes(combinedKey)); + byte[] sha1Hash = SHA1.HashData(Encoding.UTF8.GetBytes(combinedKey)); return Convert.ToBase64String(sha1Hash); } } diff --git a/src/libraries/System.Security.Cryptography.Cng/tests/RsaCngTests.cs b/src/libraries/System.Security.Cryptography.Cng/tests/RsaCngTests.cs index ea99779be3efe..1a4439232849e 100644 --- a/src/libraries/System.Security.Cryptography.Cng/tests/RsaCngTests.cs +++ b/src/libraries/System.Security.Cryptography.Cng/tests/RsaCngTests.cs @@ -19,7 +19,7 @@ public static class RsaCngTests public static void SignVerifyHashRoundTrip() { byte[] message = "781021abcd982139a8bc91387870ac01".HexToByteArray(); - byte[] hash = SHA1.Create().ComputeHash(message); + byte[] hash = SHA1.HashData(message); TestSignVerifyHashRoundTrip(hash, HashAlgorithmName.SHA1, RSASignaturePadding.Pss, 0x100); } diff --git a/src/libraries/System.Security.Cryptography.Csp/tests/DSACryptoServiceProviderTests.cs b/src/libraries/System.Security.Cryptography.Csp/tests/DSACryptoServiceProviderTests.cs index 5ac96578f6a3d..d2a64c4a96fd8 100644 --- a/src/libraries/System.Security.Cryptography.Csp/tests/DSACryptoServiceProviderTests.cs +++ b/src/libraries/System.Security.Cryptography.Csp/tests/DSACryptoServiceProviderTests.cs @@ -248,11 +248,7 @@ public static void ImportParameters_KeyTooBig_Throws() [ConditionalFact(nameof(SupportsKeyGeneration))] public static void VerifyHash_InvalidHashAlgorithm_Throws() { - byte[] hashVal; - using (SHA1 sha1 = SHA1.Create()) - { - hashVal = sha1.ComputeHash(DSATestData.HelloBytes); - } + byte[] hashVal = SHA1.HashData(DSATestData.HelloBytes); using (var dsa = new DSACryptoServiceProvider()) { @@ -264,11 +260,7 @@ public static void VerifyHash_InvalidHashAlgorithm_Throws() [ConditionalFact(nameof(SupportsKeyGeneration))] public static void SignHash_DefaultAlgorithm_Success() { - byte[] hashVal; - using (SHA1 sha1 = SHA1.Create()) - { - hashVal = sha1.ComputeHash(DSATestData.HelloBytes); - } + byte[] hashVal = SHA1.HashData(DSATestData.HelloBytes); using (var dsa = new DSACryptoServiceProvider()) { @@ -280,11 +272,7 @@ public static void SignHash_DefaultAlgorithm_Success() [ConditionalFact(nameof(SupportsKeyGeneration))] public static void SignHash_InvalidHashAlgorithm_Throws() { - byte[] hashVal; - using (SHA256 sha256 = SHA256.Create()) - { - hashVal = sha256.ComputeHash(DSATestData.HelloBytes); - } + byte[] hashVal = SHA256.HashData(DSATestData.HelloBytes); using (var dsa = new DSACryptoServiceProvider()) { @@ -295,11 +283,7 @@ public static void SignHash_InvalidHashAlgorithm_Throws() [ConditionalFact(nameof(SupportsKeyGeneration))] public static void VerifyHash_DefaultAlgorithm_Success() { - byte[] hashVal; - using (SHA1 sha1 = SHA1.Create()) - { - hashVal = sha1.ComputeHash(DSATestData.HelloBytes); - } + byte[] hashVal = SHA1.HashData(DSATestData.HelloBytes); using (var dsa = new DSACryptoServiceProvider()) { @@ -311,11 +295,7 @@ public static void VerifyHash_DefaultAlgorithm_Success() [ConditionalFact(nameof(SupportsKeyGeneration))] public static void VerifyHash_CaseInsensitive_Success() { - byte[] hashVal; - using (SHA1 sha1 = SHA1.Create()) - { - hashVal = sha1.ComputeHash(DSATestData.HelloBytes); - } + byte[] hashVal = SHA1.HashData(DSATestData.HelloBytes); using (var dsa = new DSACryptoServiceProvider()) { diff --git a/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderBackCompat.cs b/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderBackCompat.cs index 229f00bed1914..35741b83f18a1 100644 --- a/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderBackCompat.cs +++ b/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderBackCompat.cs @@ -124,10 +124,7 @@ public static void VerifyLegacySignVerifyHash(bool useLegacySign, bool useLegacy { byte[] dataHash, signature; - using (HashAlgorithm hash = SHA256.Create()) - { - dataHash = hash.ComputeHash(TestData.HelloBytes); - } + dataHash = SHA256.HashData(TestData.HelloBytes); using (var rsa = new RSACryptoServiceProvider()) { diff --git a/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderTests.cs b/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderTests.cs index 3248fa8e759b2..083565c3ca990 100644 --- a/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderTests.cs +++ b/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderTests.cs @@ -308,11 +308,7 @@ public static void ImportParameters_ExponentTooBig_Throws() [Fact] public static void SignHash_DefaultAlgorithm_Success() { - byte[] hashVal; - using (SHA1 sha1 = SHA1.Create()) - { - hashVal = sha1.ComputeHash(TestData.HelloBytes); - } + byte[] hashVal = SHA1.HashData(TestData.HelloBytes); using (var rsa = new RSACryptoServiceProvider()) { @@ -324,11 +320,7 @@ public static void SignHash_DefaultAlgorithm_Success() [Fact] public static void VerifyHash_DefaultAlgorithm_Success() { - byte[] hashVal; - using (SHA1 sha1 = SHA1.Create()) - { - hashVal = sha1.ComputeHash(TestData.HelloBytes); - } + byte[] hashVal = SHA1.HashData(TestData.HelloBytes); using (var rsa = new RSACryptoServiceProvider()) { @@ -386,11 +378,7 @@ public static void SignatureAlgorithm_Success() [Fact] public static void SignData_VerifyHash_CaseInsensitive_Success() { - byte[] hashVal; - using (SHA1 sha1 = SHA1.Create()) - { - hashVal = sha1.ComputeHash(TestData.HelloBytes); - } + byte[] hashVal = SHA1.HashData(TestData.HelloBytes); using (var rsa = new RSACryptoServiceProvider()) { diff --git a/src/libraries/System.Security.Cryptography.Pkcs/tests/Rfc3161/TimestampTokenTests.cs b/src/libraries/System.Security.Cryptography.Pkcs/tests/Rfc3161/TimestampTokenTests.cs index 4de8e40f6ae3c..5532bbaa79e02 100644 --- a/src/libraries/System.Security.Cryptography.Pkcs/tests/Rfc3161/TimestampTokenTests.cs +++ b/src/libraries/System.Security.Cryptography.Pkcs/tests/Rfc3161/TimestampTokenTests.cs @@ -859,17 +859,14 @@ private static byte[] BuildCustomToken( if (validHash) { - using (SHA1 hasher = SHA1.Create()) - { - byte[] hash = hasher.ComputeHash(tsaCert.RawData); - - Buffer.BlockCopy( - hash, - 0, - signingCertificateV1Bytes, - signingCertificateV1Bytes.Length - hash.Length, - hash.Length); - } + byte[] hash = SHA1.HashData(tsaCert.RawData); + + Buffer.BlockCopy( + hash, + 0, + signingCertificateV1Bytes, + signingCertificateV1Bytes.Length - hash.Length, + hash.Length); } if (!skipIssuerSerial) diff --git a/src/libraries/System.Security.Cryptography.Pkcs/tests/SignedCms/SignedCmsWholeDocumentTests.cs b/src/libraries/System.Security.Cryptography.Pkcs/tests/SignedCms/SignedCmsWholeDocumentTests.cs index 8dc711c7df264..d5ec34787c673 100644 --- a/src/libraries/System.Security.Cryptography.Pkcs/tests/SignedCms/SignedCmsWholeDocumentTests.cs +++ b/src/libraries/System.Security.Cryptography.Pkcs/tests/SignedCms/SignedCmsWholeDocumentTests.cs @@ -337,12 +337,9 @@ public static void CheckNoSignatureDocument() Assert.Equal(DateTimeKind.Utc, signingTime.SigningTime.Kind); - using (SHA1 sha1 = SHA1.Create()) - { - Assert.Equal( - sha1.ComputeHash(cms.ContentInfo.Content).ByteArrayToHex(), - messageDigest.MessageDigest.ByteArrayToHex()); - } + Assert.Equal( + SHA1.HashData(cms.ContentInfo.Content).ByteArrayToHex(), + messageDigest.MessageDigest.ByteArrayToHex()); CryptographicAttributeObjectCollection unsignedAttrs = signer.UnsignedAttributes; Assert.Single(unsignedAttrs); @@ -458,12 +455,9 @@ public static void NonEmbeddedCertificate() signer.GetSignature().ByteArrayToHex()); #endif - using (SHA1 sha1 = SHA1.Create()) - { - Assert.Equal( - sha1.ComputeHash(cms.ContentInfo.Content).ByteArrayToHex(), - messageDigest.MessageDigest.ByteArrayToHex()); - } + Assert.Equal( + SHA1.HashData(cms.ContentInfo.Content).ByteArrayToHex(), + messageDigest.MessageDigest.ByteArrayToHex()); // Since it's not NoSignature CheckHash will throw. Assert.Throws(() => signer.CheckHash()); From c3e1af1051a806ebee493109837dc690badccf94 Mon Sep 17 00:00:00 2001 From: Wz Chua <13881045+wzchua@users.noreply.github.com> Date: Sun, 7 Feb 2021 12:27:40 +0800 Subject: [PATCH 2/3] Revert changes for code that target net48 --- src/libraries/Common/tests/System/Net/Http/TestHelper.cs | 5 ++++- .../tests/mono/System.Drawing/BitmapTests.cs | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/libraries/Common/tests/System/Net/Http/TestHelper.cs b/src/libraries/Common/tests/System/Net/Http/TestHelper.cs index bc03dfacc554e..d0f0d368a79f4 100644 --- a/src/libraries/Common/tests/System/Net/Http/TestHelper.cs +++ b/src/libraries/Common/tests/System/Net/Http/TestHelper.cs @@ -78,7 +78,10 @@ public static byte[] ComputeMD5Hash(string data) public static byte[] ComputeMD5Hash(byte[] data) { - return MD5.HashData(data); + using (MD5 md5 = MD5.Create()) + { + return md5.ComputeHash(data); + } } public static Task WhenAllCompletedOrAnyFailed(params Task[] tasks) diff --git a/src/libraries/System.Drawing.Common/tests/mono/System.Drawing/BitmapTests.cs b/src/libraries/System.Drawing.Common/tests/mono/System.Drawing/BitmapTests.cs index 8546f46001279..273425380cb73 100644 --- a/src/libraries/System.Drawing.Common/tests/mono/System.Drawing/BitmapTests.cs +++ b/src/libraries/System.Drawing.Common/tests/mono/System.Drawing/BitmapTests.cs @@ -423,7 +423,7 @@ public string RotateBmp(Bitmap src, RotateFlipType rotate) } } - hash = MD5.HashData(pixels); + hash = MD5.Create().ComputeHash(pixels); return ByteArrayToString(hash); } } @@ -482,7 +482,7 @@ public string RotateIndexedBmp(Bitmap src, RotateFlipType type) if (pixel_data == null) return "--ERROR--"; - byte[] hash = MD5.HashData(pixel_data); + byte[] hash = MD5.Create().ComputeHash(pixel_data); return ByteArrayToString(hash); } } @@ -574,7 +574,7 @@ private byte[] HashPixels(Bitmap bmp) pixels[index++] = clr.B; } } - return MD5.HashData(pixels); + return MD5.Create().ComputeHash(pixels); } private byte[] HashLock(Bitmap bmp, int width, int height, PixelFormat fmt, ImageLockMode mode) @@ -620,7 +620,7 @@ private byte[] HashLock(Bitmap bmp, int width, int height, PixelFormat fmt, Imag { bmp.UnlockBits(bd); } - return MD5.HashData(pixels); + return MD5.Create().ComputeHash(pixels); } // Tests the LockBitmap functions. Makes a hash of the block of pixels that it returns From 04da8904cad82932bd23726f76f31707b0f17644 Mon Sep 17 00:00:00 2001 From: Wz Chua <13881045+wzchua@users.noreply.github.com> Date: Sun, 7 Feb 2021 14:45:55 +0800 Subject: [PATCH 3/3] Revert more changes that are used in net48 --- .../System/Net/Http/HttpClientHandlerTest.cs | 2 +- .../RSA/SignVerify.cs | 42 ++++++++++++++++--- .../tests/RsaCngTests.cs | 2 +- .../SignedCms/SignedCmsWholeDocumentTests.cs | 18 +++++--- 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs index 65006aa10c26e..885a202a229ec 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs @@ -569,7 +569,7 @@ await LoopbackServerFactory.CreateClientAndServerAsync(async uri => request.Headers.Connection.Add("close"); request.Headers.Add("Cookie", "$Version=1; Skin=new"); request.Content.Headers.ContentLength = contentArray.Length; - request.Content.Headers.ContentMD5 = MD5.HashData(contentArray); + request.Content.Headers.ContentMD5 = MD5.Create().ComputeHash(contentArray); request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); request.Headers.Date = DateTimeOffset.Parse("Tue, 15 Nov 1994 08:12:31 GMT"); request.Headers.Expect.Add(new NameValueWithParametersHeaderValue("100-continue")); diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/SignVerify.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/SignVerify.cs index 37bb5bb15fbac..afeddd9be65de 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/SignVerify.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/SignVerify.cs @@ -645,7 +645,12 @@ public void ExpectedHashSignature_SHA1_2048() 0xAE, 0xBD, 0xA5, 0x57, 0x2C, 0xEC, 0x3D, 0x0D, }; - byte[] dataHash = SHA1.HashData(TestData.HelloBytes); + byte[] dataHash; + + using (HashAlgorithm hash = SHA1.Create()) + { + dataHash = hash.ComputeHash(TestData.HelloBytes); + } ExpectHashSignature(expectedHashSignature, dataHash, "SHA1", TestData.RSA2048Params); } @@ -673,7 +678,12 @@ public void ExpectedHashSignature_SHA256_1024() 0x70, 0x95, 0x25, 0x49, 0x8A, 0x1A, 0xD7, 0xFC, }; - byte[] dataHash = SHA256.HashData(TestData.HelloBytes); + byte[] dataHash; + + using (HashAlgorithm hash = SHA256.Create()) + { + dataHash = hash.ComputeHash(TestData.HelloBytes); + } ExpectHashSignature(expectedHashSignature, dataHash, "SHA256", TestData.RSA1024Params); } @@ -717,7 +727,12 @@ public void ExpectedHashSignature_SHA256_2048() 0x6B, 0x9B, 0x97, 0x2D, 0x8C, 0xB2, 0x1C, 0x16, }; - byte[] dataHash = SHA256.HashData(TestData.HelloBytes); + byte[] dataHash; + + using (HashAlgorithm hash = SHA256.Create()) + { + dataHash = hash.ComputeHash(TestData.HelloBytes); + } ExpectHashSignature(expectedHashSignature, dataHash, "SHA256", TestData.RSA2048Params); } @@ -761,7 +776,12 @@ public void VerifyHashSignature_SHA1_2048() 0xAE, 0xBD, 0xA5, 0x57, 0x2C, 0xEC, 0x3D, 0x0D, }; - byte[] dataHash = SHA1.HashData(TestData.HelloBytes); + byte[] dataHash; + + using (HashAlgorithm hash = SHA1.Create()) + { + dataHash = hash.ComputeHash(TestData.HelloBytes); + } VerifyHashSignature(hashSignature, dataHash, "SHA1", TestData.RSA2048Params); } @@ -789,7 +809,12 @@ public void VerifyHashSignature_SHA256_1024() 0x70, 0x95, 0x25, 0x49, 0x8A, 0x1A, 0xD7, 0xFC, }; - byte[] dataHash = SHA256.HashData(TestData.HelloBytes); + byte[] dataHash; + + using (HashAlgorithm hash = SHA256.Create()) + { + dataHash = hash.ComputeHash(TestData.HelloBytes); + } VerifyHashSignature(hashSignature, dataHash, "SHA256", TestData.RSA1024Params); } @@ -833,7 +858,12 @@ public void VerifyHashSignature_SHA256_2048() 0x6B, 0x9B, 0x97, 0x2D, 0x8C, 0xB2, 0x1C, 0x16, }; - byte[] dataHash = SHA256.HashData(TestData.HelloBytes); + byte[] dataHash; + + using (HashAlgorithm hash = SHA256.Create()) + { + dataHash = hash.ComputeHash(TestData.HelloBytes); + } VerifyHashSignature(hashSignature, dataHash, "SHA256", TestData.RSA2048Params); } diff --git a/src/libraries/System.Security.Cryptography.Cng/tests/RsaCngTests.cs b/src/libraries/System.Security.Cryptography.Cng/tests/RsaCngTests.cs index 1a4439232849e..ea99779be3efe 100644 --- a/src/libraries/System.Security.Cryptography.Cng/tests/RsaCngTests.cs +++ b/src/libraries/System.Security.Cryptography.Cng/tests/RsaCngTests.cs @@ -19,7 +19,7 @@ public static class RsaCngTests public static void SignVerifyHashRoundTrip() { byte[] message = "781021abcd982139a8bc91387870ac01".HexToByteArray(); - byte[] hash = SHA1.HashData(message); + byte[] hash = SHA1.Create().ComputeHash(message); TestSignVerifyHashRoundTrip(hash, HashAlgorithmName.SHA1, RSASignaturePadding.Pss, 0x100); } diff --git a/src/libraries/System.Security.Cryptography.Pkcs/tests/SignedCms/SignedCmsWholeDocumentTests.cs b/src/libraries/System.Security.Cryptography.Pkcs/tests/SignedCms/SignedCmsWholeDocumentTests.cs index d5ec34787c673..8dc711c7df264 100644 --- a/src/libraries/System.Security.Cryptography.Pkcs/tests/SignedCms/SignedCmsWholeDocumentTests.cs +++ b/src/libraries/System.Security.Cryptography.Pkcs/tests/SignedCms/SignedCmsWholeDocumentTests.cs @@ -337,9 +337,12 @@ public static void CheckNoSignatureDocument() Assert.Equal(DateTimeKind.Utc, signingTime.SigningTime.Kind); - Assert.Equal( - SHA1.HashData(cms.ContentInfo.Content).ByteArrayToHex(), - messageDigest.MessageDigest.ByteArrayToHex()); + using (SHA1 sha1 = SHA1.Create()) + { + Assert.Equal( + sha1.ComputeHash(cms.ContentInfo.Content).ByteArrayToHex(), + messageDigest.MessageDigest.ByteArrayToHex()); + } CryptographicAttributeObjectCollection unsignedAttrs = signer.UnsignedAttributes; Assert.Single(unsignedAttrs); @@ -455,9 +458,12 @@ public static void NonEmbeddedCertificate() signer.GetSignature().ByteArrayToHex()); #endif - Assert.Equal( - SHA1.HashData(cms.ContentInfo.Content).ByteArrayToHex(), - messageDigest.MessageDigest.ByteArrayToHex()); + using (SHA1 sha1 = SHA1.Create()) + { + Assert.Equal( + sha1.ComputeHash(cms.ContentInfo.Content).ByteArrayToHex(), + messageDigest.MessageDigest.ByteArrayToHex()); + } // Since it's not NoSignature CheckHash will throw. Assert.Throws(() => signer.CheckHash());