note 58186 deleted from language.operators.bitwise by cmb

From: Date: Sun, 20 Oct 2019 08:51:06 +0000
Subject: note 58186 deleted from language.operators.bitwise by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212699@lists.php.net to get a copy of this message
Note Submitter: zlel ---- Here're my 32-bit carry-discarding operations for those of you porting encryption algorithms from C. Be warned that some of these are not very efficient compared to the native operations, especially when called by heavy-duty encryption algorithms - but not discarding the carry bit may not land you the same results you get in C, simply because PHP's bitwise operations were not designed to work on fixed-sized registers. (If your ported encryption algo still doen't give you the same results, remember to check your Endian-ness!) function _BF_SHR32 ($x, $bits) { if ($bits==0) return $x; if ($bits==32) return 0; $y = ($x & 0x7FFFFFFF) >> $bits; if (0x80000000 & $x) { $y |= (1<<(31-$bits)); } return $y; } function _BF_SHL32 ($x, $bits) { if ($bits==0) return $x; if ($bits==32) return 0; $mask = (1<<(32-$bits)) - 1; return (($x & $mask) << $bits) & 0xFFFFFFFF; } function _BF_GETBYTE ($x, $y) { return _BF_SHR32 ($x, 8 * $y) & 0xFF; } function _BF_OR32 ($x, $y) { return ($x | $y) & 0xFFFFFFFF; } function _BF_ADD32 ($x, $y) { $x = $x & 0xFFFFFFFF; $y = $y & 0xFFFFFFFF; $total = 0; $carry = 0; for ($i=0; $i<4; $i++) { $byte_x = _BF_GETBYTE($x, $i); $byte_y = _BF_GETBYTE($y, $i); $sum = $byte_x + $byte_y; $result = $sum & 0xFF; $carryforward = _BF_SHR32($sum, 8); $sum = $result + $carry; $result = $sum & 0xFF; $carry = $carryforward + _BF_SHR32($sum, 8); $total = _BF_OR32(_BF_SHL32($result, $i*8), $total); } return $total; }

« previous php.notes (#212699) next »