Skip to content

Commit fed6ba9

Browse files
committed
Coding style
use "using" and raw string when there is a lot of "
1 parent 41fbf13 commit fed6ba9

2 files changed

Lines changed: 29 additions & 21 deletions

File tree

RTSP.Tests/Authentication/AuthenticationDigestTests.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,22 @@ namespace RTSP.Tests.Authentication
99
public class AuthenticationDigestTests
1010
{
1111
// MD5, no Algorithm specified
12-
string authStringPLAY_MD5_noalg = "Digest username=\"user\", realm=\"SharpRTSPServer\", nonce=\"556284985\", uri=\"rtsp://192.168.26.76:8554/\", response=\"f3ce799d94cb45e14bf59e57cd41c749\"";
13-
string authStringPLAY_MD5_with_alg = "Digest username=\"user\", realm=\"SharpRTSPServer\", nonce=\"556284985\", uri=\"rtsp://192.168.26.76:8554/\", response=\"f3ce799d94cb45e14bf59e57cd41c749\", algorithm=\"MD5\"";
14-
string authStringPLAY_SHA256 = "Digest username=\"user\", realm=\"SharpRTSPServer\", nonce=\"729461183\", uri=\"rtsp://192.168.26.76:8554/\", response=\"9f6d99e827799b23b273aaf62f7eea84a720e25a205284a921f0d9aa14621dcc\", algorithm=\"SHA-256\"";
15-
string authStringPLAY_BadAlg = "Digest username=\"user\", realm=\"SharpRTSPServer\", nonce=\"729461183\", uri=\"rtsp://192.168.26.76:8554/\", response=\"9f6d99e827799b23b273aaf62f7eea84a720e25a205284a921f0d9aa14621dcc\", algorithm=\"BADVALUE\"";
16-
string realm = "SharpRTSPServer";
17-
string qop = "";
12+
private const string authStringPLAY_MD5_noalg = """
13+
Digest username="user", realm="SharpRTSPServer", nonce="556284985", uri="rtsp://192.168.26.76:8554/", response="f3ce799d94cb45e14bf59e57cd41c749"
14+
""";
15+
private const string authStringPLAY_MD5_with_alg = """
16+
Digest username="user", realm="SharpRTSPServer", nonce="556284985", uri="rtsp://192.168.26.76:8554/", response="f3ce799d94cb45e14bf59e57cd41c749", algorithm="MD5"
17+
""";
18+
private const string authStringPLAY_SHA256 = """
19+
Digest username="user", realm="SharpRTSPServer", nonce="729461183", uri="rtsp://192.168.26.76:8554/", response="9f6d99e827799b23b273aaf62f7eea84a720e25a205284a921f0d9aa14621dcc", algorithm="SHA-256"
20+
""";
21+
22+
private const string authStringPLAY_BadAlg = """
23+
Digest username="user", realm="SharpRTSPServer", nonce="729461183", uri="rtsp://192.168.26.76:8554/", response="9f6d99e827799b23b273aaf62f7eea84a720e25a205284a921f0d9aa14621dcc", algorithm="BADVALUE"
24+
""";
25+
26+
private const string realm = "SharpRTSPServer";
27+
private const string qop = "";
1828

1929
[Test]
2030
public void IsValid_MD5_No_Alg_Test1()
@@ -78,7 +88,7 @@ public void GetWWWAuthenticate_MD5()
7888
var testObject = new AuthenticationDigest(new NetworkCredential("user", "password", realm), realm, nonce, qop, AuthenticationDigest.HashAlgorithm.MD5);
7989
var result = testObject.GetServerResponse();
8090

81-
Assert.That(result.Contains("algorithm"), Is.False); // We don't add 'algorithm=MD5' as it is not required
91+
Assert.That(result, Does.Not.Contain("algorithm")); // We don't add 'algorithm=MD5' as it is not required
8292
}
8393

8494
[Test]
@@ -91,7 +101,8 @@ public void GetWWWAuthenticate_SHA256()
91101
var testObject = new AuthenticationDigest(new NetworkCredential("user", "password", realm), realm, nonce, qop, AuthenticationDigest.HashAlgorithm.SHA256);
92102
var result = testObject.GetServerResponse();
93103

94-
Assert.That(result.Contains("algorithm") && result.Contains("SHA-256"), Is.True);
104+
Assert.That(result, Does.Contain("algorithm"));
105+
Assert.That(result, Does.Contain("SHA-256"));
95106
}
96107

97108
}

RTSP/AuthenticationDigest.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ public override string GetServerResponse()
5151
public override string GetResponse(uint nonceCounter, string uri, string method,
5252
byte[] entityBodyBytes)
5353
{
54-
System.Security.Cryptography.HashAlgorithm hashAlgorithm;
55-
if (_algorithm == HashAlgorithm.SHA256)
56-
hashAlgorithm = SHA256.Create();
57-
else /* default is MD5 */
58-
hashAlgorithm = MD5.Create();
54+
using var hashAlgorithm = CreateHashAlgorithm(_algorithm);
5955

6056
string ha1 = CalculateHash(hashAlgorithm, $"{Credentials.UserName}:{_realm}:{Credentials.Password}");
6157
string ha2Argument = $"{method}:{uri}";
@@ -80,8 +76,6 @@ public override string GetResponse(uint nonceCounter, string uri, string method,
8076
sb.AppendFormat(CultureInfo.InvariantCulture, ", response=\"{0}\", cnonce=\"{1}\", nc=\"{2:X8}\", qop=\"{3}\"", response, _cnonce, nonceCounter, _qop);
8177
}
8278

83-
hashAlgorithm.Dispose();
84-
8579
return sb.ToString();
8680
}
8781
public override bool IsValid(RtspRequest receivedMessage)
@@ -137,16 +131,11 @@ public override bool IsValid(RtspRequest receivedMessage)
137131

138132
// Create the MD5 Hash using all parameters passed in the Auth Header with the
139133
// addition of the 'Password'
140-
System.Security.Cryptography.HashAlgorithm hashAlgorithm;
141-
if (algorithm == HashAlgorithm.SHA256)
142-
hashAlgorithm = SHA256.Create();
143-
else /* Default to MD5 */
144-
hashAlgorithm = MD5.Create();
134+
using var hashAlgorithm = CreateHashAlgorithm(algorithm);
145135

146136
string hashA1 = CalculateHash(hashAlgorithm, username + ":" + realm + ":" + Credentials.Password);
147137
string hashA2 = CalculateHash(hashAlgorithm, receivedMessage.RequestTyped + ":" + uri);
148138
string expectedResponse = CalculateHash(hashAlgorithm, hashA1 + ":" + nonce + ":" + hashA2);
149-
hashAlgorithm.Dispose();
150139

151140
// Check if everything matches
152141
// ToDo - extract paths from the URIs (ignoring SETUP's trackID)
@@ -158,6 +147,14 @@ public override bool IsValid(RtspRequest receivedMessage)
158147
return false;
159148
}
160149

150+
private static System.Security.Cryptography.HashAlgorithm CreateHashAlgorithm(HashAlgorithm algorithm) =>
151+
algorithm switch
152+
{
153+
HashAlgorithm.SHA256 => SHA256.Create(),
154+
/* default is MD5 */
155+
_ => MD5.Create(),
156+
};
157+
161158
private static string CalculateHash(System.Security.Cryptography.HashAlgorithm hashAlgorithm, string input)
162159
{
163160
byte[] inputBytes = Encoding.UTF8.GetBytes(input);

0 commit comments

Comments
 (0)