Skip to content

Commit 8b891bb

Browse files
authored
Fix 32-bit Problem in PasswordEncoder (#2551)
* Fix 32-bit Problem in PasswordEncoder Fix #2550. * Update change log
1 parent 2f4da6e commit 8b891bb

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

docs/changes/2.x/2.0.0.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
- TemplateProcessor Persist File After Destruct [@oleibman](https://github.com/oleibman) fixing [#2539](https://github.com/PHPOffice/PHPWord/issues/2539) in [#2545](https://github.com/PHPOffice/PHPWord/pull/2545)
1313
- bug: TemplateProcessor fix multiline values [@gimler](https://github.com/gimler) fixing [#268](https://github.com/PHPOffice/PHPWord/issues/268), [#2323](https://github.com/PHPOffice/PHPWord/issues/2323) and [#2486](https://github.com/PHPOffice/PHPWord/issues/2486) in [#2522](https://github.com/PHPOffice/PHPWord/pull/2522)
1414

15+
- 32-bit Problem in PasswordEncoder [@oleibman](https://github.com/oleibman) fixing [#2550](https://github.com/PHPOffice/PHPWord/issues/2550) in [#2551](https://github.com/PHPOffice/PHPWord/pull/2551)
16+
1517
### Miscellaneous
1618

1719
- Bump dompdf/dompdf from 2.0.3 to 2.0.4 by [@dependabot](https://github.com/dependabot) in [#2530](https://github.com/PHPOffice/PHPWord/pull/2530)

src/PhpWord/Shared/Microsoft/PasswordEncoder.php

+7-4
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)