McLyd
asked on
Convert PHP function into C#
I need help converting a, for me, complex php function into C# code.
PHP Function:
My own C# code (that doesn't work):
PHP Function:
function ssha($password){
mt_srand((double)microtime()*1000000);
$salt = pack("CCCC", mt_rand(), mt_rand(), mt_rand(), mt_rand());
$hash = base64_encode(pack("H*", sha1($password . $salt)) . $salt);
$hash = str_replace('+', '@', $hash);
$hash = str_replace('/', '.', $hash);
return $hash;
}
My own C# code (that doesn't work):
public static string GetLegatoHash(string Password)
{
// mt_srand((double)microtime()*1000000);
int seconds = DateTime.Now.Second;
Random rnd = new Random(seconds);
// $salt = pack("CCCC", mt_rand(), mt_rand(), mt_rand(), mt_rand());
byte[] data = new byte[] { (byte)rnd.Next(), (byte)rnd.Next(), (byte)rnd.Next(), (byte)rnd.Next() };
string salt = data[0].ToString() + data[1].ToString() + data[2].ToString() + data[3].ToString();
// sha1($password . $salt)
ASCIIEncoding UE = new ASCIIEncoding();
byte[] HashValue, MessageBytes = UE.GetBytes((Password + salt));
HashAlgorithm SHhash = new SHA1Managed();
HashValue = SHhash.ComputeHash(data);
// pack("H*", sha1($password . $salt))
List<byte> hashdata = new List<byte>();
foreach (byte b in HashValue)
{
byte nibbleLow = (byte)(b & 0x0F);
byte nibbleHigh = (byte)((b & 0xF0) >> 4);
hashdata.Add(nibbleHigh);
hashdata.Add(nibbleLow);
}
foreach (byte b in data)
{
byte nibbleLow = (byte)(b & 0x0F);
byte nibbleHigh = (byte)((b & 0xF0) >> 4);
hashdata.Add(nibbleHigh);
hashdata.Add(nibbleLow);
}
// base64_encode(pack("H*", sha1($password . $salt)) . $salt);
byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(string.Join("", hashdata.ToArray()) + salt);
string hash = System.Convert.ToBase64String(toEncodeAsBytes);
//$hash = str_replace('+', '@', $hash);
//$hash = str_replace('/', '.', $hash);
hash.Replace("+", "@").Replace("/", ".");
return hash;
}
ASKER
Sorry, no go on that one. But probably a good modification though.
Any other ideas?
Any other ideas?
any reasons for not using the code...
what challenge you see in it...
what challenge you see in it...
ASKER
Well, the problem is that it still isn't working. The C# code doesn't produce the same output as the php code does.
The challenge.... is that I don't know enough php to make the conversion on my own.
The challenge.... is that I don't know enough php to make the conversion on my own.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It's the only solution that works.
Open in new window
see if it works....