开发者

how to convert c# function to php

开发者 https://www.devze.com 2023-02-10 11:27 出处:网络
How do I convert a C# function to a PHP function?The C# code is below: internal string EncodePassword(string password, string salt)

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


You can use the PHP SHA1 function to do the encryption.

0

精彩评论

暂无评论...
验证码 换一张
取 消