简体   繁体   中英

how to convert c# function to php

How do I convert a C# function to a PHP function? The C# code is below:

internal string EncodePassword(string password, string salt)
{
       // Get the Unicode bytes of the plain text password.
       byte[] bytes = System.Text.Encoding.Unicode.GetBytes(password);

       // The salt is a Base64 encoded string, convert back to a byte array.
       byte[] src = Convert.FromBase64String(salt);

       // Concat both byte buffers.
       byte[] dst = new byte[src.Length + bytes.Length];
       byte[] inArray = null;
       System.Buffer.BlockCopy(src, 0, dst, 0, src.Length);
       System.Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);

       // Compute the SHA1-hash from the concatenated buffer.
       System.Security.Cryptography.SHA1 algorithm = System.Security.Cryptography.SHA1Managed.Create();
       inArray = algorithm.ComputeHash(dst);

       // Return the result as a Base64-string.
       return Convert.ToBase64String(inArray);
}

I know its too late to answer this question. But i think it will be helpful for someone who is trying to convert C# code to PHP. I had a similar kind of requirement recently

<?php

$password ='test123';
$salt = '5Br2nXb56EpliGXmfHdWWw==';

$password = mb_convert_encoding($password,'UTF-16LE');
echo base64_encode(sha1(base64_decode($salt).$password,true));

I'd say it's something like this:

function encodePassword($password, $salt){
  return base64_encode(sha1($password . base64_decode($salt), true));
}

您可以使用PHP SHA1函数进行加密。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM