PHP is one of those languages I use periodically, but usually have to dust the cobwebs off when I start using it again. The same applies this week whilst I port some C code to PHP. This uses a lot of AES encryption and SHA256 hashing - all working fine so far. However, decrypted strings come out in "C" form - i.e. terminated with a zero byte followed by "garbage" padding bytes.
I currently "trim" these C-style strings to PHP form with the following:
$iv = strpos( $hashsalt8, "\0");
if ($iv)
$hashsalt8 = substr( $hashsalt8, 0, $iv );
Seems long-winded and that there should be a one line function call instead, but I can't find it?
Note: Although in this case the "hash salt" name implies I might know the length of the original string, this is unknown in the general case. Clearly a one line solution is available using substr()
when the length is known a priori.
Use strstr
:
$hashsalt8 = strstr($hashsalt8, "\0", TRUE);
An uglier solution for versions less than 5.3.0 (where the third parameter isn't available) uses the strrev
, substr
and strrchr
functions instead:
$hashsalt8 = strrev(substr(strrchr(strrev($hashsalt8), "\0"), 1));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With