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 867e87c34dfa6f..22c3a02648a576 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/X509Certificates/CertificateAuthority.cs b/src/libraries/Common/tests/System/Security/Cryptography/X509Certificates/CertificateAuthority.cs index a2227ddd1a4488..1257b8a0db8d27 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.Net.WebClient/tests/WebClientTest.cs b/src/libraries/System.Net.WebClient/tests/WebClientTest.cs index b727562f22a149..f168d4a9eb0710 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 92732ab643aee0..a378566af65ca3 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 82ae0bf901be01..5726326c6ab8fa 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.Csp/tests/DSACryptoServiceProviderTests.cs b/src/libraries/System.Security.Cryptography.Csp/tests/DSACryptoServiceProviderTests.cs index 5ac96578f6a3d0..d2a64c4a96fd89 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 229f00bed19141..35741b83f18a1f 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 3248fa8e759b2b..083565c3ca9902 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 4de8e40f6ae3c0..5532bbaa79e026 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)