Skip to content

Commit 7e21348

Browse files
committed
Fix 32-bit Problem in PasswordEncoder
Fix #2550.
1 parent 9bf816b commit 7e21348

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/PhpWord/Shared/Microsoft/PasswordEncoder.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class PasswordEncoder
3434
const ALGORITHM_MAC = 'MAC';
3535
const ALGORITHM_HMAC = 'HMAC';
3636

37+
private const ALL_ONE_BITS = (PHP_INT_SIZE > 4) ? 0xFFFFFFFF : -1;
38+
private const HIGH_ORDER_BIT = (PHP_INT_SIZE > 4) ? 0x80000000 : PHP_INT_MIN;
39+
3740
/**
3841
* Mapping between algorithm name and algorithm ID.
3942
*
@@ -128,7 +131,7 @@ public static function hashPassword($password, $algorithmName = self::ALGORITHM_
128131
// build low-order word and hig-order word and combine them
129132
$combinedKey = self::buildCombinedKey($byteChars);
130133
// build reversed hexadecimal string
131-
$hex = str_pad(strtoupper(dechex($combinedKey & 0xFFFFFFFF)), 8, '0', \STR_PAD_LEFT);
134+
$hex = str_pad(strtoupper(dechex($combinedKey & self::ALL_ONE_BITS)), 8, '0', \STR_PAD_LEFT);
132135
$reversedHex = $hex[6] . $hex[7] . $hex[4] . $hex[5] . $hex[2] . $hex[3] . $hex[0] . $hex[1];
133136

134137
$generatedKey = mb_convert_encoding($reversedHex, 'UCS-2LE', 'UTF-8');
@@ -232,10 +235,10 @@ private static function buildCombinedKey($byteChars)
232235
*/
233236
private static function int32($value)
234237
{
235-
$value = ($value & 0xFFFFFFFF);
238+
$value = $value & self::ALL_ONE_BITS;
236239

237-
if ($value & 0x80000000) {
238-
$value = -((~$value & 0xFFFFFFFF) + 1);
240+
if ($value & self::HIGH_ORDER_BIT) {
241+
$value = -((~$value & self::ALL_ONE_BITS) + 1);
239242
}
240243

241244
return $value;

0 commit comments

Comments
 (0)