Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 32-bit Problem in PasswordEncoder #2551

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/changes/2.x/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

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

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

### Miscellaneous

- 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)
Expand Down
11 changes: 7 additions & 4 deletions src/PhpWord/Shared/Microsoft/PasswordEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class PasswordEncoder
const ALGORITHM_MAC = 'MAC';
const ALGORITHM_HMAC = 'HMAC';

private const ALL_ONE_BITS = (PHP_INT_SIZE > 4) ? 0xFFFFFFFF : -1;
private const HIGH_ORDER_BIT = (PHP_INT_SIZE > 4) ? 0x80000000 : PHP_INT_MIN;

/**
* Mapping between algorithm name and algorithm ID.
*
Expand Down Expand Up @@ -128,7 +131,7 @@ public static function hashPassword($password, $algorithmName = self::ALGORITHM_
// build low-order word and hig-order word and combine them
$combinedKey = self::buildCombinedKey($byteChars);
// build reversed hexadecimal string
$hex = str_pad(strtoupper(dechex($combinedKey & 0xFFFFFFFF)), 8, '0', \STR_PAD_LEFT);
$hex = str_pad(strtoupper(dechex($combinedKey & self::ALL_ONE_BITS)), 8, '0', \STR_PAD_LEFT);
$reversedHex = $hex[6] . $hex[7] . $hex[4] . $hex[5] . $hex[2] . $hex[3] . $hex[0] . $hex[1];

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

if ($value & 0x80000000) {
$value = -((~$value & 0xFFFFFFFF) + 1);
if ($value & self::HIGH_ORDER_BIT) {
$value = -((~$value & self::ALL_ONE_BITS) + 1);
}

return $value;
Expand Down
Loading