Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
38 / 38 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
PasswordEncoder | |
100.00% |
38 / 38 |
|
100.00% |
4 / 4 |
14 | |
100.00% |
1 / 1 |
hashPassword | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
4 | |||
getAlgorithm | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getAlgorithmId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
buildCombinedKey | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
5 | |||
int32 | n/a |
0 / 0 |
n/a |
0 / 0 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * This file is part of PHPWord - A pure PHP library for reading and writing |
5 | * word processing documents. |
6 | * |
7 | * PHPWord is free software distributed under the terms of the GNU Lesser |
8 | * General Public License version 3 as published by the Free Software Foundation. |
9 | * |
10 | * For the full copyright and license information, please read the LICENSE |
11 | * file that was distributed with this source code. For the full list of |
12 | * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
13 | * |
14 | * @see https://github.com/PHPOffice/PHPWord |
15 | * |
16 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
17 | */ |
18 | |
19 | namespace PhpOffice\PhpWord\Shared\Microsoft; |
20 | |
21 | /** |
22 | * Password encoder for microsoft office applications. |
23 | */ |
24 | class PasswordEncoder |
25 | { |
26 | const ALGORITHM_MD2 = 'MD2'; |
27 | const ALGORITHM_MD4 = 'MD4'; |
28 | const ALGORITHM_MD5 = 'MD5'; |
29 | const ALGORITHM_SHA_1 = 'SHA-1'; |
30 | const ALGORITHM_SHA_256 = 'SHA-256'; |
31 | const ALGORITHM_SHA_384 = 'SHA-384'; |
32 | const ALGORITHM_SHA_512 = 'SHA-512'; |
33 | const ALGORITHM_RIPEMD = 'RIPEMD'; |
34 | const ALGORITHM_RIPEMD_160 = 'RIPEMD-160'; |
35 | const ALGORITHM_MAC = 'MAC'; |
36 | const ALGORITHM_HMAC = 'HMAC'; |
37 | |
38 | private const ALL_ONE_BITS = (PHP_INT_SIZE > 4) ? 0xFFFFFFFF : -1; |
39 | private const HIGH_ORDER_BIT = (PHP_INT_SIZE > 4) ? 0x80000000 : PHP_INT_MIN; |
40 | |
41 | /** |
42 | * Mapping between algorithm name and algorithm ID. |
43 | * |
44 | * @var array |
45 | * |
46 | * @see https://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.writeprotection.cryptographicalgorithmsid(v=office.14).aspx |
47 | */ |
48 | private static $algorithmMapping = [ |
49 | self::ALGORITHM_MD2 => [1, 'md2'], |
50 | self::ALGORITHM_MD4 => [2, 'md4'], |
51 | self::ALGORITHM_MD5 => [3, 'md5'], |
52 | self::ALGORITHM_SHA_1 => [4, 'sha1'], |
53 | self::ALGORITHM_MAC => [5, ''], // 'mac' -> not possible with hash() |
54 | self::ALGORITHM_RIPEMD => [6, 'ripemd'], |
55 | self::ALGORITHM_RIPEMD_160 => [7, 'ripemd160'], |
56 | self::ALGORITHM_HMAC => [9, ''], //'hmac' -> not possible with hash() |
57 | self::ALGORITHM_SHA_256 => [12, 'sha256'], |
58 | self::ALGORITHM_SHA_384 => [13, 'sha384'], |
59 | self::ALGORITHM_SHA_512 => [14, 'sha512'], |
60 | ]; |
61 | |
62 | private static $initialCodeArray = [ |
63 | 0xE1F0, |
64 | 0x1D0F, |
65 | 0xCC9C, |
66 | 0x84C0, |
67 | 0x110C, |
68 | 0x0E10, |
69 | 0xF1CE, |
70 | 0x313E, |
71 | 0x1872, |
72 | 0xE139, |
73 | 0xD40F, |
74 | 0x84F9, |
75 | 0x280C, |
76 | 0xA96A, |
77 | 0x4EC3, |
78 | ]; |
79 | |
80 | private static $encryptionMatrix = [ |
81 | [0xAEFC, 0x4DD9, 0x9BB2, 0x2745, 0x4E8A, 0x9D14, 0x2A09], |
82 | [0x7B61, 0xF6C2, 0xFDA5, 0xEB6B, 0xC6F7, 0x9DCF, 0x2BBF], |
83 | [0x4563, 0x8AC6, 0x05AD, 0x0B5A, 0x16B4, 0x2D68, 0x5AD0], |
84 | [0x0375, 0x06EA, 0x0DD4, 0x1BA8, 0x3750, 0x6EA0, 0xDD40], |
85 | [0xD849, 0xA0B3, 0x5147, 0xA28E, 0x553D, 0xAA7A, 0x44D5], |
86 | [0x6F45, 0xDE8A, 0xAD35, 0x4A4B, 0x9496, 0x390D, 0x721A], |
87 | [0xEB23, 0xC667, 0x9CEF, 0x29FF, 0x53FE, 0xA7FC, 0x5FD9], |
88 | [0x47D3, 0x8FA6, 0x0F6D, 0x1EDA, 0x3DB4, 0x7B68, 0xF6D0], |
89 | [0xB861, 0x60E3, 0xC1C6, 0x93AD, 0x377B, 0x6EF6, 0xDDEC], |
90 | [0x45A0, 0x8B40, 0x06A1, 0x0D42, 0x1A84, 0x3508, 0x6A10], |
91 | [0xAA51, 0x4483, 0x8906, 0x022D, 0x045A, 0x08B4, 0x1168], |
92 | [0x76B4, 0xED68, 0xCAF1, 0x85C3, 0x1BA7, 0x374E, 0x6E9C], |
93 | [0x3730, 0x6E60, 0xDCC0, 0xA9A1, 0x4363, 0x86C6, 0x1DAD], |
94 | [0x3331, 0x6662, 0xCCC4, 0x89A9, 0x0373, 0x06E6, 0x0DCC], |
95 | [0x1021, 0x2042, 0x4084, 0x8108, 0x1231, 0x2462, 0x48C4], |
96 | ]; |
97 | |
98 | private static $passwordMaxLength = 15; |
99 | |
100 | /** |
101 | * Create a hashed password that MS Word will be able to work with. |
102 | * |
103 | * @see https://blogs.msdn.microsoft.com/vsod/2010/04/05/how-to-set-the-editing-restrictions-in-word-using-open-xml-sdk-2-0/ |
104 | * |
105 | * @param string $password |
106 | * @param string $algorithmName |
107 | * @param string $salt |
108 | * @param int $spinCount |
109 | * |
110 | * @return string |
111 | */ |
112 | public static function hashPassword($password, $algorithmName = self::ALGORITHM_SHA_1, $salt = null, $spinCount = 10000) |
113 | { |
114 | $origEncoding = mb_internal_encoding(); |
115 | mb_internal_encoding('UTF-8'); |
116 | |
117 | $password = mb_substr($password, 0, min(self::$passwordMaxLength, mb_strlen($password))); |
118 | |
119 | // Get the single-byte values by iterating through the Unicode characters of the truncated password. |
120 | // For each character, if the low byte is not equal to 0, take it. Otherwise, take the high byte. |
121 | $passUtf8 = mb_convert_encoding($password, 'UCS-2LE', 'UTF-8'); |
122 | $byteChars = []; |
123 | |
124 | for ($i = 0; $i < mb_strlen($password); ++$i) { |
125 | $byteChars[$i] = ord(substr($passUtf8, $i * 2, 1)); |
126 | |
127 | if ($byteChars[$i] == 0) { |
128 | $byteChars[$i] = ord(substr($passUtf8, $i * 2 + 1, 1)); |
129 | } |
130 | } |
131 | |
132 | // build low-order word and hig-order word and combine them |
133 | $combinedKey = self::buildCombinedKey($byteChars); |
134 | // build reversed hexadecimal string |
135 | $hex = str_pad(strtoupper(dechex($combinedKey & self::ALL_ONE_BITS)), 8, '0', \STR_PAD_LEFT); |
136 | $reversedHex = $hex[6] . $hex[7] . $hex[4] . $hex[5] . $hex[2] . $hex[3] . $hex[0] . $hex[1]; |
137 | |
138 | $generatedKey = mb_convert_encoding($reversedHex, 'UCS-2LE', 'UTF-8'); |
139 | |
140 | // Implementation Notes List: |
141 | // Word requires that the initial hash of the password with the salt not be considered in the count. |
142 | // The initial hash of salt + key is not included in the iteration count. |
143 | $algorithm = self::getAlgorithm($algorithmName); |
144 | $generatedKey = hash($algorithm, $salt . $generatedKey, true); |
145 | |
146 | for ($i = 0; $i < $spinCount; ++$i) { |
147 | $generatedKey = hash($algorithm, $generatedKey . pack('CCCC', $i, $i >> 8, $i >> 16, $i >> 24), true); |
148 | } |
149 | $generatedKey = base64_encode($generatedKey); |
150 | |
151 | mb_internal_encoding($origEncoding); |
152 | |
153 | return $generatedKey; |
154 | } |
155 | |
156 | /** |
157 | * Get algorithm from self::$algorithmMapping. |
158 | * |
159 | * @param string $algorithmName |
160 | * |
161 | * @return string |
162 | */ |
163 | private static function getAlgorithm($algorithmName) |
164 | { |
165 | $algorithm = self::$algorithmMapping[$algorithmName][1]; |
166 | if ($algorithm == '') { |
167 | $algorithm = 'sha1'; |
168 | } |
169 | |
170 | return $algorithm; |
171 | } |
172 | |
173 | /** |
174 | * Returns the algorithm ID. |
175 | * |
176 | * @param string $algorithmName |
177 | * |
178 | * @return int |
179 | */ |
180 | public static function getAlgorithmId($algorithmName) |
181 | { |
182 | return self::$algorithmMapping[$algorithmName][0]; |
183 | } |
184 | |
185 | /** |
186 | * Build combined key from low-order word and high-order word. |
187 | * |
188 | * @param array $byteChars byte array representation of password |
189 | * |
190 | * @return int |
191 | */ |
192 | private static function buildCombinedKey($byteChars) |
193 | { |
194 | $byteCharsLength = count($byteChars); |
195 | // Compute the high-order word |
196 | // Initialize from the initial code array (see above), depending on the passwords length. |
197 | $highOrderWord = self::$initialCodeArray[$byteCharsLength - 1]; |
198 | |
199 | // For each character in the password: |
200 | // For every bit in the character, starting with the least significant and progressing to (but excluding) |
201 | // the most significant, if the bit is set, XOR the key’s high-order word with the corresponding word from |
202 | // the Encryption Matrix |
203 | for ($i = 0; $i < $byteCharsLength; ++$i) { |
204 | $tmp = self::$passwordMaxLength - $byteCharsLength + $i; |
205 | $matrixRow = self::$encryptionMatrix[$tmp]; |
206 | for ($intBit = 0; $intBit < 7; ++$intBit) { |
207 | if (($byteChars[$i] & (0x0001 << $intBit)) != 0) { |
208 | $highOrderWord = ($highOrderWord ^ $matrixRow[$intBit]); |
209 | } |
210 | } |
211 | } |
212 | |
213 | // Compute low-order word |
214 | // Initialize with 0 |
215 | $lowOrderWord = 0; |
216 | // For each character in the password, going backwards |
217 | for ($i = $byteCharsLength - 1; $i >= 0; --$i) { |
218 | // low-order word = (((low-order word SHR 14) AND 0x0001) OR (low-order word SHL 1) AND 0x7FFF)) XOR character |
219 | $lowOrderWord = (((($lowOrderWord >> 14) & 0x0001) | (($lowOrderWord << 1) & 0x7FFF)) ^ $byteChars[$i]); |
220 | } |
221 | // Lastly, low-order word = (((low-order word SHR 14) AND 0x0001) OR (low-order word SHL 1) AND 0x7FFF)) XOR strPassword length XOR 0xCE4B. |
222 | $lowOrderWord = (((($lowOrderWord >> 14) & 0x0001) | (($lowOrderWord << 1) & 0x7FFF)) ^ $byteCharsLength ^ 0xCE4B); |
223 | |
224 | // Combine the Low and High Order Word |
225 | return self::int32(($highOrderWord << 16) + $lowOrderWord); |
226 | } |
227 | |
228 | /** |
229 | * Simulate behaviour of (signed) int32. |
230 | * |
231 | * @codeCoverageIgnore |
232 | * |
233 | * @param int $value |
234 | * |
235 | * @return int |
236 | */ |
237 | private static function int32($value) |
238 | { |
239 | $value = $value & self::ALL_ONE_BITS; |
240 | |
241 | if ($value & self::HIGH_ORDER_BIT) { |
242 | $value = -((~$value & self::ALL_ONE_BITS) + 1); |
243 | } |
244 | |
245 | return $value; |
246 | } |
247 | } |