PasswordHasher.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Shared;
  3. class PasswordHasher
  4. {
  5. /**
  6. * Create a password hash from a given string.
  7. *
  8. * This method is based on the algorithm provided by
  9. * Daniel Rentz of OpenOffice and the PEAR package
  10. * Spreadsheet_Excel_Writer by Xavier Noguer <xnoguer@rezebra.com>.
  11. *
  12. * @param string $pPassword Password to hash
  13. *
  14. * @return string Hashed password
  15. */
  16. public static function hashPassword($pPassword)
  17. {
  18. $password = 0x0000;
  19. $charPos = 1; // char position
  20. // split the plain text password in its component characters
  21. $chars = preg_split('//', $pPassword, -1, PREG_SPLIT_NO_EMPTY);
  22. foreach ($chars as $char) {
  23. $value = ord($char) << $charPos++; // shifted ASCII value
  24. $rotated_bits = $value >> 15; // rotated bits beyond bit 15
  25. $value &= 0x7fff; // first 15 bits
  26. $password ^= ($value | $rotated_bits);
  27. }
  28. $password ^= strlen($pPassword);
  29. $password ^= 0xCE4B;
  30. return strtoupper(dechex($password));
  31. }
  32. }