Not to say that it doesn't work, it does, but it doesn't give the desired return.
VB6: BNLSChecksum("test", 1) = 520521760
PHP: BNLSChecksum("test", 1) = -2082672713
PHP: BNLSChecksum2("test", 1) = -186917087
[update] PHP: BNLSChecksum2("Sample", 0x123ABCD) == 212270949(!=318631304)
[update] PHP: BNLSChecksum2("Sample", 0x123ABCD) == -810156055 (!=318631304)
<?php function BNLSChecksum($password, $servercode) { $val = "0000000" + $servercode; return CRC32($password + substr($val, strlen($val) - 8)); } function BNLSChecksum2($password, $servercode) { $val = "0000000" & $servercode; return CRC32($password & substr($val, strlen($val) - 8)); }?>
Any ideas?
Explain the code?
Why add 00000000 and what doe CRC32 do?
Quote from: raylu on April 29, 2005, 09:06 PM
Explain the code?
Why add 00000000 and what doe CRC32 do?
This is the code for the BNLS_AUTHORIZE_PROOF message (documented http://www.valhallalegends.com/yoni/bnlsprotocolspec.txt). CRC32 is a standard algorithm -- the 32-bit cyclic redundancy check.
rabbit: I think you're padding 8 '0' characters, whereas you're only supposed to pad *up to* 8 total characters. So if your hex value is 6 characters, you add two 0s to it.
Take another look at his code. It is equivalent to:
Function BNLSChecksum(Password As String, ServerCode As Integer) As Integer
Dim Var As String
Var = "0000000" & Hex(ServerCode)
Return CRC32(Password & Mid(Var, Len(Var)-8))
End Sub
I don't see a difference between BNLSChecksum and BNLSChecksum2 in that PHP script, though.
Remember that BNLS expects the hexadecimal digits to be in UPPER CASE. I do not see you making sure of such.
Edit: nevermind, found the difference... + vs &
You're right Kane, I missed that.
rabbit: it might be useful for you to break out subexpressions and write them to output somewhere that you can evaluate everything to make sure you're doing it right. I might suggest this:
<?php function BNLSChecksum($password, $servercode) { $val = "0000000" + $servercode;// Print $val here. $len = strlen($val); // is that the right declaration? I can never remember these variable $ symbols.// Print $len $part = substr($val, $len - 8);// Print $part $crcexp = $password + $part;// Print $crcexp $crcval = CRC32($crcexp);// Print $crcval return $crcval; }?>
If you do something like that, it should probably become obvious where the problem is.
The 8 "0"'s came from my port from the VB function. I'll break it up like you suggested, and I'll see what happens.
<?phpfunction BNLSChecksum($password, $servercode){$val = "0000000" . $servercode;return CRC32($password . substr($val, strlen($val) - 8));}
'.' is the string concat operator.
Yes, by stepping through I found a few problems:
1. You must use dechex() on the $servercode or else it will insert the dec value (which is wrong)
2. You must convert the new $servercode to upper case
3. You must use '.' (as Banana said), not '+'
That's all I can remember for now, here's the final, working code:
<?php function ucase($str) { $lc = array('a', 'b', 'c', 'd', 'e', 'f'); $uc = array('A', 'B', 'C', 'D', 'E', 'F'); $final = str_replace($lc, $uc, $str); return $final; } function BNLSChecksum($password, $servercode) { $uc_sc = ucase(dechex($servercode)); // echo '$uc_sc = ' . $uc_sc . "<BR>\r\n"; $val = "0000000" . $uc_sc; // echo '$val = ' . $val . "<BR>\r\n"; $len = strlen($val); // echo '$len = ' . $len . "<BR>\r\n"; $part = substr($val, $len - 8); // echo '$part = ' . $part . "<BR>\r\n"; $crcexp = $password . $part; // echo '$crcexp = ' . $crcexp . "<BR>\r\n"; $crcval = CRC32($crcexp); // echo '$crcval = ' . $crcval . "<BR>\r\n"; return $crcval; }?>
Out of curiosity, what will this be used for?
A PHP Bot (a real one, that is).