Link to home
Start Free TrialLog in
Avatar of McLyd
McLydFlag for Sweden

asked on

Convert PHP function into C#

I need help converting a, for me, complex php function into C# code.

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;
}

Open in new window


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;
        }

Open in new window

Avatar of BuggyCoder
BuggyCoder
Flag of India image

i am sure there is no tool to convert php to c#, however i have modified the code in c# as:-

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 = Encoding.ASCII.GetBytes(salt);
            hashdata.AddRange(toEncodeAsBytes);

            string hash = System.Convert.ToBase64String(hashdata.ToArray());

            //$hash = str_replace('+', '@', $hash);
            //$hash = str_replace('/', '.', $hash);
            hash.Replace("+", "@").Replace("/", ".");

            return hash;
        }

Open in new window


see if it works....
Avatar of McLyd

ASKER

Sorry, no go on that one. But probably a good modification though.

Any other ideas?
any reasons for not using the code...
what challenge you see in it...
Avatar of McLyd

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.
ASKER CERTIFIED SOLUTION
Avatar of McLyd
McLyd
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of McLyd

ASKER

It's the only solution that works.