2009-03-03 20 views
0

我想了解这一点,所以我可以做类似的事情。我知道:在我的C#到PHP代码转换中没有得到sha1匹配,我错过了什么?

BUF包含附加到它(最后20个字节)的哈希认证密钥 那就是BEING在MachineKeySection抬头的HashData是SHA1

length -= 20; 
byte[] buffer2 = MachineKeySection.HashData(buf, null, 0, length); 

for (int i = 0; i < 20; i++) 
{ 
    if (buffer2[i] != buf[length + i]) 
    { 
     return null; 
    } 
} 

这就是我认为正在发生的: 我们对buf​​的最后20个字节进行散列。 然后,我们每次处理1个字节,将我们刚刚创建的散列与附加在散列最后20个字节上的散列进行比较。

所以在PHP我想这一点:

//get the length of the ticket -20 bytes 
$ticketLn = strlen($buf)-40; 
//grab all but the last 20 bytes 
$ticket = substr($decrypthex, 0, $ticketLn); 
//create a hash of the ticket 
$hash = substr($decrypthex, $ticketLn); 

,下一步就是进行比较。但是当我回应$ hash和sha1($ ticket)的输出时,它们不匹配,所以我甚至没有在代码中比较它们。

回答

2

默认情况下,php的sha1() function返回40个字符的十六进制数字。你必须明确地请求20字节的二进制格式,如果这就是你想要的

$hash = sha1($ticket, true); 
1
$ticket = substr($decrypthex, 0, -20); 
$hash = substr($decrypthex, -20); 
+0

感谢更好的语法,但仍然不是我所需要的。 – lynn 2009-03-03 16:24:53

相关问题