@@ -225,8 +225,8 @@ private function getProtection()
225
225
'w:cryptAlgorithmType ' => 'typeAny ' ,
226
226
'w:cryptAlgorithmSid ' => $ protection ->getMswordAlgorithmSid (),
227
227
'w:cryptSpinCount ' => $ protection ->getSpinCount (),
228
- 'w:hash ' => $ this ->getPasswordHash ($ protection ),
229
- 'w:salt ' => $ this -> getSaltHash ($ protection ->getSalt ()),
228
+ 'w:hash ' => $ this ->getEncodedPasswordHash ($ protection ),
229
+ 'w:salt ' => base64_encode ($ protection ->getSalt ()),
230
230
)
231
231
);
232
232
}
@@ -255,19 +255,19 @@ private function getCompatibility()
255
255
256
256
/**
257
257
* Create a hashed password that MS Word will be able to work with
258
+ * @link https://blogs.msdn.microsoft.com/vsod/2010/04/05/how-to-set-the-editing-restrictions-in-word-using-open-xml-sdk-2-0/
258
259
*
259
260
* @param \PhpOffice\PhpWord\Metadata\Protection $protection
260
261
* @return string
261
262
*/
262
- private function getPasswordHash ($ protection )
263
+ private function getEncodedPasswordHash ($ protection )
263
264
{
264
265
$ orig_encoding = mb_internal_encoding ();
265
266
mb_internal_encoding ("UTF-8 " );
266
267
267
268
$ password = $ protection ->getPassword ();
268
269
$ password = mb_substr ($ password , 0 , min (self ::$ passwordMaxLength , mb_strlen ($ password )));
269
270
270
- // Construct a new NULL-terminated string consisting of single-byte characters:
271
271
// Get the single-byte values by iterating through the Unicode characters of the truncated password.
272
272
// For each character, if the low byte is not equal to 0, take it. Otherwise, take the high byte.
273
273
$ pass_utf8 = mb_convert_encoding ($ password , 'UCS-2LE ' , 'UTF-8 ' );
@@ -291,7 +291,7 @@ private function getPasswordHash($protection)
291
291
// Word requires that the initial hash of the password with the salt not be considered in the count.
292
292
// The initial hash of salt + key is not included in the iteration count.
293
293
$ algorithm = $ this ->getAlgorithm ($ protection ->getMswordAlgorithmSid ());
294
- $ generatedKey = hash ($ algorithm , base64_decode ( $ this -> getSaltHash ( $ protection ->getSalt ()) ) . $ generatedKey , true );
294
+ $ generatedKey = hash ($ algorithm , $ protection ->getSalt () . $ generatedKey , true );
295
295
296
296
for ($ i = 0 ; $ i < $ protection ->getSpinCount (); $ i ++) {
297
297
$ generatedKey = hash ($ algorithm , $ generatedKey . pack ("CCCC " , $ i , $ i >> 8 , $ i >> 16 , $ i >> 24 ), true );
@@ -319,17 +319,6 @@ private function getAlgorithm($sid)
319
319
return $ algorithm ;
320
320
}
321
321
322
- /**
323
- * Get salt hash
324
- *
325
- * @param string $salt
326
- * @return string
327
- */
328
- private function getSaltHash ($ salt )
329
- {
330
- return base64_encode (str_pad (substr ($ salt , 0 , 16 ), 16 , '1 ' ));
331
- }
332
-
333
322
/**
334
323
* Build combined key from low-order word and high-order word
335
324
*
@@ -372,7 +361,7 @@ private function buildCombinedKey($byteChars)
372
361
}
373
362
374
363
/**
375
- * Simulate behaviour of int32
364
+ * Simulate behaviour of (signed) int32
376
365
*
377
366
* @param int $value
378
367
* @return int
0 commit comments