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
Changes from 1 commit
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
Next Next commit
Fix 32-bit Problem in PasswordEncoder
Fix #2550.
  • Loading branch information
oleibman committed Jan 15, 2024
commit 7e21348e06dfc771bd8be977f2a027b74e78396e
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