/monica/vendor/zendframework/zendframework/library/Zend/Crypt/Symmetric/Padding/Pkcs7.php

https://bitbucket.org/alexandretaz/maniac_divers · PHP · 49 lines · 20 code · 4 blank · 25 comment · 2 complexity · a5ef1016d0bbf4aba5aef856a0f28035 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Crypt\Symmetric\Padding;
  10. /**
  11. * PKCS#7 padding
  12. */
  13. class Pkcs7 implements PaddingInterface
  14. {
  15. /**
  16. * Pad the string to the specified size
  17. *
  18. * @param string $string The string to pad
  19. * @param int $blockSize The size to pad to
  20. *
  21. * @return string The padded string
  22. */
  23. public function pad($string, $blockSize = 32)
  24. {
  25. $pad = $blockSize - (strlen($string) % $blockSize);
  26. return $string . str_repeat(chr($pad), $pad);
  27. }
  28. /**
  29. * Strip the padding from the supplied string
  30. *
  31. * @param string $string The string to trim
  32. *
  33. * @return string The unpadded string
  34. */
  35. public function strip($string)
  36. {
  37. $end = substr($string, -1);
  38. $last = ord($end);
  39. $len = strlen($string) - $last;
  40. if (substr($string, $len) == str_repeat($end, $last)) {
  41. return substr($string, 0, $len);
  42. }
  43. return false;
  44. }
  45. }