Skip to content

Commit

Permalink
Convert calls of instance HashAlgorithm.ComputeHash to static HashData
Browse files Browse the repository at this point in the history
This only affects target sites which are not cross-compiled for a TFM where the static HashData methods don't exist.
  • Loading branch information
wzchua authored Feb 8, 2021
1 parent 9450aa1 commit d1d6ab1
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,10 +642,7 @@ private CertStatus CheckRevocation(ReadOnlyMemory<byte> 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<byte> reqDn))
Expand Down
11 changes: 4 additions & 7 deletions src/libraries/System.Net.WebClient/tests/WebClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,9 @@ private static void AddWebSocketHeaders(HttpRequestMessage request, string secKe
private static KeyValuePair<string, string> CreateSecKeyAndSecWebSocketAccept()
{
string secKey = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
using (SHA1 sha = SHA1.Create())
{
return new KeyValuePair<string, string>(
secKey,
Convert.ToBase64String(sha.ComputeHash(Encoding.ASCII.GetBytes(secKey + WSServerGuid))));
}
return new KeyValuePair<string, string>(
secKey,
Convert.ToBase64String(SHA1.HashData(Encoding.ASCII.GetBytes(secKey + WSServerGuid))));
}

private static void ValidateHeader(HttpHeaders headers, string name, string expectedValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand All @@ -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())
{
Expand All @@ -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())
{
Expand All @@ -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())
{
Expand All @@ -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())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand All @@ -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())
{
Expand Down Expand Up @@ -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())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d1d6ab1

Please sign in to comment.