试图端口C#函数PHP5 [英] Trying to port C# function to PHP5

查看:158
本文介绍了试图端口C#函数PHP5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这个功能到PHP转换,但不知它不会给相同的结果。

 公共静态字符串EncodePassword (字符串传递,字符串盐){
字节[]字节= Encoding.Unicode.GetBytes(PASS);
的byte [] SRC = Convert.FromBase64String(盐);
字节[] = DST新的字节[src.Length + bytes.Length];
的byte [] inArray = NULL;
Buffer.BlockCopy(源,0,dst的,0,src.Length);
Buffer.BlockCopy(字节,0,DST,src.Length,bytes.Length);
算法的HashAlgorithm = HashAlgorithm.Create(SHA1);
inArray = algorithm.ComputeHash(DST);
返回Convert.ToBase64String(inArray);
}

这是我对在PHP

 函数CreatePasswordHash($的密码,$盐){
$ salted_pa​​ssword = BASE64_DECODE($盐)$的密码;
$结果=散列('SHA1',$ salted_pa​​ssword,真正的);
返回BASE64_ENCODE($结果);
}



当然,这是行不通的。 ?所以我在做什么错在这里。



这些都是值来进行测试:

  $盐='Xh2pHwDv3VEUQCvz5qOm7w =='; 
$ hashed_value ='0U / kYMz3yCXLsw / r9kocT5zf0cc =';
$密码='Welcome1!;

如果($ hashed_value === CreatePasswordHash($的密码,$盐)){
回声干得好!
}



编辑:基于来自Martyx和休闲裤的建议工作液

 函数CreatePasswordHash($的密码,$盐){
$ upass = mb_convert_encoding($的密码,UCS-2LE','汽车' );
$ salted_pa​​ssword = BASE64_DECODE($盐)$ upass。
$结果=散列('SHA1',$ salted_pa​​ssword,真正的);
返回BASE64_ENCODE($结果);
}


解决方案

在我使用SHA1在PHP和C#和C#输出字母均大写字母,PHP是小写。



我会建议使用C#和PHP中相同的编码(UTF8和UTF16都是不错的选择)



在C#中的选择:




  • Encoding.ASCII。 GetBytes会

  • Encoding.UTF8.GetBytes

  • ...



编码PHP:




I'm trying to convert this function into PHP but somehow it does not give same results.

public static string EncodePassword(string pass, string salt) {
    byte[] bytes = Encoding.Unicode.GetBytes(pass);
    byte[] src = Convert.FromBase64String(salt);
    byte[] dst = new byte[src.Length + bytes.Length];
    byte[] inArray = null;
    Buffer.BlockCopy(src, 0, dst, 0, src.Length);
    Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
    HashAlgorithm algorithm = HashAlgorithm.Create("SHA1");
    inArray = algorithm.ComputeHash(dst);
    return Convert.ToBase64String(inArray);
}

This is my take on that in PHP

function CreatePasswordHash($password, $salt) {
    $salted_password = base64_decode($salt).$password;
    $result = hash('SHA1',$salted_password,true);
    return base64_encode($result);
}

And of course it does not work. So what am I doing wrong here?

These are the values for testing:

$salt         = 'Xh2pHwDv3VEUQCvz5qOm7w==';
$hashed_value = '0U/kYMz3yCXLsw/r9kocT5zf0cc=';
$password     = 'Welcome1!';

if ($hashed_value === CreatePasswordHash($password,$salt)) {
  echo "Good job!";
}

Edit: Working solution based on suggestions from Martyx and Slacks

function CreatePasswordHash($password, $salt) {
    $upass = mb_convert_encoding($password,'UCS-2LE','auto');
    $salted_password = base64_decode($salt).$upass;
    $result = hash('SHA1',$salted_password,true);
    return base64_encode($result);
}

解决方案

Once I used SHA1 in PHP and C# and in C# output letters were in uppercase and in PHP it was in lowercase.

I would recommend to use the same encoding in C# and in PHP (UTF8 and UTF16 are good choices).

Choices in C#:

  • Encoding.ASCII.GetBytes
  • Encoding.UTF8.GetBytes
  • ...

encoding PHP:

这篇关于试图端口C#函数PHP5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆