Помогите переписать код с PHP на C#
Формулировка задачи:
Народ помогите пожалуйста переделать с PHP на C#
на $_GET внимание можно не обращать.
$hexchal = pack ("H32", $_GET['chal']);
$newchal = $uamsecret ? pack("H*", md5($hexchal . $uamsecret)) : $hexchal;
$response = md5("\0" . $_GET['password'] . $newchal);
$newpwd = pack("a32", $_GET['password']);
$pappassword = implode ('', unpack("H32", ($newpwd ^ $newchal)));Решение задачи: «Помогите переписать код с PHP на C#»
textual
Листинг программы
string chal = "2121212121212122"; // 16 символов
string uamsecret = null;
string password = "!Пар0л-ь";
string hexchal = pack("H32", chal);
string newchal = uamsecret != null ? pack("H*", md5(hexchal + uamsecret)) : hexchal;
byte[] response = md5("\0" + password + newchal);
byte[] newpwd = Encoding.GetEncoding("windows-1251").GetBytes(password).Concat(Enumerable.Repeat((byte)0, 32)).Take(32).ToArray();
byte[] newchal_buf = Encoding.GetEncoding("windows-1251").GetBytes(newchal).SelectMany(b => new byte[] { (byte)((b & 0xF0)>>4), (byte)(b & 0xF)}).ToArray();
StringBuilder sbPappassword = new StringBuilder(32);
for (int i=0; i<32; i++)
{
sbPappassword.AppendFormat("X2", newpwd[i] ^ newchal_buf[i]);
}
string pappassword = sbPappassword.ToString();
//...
static byte[] md5(string text)
{
using (var md5prov = new System.Security.Cryptography.MD5CryptoServiceProvider())
{
return md5prov.ComputeHash(Encoding.GetEncoding("windows-1251").GetBytes(text));
}
}
static string pack(string format, string value)
{
switch (format[0])
{
case 'H':
int maxLen = format[1] == '*' ? value.Length : int.Parse(format.Substring(1));
byte[] buf = new byte[maxLen/2];
for (int i=0; i<maxLen/2; i+=2)
{
buf[i/2] = Convert.ToByte(value.Substring(i,2), 16);
}
return Encoding.GetEncoding("windows-1251").GetString(buf);
case 'a':
int padWidth = int.Parse(format.Substring(1));
return value.PadLeft(padWidth, '\0');
}
return null;
}
static string pack(string format, byte[] value)
{
switch (format[0])
{
case 'H':
int maxLen = format[1] == '*' ? value.Length : int.Parse(format.Substring(1));
return Encoding.GetEncoding("windows-1251").GetString(value, 0, maxLen);
}
return null;
}