Files
2026-06-04 11:42:34 +02:00

236 lines
6.0 KiB
C#

using System;
using System.Security.Cryptography;
using System.Text;
namespace System.Net
{
// Token: 0x02000210 RID: 528
internal class DigestSession
{
// Token: 0x060011F2 RID: 4594 RVA: 0x000357A0 File Offset: 0x000339A0
public DigestSession()
{
this._nc = 1;
this.lastUse = DateTime.Now;
}
// Token: 0x170005E1 RID: 1505
// (get) Token: 0x060011F4 RID: 4596 RVA: 0x000357C8 File Offset: 0x000339C8
public string Algorithm
{
get
{
return this.parser.Algorithm;
}
}
// Token: 0x170005E2 RID: 1506
// (get) Token: 0x060011F5 RID: 4597 RVA: 0x000357D8 File Offset: 0x000339D8
public string Realm
{
get
{
return this.parser.Realm;
}
}
// Token: 0x170005E3 RID: 1507
// (get) Token: 0x060011F6 RID: 4598 RVA: 0x000357E8 File Offset: 0x000339E8
public string Nonce
{
get
{
return this.parser.Nonce;
}
}
// Token: 0x170005E4 RID: 1508
// (get) Token: 0x060011F7 RID: 4599 RVA: 0x000357F8 File Offset: 0x000339F8
public string Opaque
{
get
{
return this.parser.Opaque;
}
}
// Token: 0x170005E5 RID: 1509
// (get) Token: 0x060011F8 RID: 4600 RVA: 0x00035808 File Offset: 0x00033A08
public string QOP
{
get
{
return this.parser.QOP;
}
}
// Token: 0x170005E6 RID: 1510
// (get) Token: 0x060011F9 RID: 4601 RVA: 0x00035818 File Offset: 0x00033A18
public string CNonce
{
get
{
if (this._cnonce == null)
{
byte[] array = new byte[15];
DigestSession.rng.GetBytes(array);
this._cnonce = Convert.ToBase64String(array);
Array.Clear(array, 0, array.Length);
}
return this._cnonce;
}
}
// Token: 0x060011FA RID: 4602 RVA: 0x00035860 File Offset: 0x00033A60
public bool Parse(string challenge)
{
this.parser = new DigestHeaderParser(challenge);
if (!this.parser.Parse())
{
return false;
}
if (this.parser.Algorithm == null || this.parser.Algorithm.ToUpper().StartsWith("MD5"))
{
this.hash = HashAlgorithm.Create("MD5");
}
return true;
}
// Token: 0x060011FB RID: 4603 RVA: 0x000358CC File Offset: 0x00033ACC
private string HashToHexString(string toBeHashed)
{
if (this.hash == null)
{
return null;
}
this.hash.Initialize();
byte[] array = this.hash.ComputeHash(Encoding.ASCII.GetBytes(toBeHashed));
StringBuilder stringBuilder = new StringBuilder();
foreach (byte b in array)
{
stringBuilder.Append(b.ToString("x2"));
}
return stringBuilder.ToString();
}
// Token: 0x060011FC RID: 4604 RVA: 0x00035948 File Offset: 0x00033B48
private string HA1(string username, string password)
{
string text = string.Format("{0}:{1}:{2}", username, this.Realm, password);
if (this.Algorithm != null && this.Algorithm.ToLower() == "md5-sess")
{
text = string.Format("{0}:{1}:{2}", this.HashToHexString(text), this.Nonce, this.CNonce);
}
return this.HashToHexString(text);
}
// Token: 0x060011FD RID: 4605 RVA: 0x000359B4 File Offset: 0x00033BB4
private string HA2(HttpWebRequest webRequest)
{
string text = string.Format("{0}:{1}", webRequest.Method, webRequest.RequestUri.PathAndQuery);
if (this.QOP == "auth-int")
{
}
return this.HashToHexString(text);
}
// Token: 0x060011FE RID: 4606 RVA: 0x000359FC File Offset: 0x00033BFC
private string Response(string username, string password, HttpWebRequest webRequest)
{
string text = string.Format("{0}:{1}:", this.HA1(username, password), this.Nonce);
if (this.QOP != null)
{
text += string.Format("{0}:{1}:{2}:", this._nc.ToString("X8"), this.CNonce, this.QOP);
}
text += this.HA2(webRequest);
return this.HashToHexString(text);
}
// Token: 0x060011FF RID: 4607 RVA: 0x00035A70 File Offset: 0x00033C70
public Authorization Authenticate(WebRequest webRequest, ICredentials credentials)
{
if (this.parser == null)
{
throw new InvalidOperationException();
}
HttpWebRequest httpWebRequest = webRequest as HttpWebRequest;
if (httpWebRequest == null)
{
return null;
}
this.lastUse = DateTime.Now;
NetworkCredential credential = credentials.GetCredential(httpWebRequest.RequestUri, "digest");
if (credential == null)
{
return null;
}
string userName = credential.UserName;
if (userName == null || userName == string.Empty)
{
return null;
}
string password = credential.Password;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendFormat("Digest username=\"{0}\", ", userName);
stringBuilder.AppendFormat("realm=\"{0}\", ", this.Realm);
stringBuilder.AppendFormat("nonce=\"{0}\", ", this.Nonce);
stringBuilder.AppendFormat("uri=\"{0}\", ", httpWebRequest.Address.PathAndQuery);
if (this.Algorithm != null)
{
stringBuilder.AppendFormat("algorithm=\"{0}\", ", this.Algorithm);
}
stringBuilder.AppendFormat("response=\"{0}\", ", this.Response(userName, password, httpWebRequest));
if (this.QOP != null)
{
stringBuilder.AppendFormat("qop=\"{0}\", ", this.QOP);
}
lock (this)
{
if (this.QOP != null)
{
stringBuilder.AppendFormat("nc={0:X8}, ", this._nc);
this._nc++;
}
}
if (this.CNonce != null)
{
stringBuilder.AppendFormat("cnonce=\"{0}\", ", this.CNonce);
}
if (this.Opaque != null)
{
stringBuilder.AppendFormat("opaque=\"{0}\", ", this.Opaque);
}
stringBuilder.Length -= 2;
return new Authorization(stringBuilder.ToString());
}
// Token: 0x170005E7 RID: 1511
// (get) Token: 0x06001200 RID: 4608 RVA: 0x00035C48 File Offset: 0x00033E48
public DateTime LastUse
{
get
{
return this.lastUse;
}
}
// Token: 0x04000FA9 RID: 4009
private static RandomNumberGenerator rng = RandomNumberGenerator.Create();
// Token: 0x04000FAA RID: 4010
private DateTime lastUse;
// Token: 0x04000FAB RID: 4011
private int _nc;
// Token: 0x04000FAC RID: 4012
private HashAlgorithm hash;
// Token: 0x04000FAD RID: 4013
private DigestHeaderParser parser;
// Token: 0x04000FAE RID: 4014
private string _cnonce;
}
}