3 * A packing mode implementation for PKCS7 padding
7 * @see http://tools.ietf.org/html/rfc2315
8 * @category PHPCryptLib
10 * @subpackage PackingMode
11 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
12 * @copyright 2011 The Authors
13 * @license http://www.opensource.org/licenses/mit-license.html MIT License
14 * @version Build @@version@@
17 namespace CryptLib\Encryption\PackingMode
;
20 * A packing mode implementation for PKCS7 padding
24 * @see http://tools.ietf.org/html/rfc2315
25 * @category PHPCryptLib
27 * @subpackage PackingMode
28 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
30 class PKCS7
implements \CryptLib\Encryption\PackingMode
{
33 * Pad the string to the specified size
35 * @param string $string The string to pad
36 * @param int $blockSize The size to pad to
38 * @return string The padded string
40 public function pad($string, $blockSize = 32) {
41 $pad = $blockSize - (strlen($string) %
$blockSize);
42 return $string . str_repeat(chr($pad), $pad);
46 * Strip the padding from the supplied string
48 * @param string $string The string to trim
50 * @return string The unpadded string
52 public function strip($string) {
53 $end = substr($string, -1);
55 $len = strlen($string) - $last;
56 if (substr($string, $len) == str_repeat($end, $last)) {
57 return substr($string, 0, $len);