2014-04-16 22:13:00 +07:00
|
|
|
|
<?php
|
|
|
|
|
|
/**
|
2014-05-05 13:06:53 +04:00
|
|
|
|
* This file is part of PHPWord - A pure PHP library for reading and writing
|
|
|
|
|
|
* word processing documents.
|
|
|
|
|
|
*
|
|
|
|
|
|
* PHPWord is free software distributed under the terms of the GNU Lesser
|
|
|
|
|
|
* General Public License version 3 as published by the Free Software Foundation.
|
|
|
|
|
|
*
|
|
|
|
|
|
* For the full copyright and license information, please read the LICENSE
|
|
|
|
|
|
* file that was distributed with this source code. For the full list of
|
|
|
|
|
|
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
|
2014-04-16 22:13:00 +07:00
|
|
|
|
*
|
|
|
|
|
|
* @link https://github.com/PHPOffice/PHPWord
|
2016-07-31 12:35:06 +04:00
|
|
|
|
* @copyright 2010-2016 PHPWord contributors
|
2014-05-04 21:03:28 +04:00
|
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
2014-04-16 22:13:00 +07:00
|
|
|
|
*/
|
|
|
|
|
|
|
2014-04-30 08:54:38 +07:00
|
|
|
|
namespace PhpOffice\PhpWord\Writer\Word2007\Part;
|
2014-04-16 22:13:00 +07:00
|
|
|
|
|
|
|
|
|
|
/**
|
2014-05-06 08:50:55 +07:00
|
|
|
|
* Word2007 settings part writer: word/settings.xml
|
2014-05-18 22:23:28 +07:00
|
|
|
|
*
|
|
|
|
|
|
* @link http://www.schemacentral.com/sc/ooxml/t-w_CT_Settings.html
|
2014-04-16 22:13:00 +07:00
|
|
|
|
*/
|
2014-04-30 08:54:38 +07:00
|
|
|
|
class Settings extends AbstractPart
|
2014-04-16 22:13:00 +07:00
|
|
|
|
{
|
2017-03-10 16:24:52 +01:00
|
|
|
|
static $algorithmMapping = [
|
|
|
|
|
|
1 => 'md2',
|
|
|
|
|
|
2 => 'md4',
|
|
|
|
|
|
3 => 'md5',
|
|
|
|
|
|
4 => 'sha1',
|
|
|
|
|
|
5 => '', // 'mac' -> not possible with hash()
|
|
|
|
|
|
6 => 'ripemd',
|
|
|
|
|
|
7 => 'ripemd160',
|
|
|
|
|
|
8 => '',
|
|
|
|
|
|
9 => '', //'hmac' -> not possible with hash()
|
|
|
|
|
|
10 => '',
|
|
|
|
|
|
11 => '',
|
|
|
|
|
|
12 => 'sha256',
|
|
|
|
|
|
13 => 'sha384',
|
|
|
|
|
|
14 => 'sha512',
|
|
|
|
|
|
];
|
|
|
|
|
|
static $initialCodeArray = [
|
|
|
|
|
|
0xE1F0,
|
|
|
|
|
|
0x1D0F,
|
|
|
|
|
|
0xCC9C,
|
|
|
|
|
|
0x84C0,
|
|
|
|
|
|
0x110C,
|
|
|
|
|
|
0x0E10,
|
|
|
|
|
|
0xF1CE,
|
|
|
|
|
|
0x313E,
|
|
|
|
|
|
0x1872,
|
|
|
|
|
|
0xE139,
|
|
|
|
|
|
0xD40F,
|
|
|
|
|
|
0x84F9,
|
|
|
|
|
|
0x280C,
|
|
|
|
|
|
0xA96A,
|
|
|
|
|
|
0x4EC3
|
|
|
|
|
|
];
|
|
|
|
|
|
static $encryptionMatrix =
|
|
|
|
|
|
[
|
|
|
|
|
|
[0xAEFC, 0x4DD9, 0x9BB2, 0x2745, 0x4E8A, 0x9D14, 0x2A09],
|
|
|
|
|
|
[0x7B61, 0xF6C2, 0xFDA5, 0xEB6B, 0xC6F7, 0x9DCF, 0x2BBF],
|
|
|
|
|
|
[0x4563, 0x8AC6, 0x05AD, 0x0B5A, 0x16B4, 0x2D68, 0x5AD0],
|
|
|
|
|
|
[0x0375, 0x06EA, 0x0DD4, 0x1BA8, 0x3750, 0x6EA0, 0xDD40],
|
|
|
|
|
|
[0xD849, 0xA0B3, 0x5147, 0xA28E, 0x553D, 0xAA7A, 0x44D5],
|
|
|
|
|
|
[0x6F45, 0xDE8A, 0xAD35, 0x4A4B, 0x9496, 0x390D, 0x721A],
|
|
|
|
|
|
[0xEB23, 0xC667, 0x9CEF, 0x29FF, 0x53FE, 0xA7FC, 0x5FD9],
|
|
|
|
|
|
[0x47D3, 0x8FA6, 0x0F6D, 0x1EDA, 0x3DB4, 0x7B68, 0xF6D0],
|
|
|
|
|
|
[0xB861, 0x60E3, 0xC1C6, 0x93AD, 0x377B, 0x6EF6, 0xDDEC],
|
|
|
|
|
|
[0x45A0, 0x8B40, 0x06A1, 0x0D42, 0x1A84, 0x3508, 0x6A10],
|
|
|
|
|
|
[0xAA51, 0x4483, 0x8906, 0x022D, 0x045A, 0x08B4, 0x1168],
|
|
|
|
|
|
[0x76B4, 0xED68, 0xCAF1, 0x85C3, 0x1BA7, 0x374E, 0x6E9C],
|
|
|
|
|
|
[0x3730, 0x6E60, 0xDCC0, 0xA9A1, 0x4363, 0x86C6, 0x1DAD],
|
|
|
|
|
|
[0x3331, 0x6662, 0xCCC4, 0x89A9, 0x0373, 0x06E6, 0x0DCC],
|
|
|
|
|
|
[0x1021, 0x2042, 0x4084, 0x8108, 0x1231, 0x2462, 0x48C4]
|
|
|
|
|
|
];
|
|
|
|
|
|
static $passwordMaxLength = 15;
|
|
|
|
|
|
|
2014-06-21 03:07:11 +07:00
|
|
|
|
/**
|
|
|
|
|
|
* Settings value
|
|
|
|
|
|
*
|
|
|
|
|
|
* @var array
|
|
|
|
|
|
*/
|
|
|
|
|
|
private $settings = array();
|
|
|
|
|
|
|
2014-04-16 22:13:00 +07:00
|
|
|
|
/**
|
2014-05-06 08:50:55 +07:00
|
|
|
|
* Write part
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return string
|
2014-04-16 22:13:00 +07:00
|
|
|
|
*/
|
2014-05-06 01:45:13 +07:00
|
|
|
|
public function write()
|
2014-04-16 22:13:00 +07:00
|
|
|
|
{
|
2014-06-21 03:07:11 +07:00
|
|
|
|
$this->getSettings();
|
2014-06-16 07:32:57 +07:00
|
|
|
|
|
|
|
|
|
|
$xmlWriter = $this->getXmlWriter();
|
|
|
|
|
|
|
|
|
|
|
|
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
|
|
|
|
|
|
$xmlWriter->startElement('w:settings');
|
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
|
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
|
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math');
|
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:sl', 'http://schemas.openxmlformats.org/schemaLibrary/2006/main');
|
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
|
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
|
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word');
|
|
|
|
|
|
|
2014-06-21 03:07:11 +07:00
|
|
|
|
foreach ($this->settings as $settingKey => $settingValue) {
|
2014-06-16 07:32:57 +07:00
|
|
|
|
$this->writeSetting($xmlWriter, $settingKey, $settingValue);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$xmlWriter->endElement(); // w:settings
|
|
|
|
|
|
|
|
|
|
|
|
return $xmlWriter->getData();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-03 15:40:24 +04:00
|
|
|
|
* Write indivual setting, recursive to any child settings.
|
2014-06-16 07:32:57 +07:00
|
|
|
|
*
|
2016-01-23 19:16:34 +04:00
|
|
|
|
* @param \PhpOffice\Common\XMLWriter $xmlWriter
|
2014-06-16 07:32:57 +07:00
|
|
|
|
* @param string $settingKey
|
|
|
|
|
|
* @param array|string $settingValue
|
2014-07-03 15:40:24 +04:00
|
|
|
|
* @return void
|
2014-06-16 07:32:57 +07:00
|
|
|
|
*/
|
|
|
|
|
|
protected function writeSetting($xmlWriter, $settingKey, $settingValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
if ($settingValue == '') {
|
|
|
|
|
|
$xmlWriter->writeElement($settingKey);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$xmlWriter->startElement($settingKey);
|
|
|
|
|
|
|
|
|
|
|
|
/** @var array $settingValue Type hint */
|
|
|
|
|
|
foreach ($settingValue as $childKey => $childValue) {
|
|
|
|
|
|
if ($childKey == '@attributes') {
|
|
|
|
|
|
foreach ($childValue as $key => $val) {
|
|
|
|
|
|
$xmlWriter->writeAttribute($key, $val);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$this->writeSetting($xmlWriter, $childKey, $childValue);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-03 15:40:24 +04:00
|
|
|
|
* Get settings.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return void
|
2014-06-16 07:32:57 +07:00
|
|
|
|
*/
|
|
|
|
|
|
private function getSettings()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Default settings
|
2014-06-21 03:07:11 +07:00
|
|
|
|
$this->settings = array(
|
2014-04-16 22:13:00 +07:00
|
|
|
|
'w:zoom' => array('@attributes' => array('w:percent' => '100')),
|
|
|
|
|
|
'w:defaultTabStop' => array('@attributes' => array('w:val' => '708')),
|
|
|
|
|
|
'w:hyphenationZone' => array('@attributes' => array('w:val' => '425')),
|
|
|
|
|
|
'w:characterSpacingControl' => array('@attributes' => array('w:val' => 'doNotCompress')),
|
2014-06-21 03:07:11 +07:00
|
|
|
|
'w:themeFontLang' => array('@attributes' => array('w:val' => 'en-US')),
|
|
|
|
|
|
'w:decimalSymbol' => array('@attributes' => array('w:val' => '.')),
|
|
|
|
|
|
'w:listSeparator' => array('@attributes' => array('w:val' => ';')),
|
2016-10-28 20:06:03 -04:00
|
|
|
|
'w:compat' => array(),
|
2014-04-16 22:13:00 +07:00
|
|
|
|
'm:mathPr' => array(
|
|
|
|
|
|
'm:mathFont' => array('@attributes' => array('m:val' => 'Cambria Math')),
|
|
|
|
|
|
'm:brkBin' => array('@attributes' => array('m:val' => 'before')),
|
|
|
|
|
|
'm:brkBinSub' => array('@attributes' => array('m:val' => '--')),
|
|
|
|
|
|
'm:smallFrac' => array('@attributes' => array('m:val' => 'off')),
|
|
|
|
|
|
'm:dispDef' => '',
|
|
|
|
|
|
'm:lMargin' => array('@attributes' => array('m:val' => '0')),
|
|
|
|
|
|
'm:rMargin' => array('@attributes' => array('m:val' => '0')),
|
|
|
|
|
|
'm:defJc' => array('@attributes' => array('m:val' => 'centerGroup')),
|
|
|
|
|
|
'm:wrapIndent' => array('@attributes' => array('m:val' => '1440')),
|
|
|
|
|
|
'm:intLim' => array('@attributes' => array('m:val' => 'subSup')),
|
|
|
|
|
|
'm:naryLim' => array('@attributes' => array('m:val' => 'undOvr')),
|
|
|
|
|
|
),
|
|
|
|
|
|
'w:clrSchemeMapping' => array(
|
|
|
|
|
|
'@attributes' => array(
|
|
|
|
|
|
'w:bg1' => 'light1',
|
|
|
|
|
|
'w:t1' => 'dark1',
|
|
|
|
|
|
'w:bg2' => 'light2',
|
|
|
|
|
|
'w:t2' => 'dark2',
|
|
|
|
|
|
'w:accent1' => 'accent1',
|
|
|
|
|
|
'w:accent2' => 'accent2',
|
|
|
|
|
|
'w:accent3' => 'accent3',
|
|
|
|
|
|
'w:accent4' => 'accent4',
|
|
|
|
|
|
'w:accent5' => 'accent5',
|
|
|
|
|
|
'w:accent6' => 'accent6',
|
|
|
|
|
|
'w:hyperlink' => 'hyperlink',
|
|
|
|
|
|
'w:followedHyperlink' => 'followedHyperlink',
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
2014-06-21 03:07:11 +07:00
|
|
|
|
|
|
|
|
|
|
// Other settings
|
|
|
|
|
|
$this->getProtection();
|
|
|
|
|
|
$this->getCompatibility();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-03 15:40:24 +04:00
|
|
|
|
* Get protection settings.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return void
|
2014-06-21 03:07:11 +07:00
|
|
|
|
*/
|
|
|
|
|
|
private function getProtection()
|
|
|
|
|
|
{
|
|
|
|
|
|
$protection = $this->getParentWriter()->getPhpWord()->getProtection();
|
|
|
|
|
|
if ($protection->getEditing() !== null) {
|
2017-03-10 13:28:32 +01:00
|
|
|
|
if (empty($protection->getPassword())) {
|
|
|
|
|
|
$this->settings['w:documentProtection'] = array(
|
|
|
|
|
|
'@attributes' => array(
|
|
|
|
|
|
'w:enforcement' => 1,
|
|
|
|
|
|
'w:edit' => $protection->getEditing(),
|
|
|
|
|
|
)
|
|
|
|
|
|
);
|
|
|
|
|
|
} else {
|
2017-03-10 16:57:42 +01:00
|
|
|
|
if ($protection->getSalt() == null) {
|
|
|
|
|
|
$protection->setSalt(openssl_random_pseudo_bytes(16));
|
|
|
|
|
|
}
|
2017-03-10 13:28:32 +01:00
|
|
|
|
$this->settings['w:documentProtection'] = array(
|
|
|
|
|
|
'@attributes' => array(
|
|
|
|
|
|
'w:enforcement' => 1,
|
|
|
|
|
|
'w:edit' => $protection->getEditing(),
|
|
|
|
|
|
'w:cryptProviderType' => 'rsaFull',
|
|
|
|
|
|
'w:cryptAlgorithmClass' => 'hash',
|
|
|
|
|
|
'w:cryptAlgorithmType' => 'typeAny',
|
2017-03-10 16:57:42 +01:00
|
|
|
|
'w:cryptAlgorithmSid' => $protection->getMswordAlgorithmSid(),
|
2017-03-10 13:28:32 +01:00
|
|
|
|
'w:cryptSpinCount' => $protection->getSpinCount(),
|
2017-03-13 16:22:04 +01:00
|
|
|
|
'w:hash' => $this->getEncodedPasswordHash($protection),
|
|
|
|
|
|
'w:salt' => base64_encode($protection->getSalt()),
|
2017-03-10 13:28:32 +01:00
|
|
|
|
)
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2014-06-21 03:07:11 +07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-03 15:40:24 +04:00
|
|
|
|
* Get compatibility setting.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return void
|
2014-06-21 03:07:11 +07:00
|
|
|
|
*/
|
|
|
|
|
|
private function getCompatibility()
|
|
|
|
|
|
{
|
|
|
|
|
|
$compatibility = $this->getParentWriter()->getPhpWord()->getCompatibility();
|
|
|
|
|
|
if ($compatibility->getOoxmlVersion() !== null) {
|
2017-03-10 16:57:42 +01:00
|
|
|
|
$this->settings['w:compat']['w:compatSetting'] = array(
|
|
|
|
|
|
'@attributes' => array(
|
|
|
|
|
|
'w:name' => 'compatibilityMode',
|
|
|
|
|
|
'w:uri' => 'http://schemas.microsoft.com/office/word',
|
|
|
|
|
|
'w:val' => $compatibility->getOoxmlVersion(),
|
|
|
|
|
|
)
|
|
|
|
|
|
);
|
2014-06-21 03:07:11 +07:00
|
|
|
|
}
|
2014-04-16 22:13:00 +07:00
|
|
|
|
}
|
2017-03-10 16:24:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Create a hashed password that MS Word will be able to work with
|
2017-03-13 16:22:04 +01:00
|
|
|
|
* @link https://blogs.msdn.microsoft.com/vsod/2010/04/05/how-to-set-the-editing-restrictions-in-word-using-open-xml-sdk-2-0/
|
2017-03-10 16:24:52 +01:00
|
|
|
|
*
|
|
|
|
|
|
* @param \PhpOffice\PhpWord\Metadata\Protection $protection
|
|
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
2017-03-13 16:22:04 +01:00
|
|
|
|
private function getEncodedPasswordHash($protection)
|
2017-03-10 16:24:52 +01:00
|
|
|
|
{
|
|
|
|
|
|
$orig_encoding = mb_internal_encoding();
|
|
|
|
|
|
mb_internal_encoding("UTF-8");
|
|
|
|
|
|
|
|
|
|
|
|
$password = $protection->getPassword();
|
|
|
|
|
|
$password = mb_substr($password, 0, min(self::$passwordMaxLength, mb_strlen($password)));
|
|
|
|
|
|
|
|
|
|
|
|
// Get the single-byte values by iterating through the Unicode characters of the truncated password.
|
|
|
|
|
|
// For each character, if the low byte is not equal to 0, take it. Otherwise, take the high byte.
|
|
|
|
|
|
$pass_utf8 = mb_convert_encoding($password, 'UCS-2LE', 'UTF-8');
|
|
|
|
|
|
$byteChars = [];
|
|
|
|
|
|
for ($i = 0; $i < mb_strlen($password); $i++) {
|
|
|
|
|
|
$byteChars[$i] = ord(substr($pass_utf8, $i * 2, 1));
|
|
|
|
|
|
if ($byteChars[$i] == 0) {
|
|
|
|
|
|
$byteChars[$i] = ord(substr($pass_utf8, $i * 2 + 1, 1));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// build low-order word and hig-order word and combine them
|
|
|
|
|
|
$combinedKey = $this->buildCombinedKey($byteChars);
|
|
|
|
|
|
// build reversed hexadecimal string
|
2017-03-10 17:30:51 +01:00
|
|
|
|
$hex = str_pad(strtoupper(dechex($combinedKey & 0xFFFFFFFF)), 8, '0', \STR_PAD_LEFT);
|
2017-03-10 16:57:42 +01:00
|
|
|
|
$reversedHex = $hex[6] . $hex[7] . $hex[4] . $hex[5] . $hex[2] . $hex[3] . $hex[0] . $hex[1];
|
2017-03-10 16:24:52 +01:00
|
|
|
|
|
|
|
|
|
|
$generatedKey = mb_convert_encoding($reversedHex, 'UCS-2LE', 'UTF-8');
|
|
|
|
|
|
|
|
|
|
|
|
// Implementation Notes List:
|
|
|
|
|
|
// Word requires that the initial hash of the password with the salt not be considered in the count.
|
|
|
|
|
|
// The initial hash of salt + key is not included in the iteration count.
|
2017-03-10 16:57:42 +01:00
|
|
|
|
$algorithm = $this->getAlgorithm($protection->getMswordAlgorithmSid());
|
2017-03-13 16:22:04 +01:00
|
|
|
|
$generatedKey = hash($algorithm, $protection->getSalt() . $generatedKey, true);
|
2017-03-10 16:24:52 +01:00
|
|
|
|
|
2017-03-10 16:57:42 +01:00
|
|
|
|
for ($i = 0; $i < $protection->getSpinCount(); $i++) {
|
|
|
|
|
|
$generatedKey = hash($algorithm, $generatedKey . pack("CCCC", $i, $i >> 8, $i >> 16, $i >> 24), true);
|
2017-03-10 16:24:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
$generatedKey = base64_encode($generatedKey);
|
|
|
|
|
|
|
|
|
|
|
|
mb_internal_encoding($orig_encoding);
|
|
|
|
|
|
|
|
|
|
|
|
return $generatedKey;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Get algorithm from self::$algorithmMapping
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param int $sid
|
|
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
|
|
|
|
|
private function getAlgorithm($sid)
|
|
|
|
|
|
{
|
|
|
|
|
|
$algorithm = self::$algorithmMapping[$sid];
|
|
|
|
|
|
if ($algorithm == '') {
|
|
|
|
|
|
$algorithm = 'sha1';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $algorithm;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Build combined key from low-order word and high-order word
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param array $byteChars -> byte array representation of password
|
|
|
|
|
|
* @return int
|
|
|
|
|
|
*/
|
|
|
|
|
|
private function buildCombinedKey($byteChars)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Compute the high-order word
|
|
|
|
|
|
// Initialize from the initial code array (see above), depending on the passwords length.
|
|
|
|
|
|
$highOrderWord = self::$initialCodeArray[sizeof($byteChars) - 1];
|
|
|
|
|
|
|
|
|
|
|
|
// For each character in the password:
|
|
|
|
|
|
// For every bit in the character, starting with the least significant and progressing to (but excluding)
|
|
|
|
|
|
// the most significant, if the bit is set, XOR the key’s high-order word with the corresponding word from
|
|
|
|
|
|
// the Encryption Matrix
|
|
|
|
|
|
for ($i = 0; $i < sizeof($byteChars); $i++) {
|
|
|
|
|
|
$tmp = self::$passwordMaxLength - sizeof($byteChars) + $i;
|
|
|
|
|
|
$matrixRow = self::$encryptionMatrix[$tmp];
|
|
|
|
|
|
for ($intBit = 0; $intBit < 7; $intBit++) {
|
|
|
|
|
|
if (($byteChars[$i] & (0x0001 << $intBit)) != 0) {
|
|
|
|
|
|
$highOrderWord = ($highOrderWord ^ $matrixRow[$intBit]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Compute low-order word
|
|
|
|
|
|
// Initialize with 0
|
|
|
|
|
|
$lowOrderWord = 0;
|
|
|
|
|
|
// For each character in the password, going backwards
|
|
|
|
|
|
for ($i = sizeof($byteChars) - 1; $i >= 0; $i--) {
|
|
|
|
|
|
// low-order word = (((low-order word SHR 14) AND 0x0001) OR (low-order word SHL 1) AND 0x7FFF)) XOR character
|
|
|
|
|
|
$lowOrderWord = (((($lowOrderWord >> 14) & 0x0001) | (($lowOrderWord << 1) & 0x7FFF)) ^ $byteChars[$i]);
|
|
|
|
|
|
}
|
|
|
|
|
|
// Lastly, low-order word = (((low-order word SHR 14) AND 0x0001) OR (low-order word SHL 1) AND 0x7FFF)) XOR strPassword length XOR 0xCE4B.
|
|
|
|
|
|
$lowOrderWord = (((($lowOrderWord >> 14) & 0x0001) | (($lowOrderWord << 1) & 0x7FFF)) ^ sizeof($byteChars) ^ 0xCE4B);
|
|
|
|
|
|
|
|
|
|
|
|
// Combine the Low and High Order Word
|
|
|
|
|
|
return $this->int32(($highOrderWord << 16) + $lowOrderWord);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-03-13 16:22:04 +01:00
|
|
|
|
* Simulate behaviour of (signed) int32
|
2017-03-10 16:24:52 +01:00
|
|
|
|
*
|
|
|
|
|
|
* @param int $value
|
|
|
|
|
|
* @return int
|
|
|
|
|
|
*/
|
|
|
|
|
|
private function int32($value)
|
|
|
|
|
|
{
|
|
|
|
|
|
$value = ($value & 0xFFFFFFFF);
|
|
|
|
|
|
|
|
|
|
|
|
if ($value & 0x80000000) {
|
|
|
|
|
|
$value = -((~$value & 0xFFFFFFFF) + 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
|
|
}
|
2014-04-16 22:13:00 +07:00
|
|
|
|
}
|