Commit | Line | Data |
---|---|---|
14d4f286 S |
1 | <?php |
2 | /** | |
3 | * A packing mode implementation for PKCS7 padding | |
4 | * | |
5 | * PHP version 5.3 | |
6 | * | |
7 | * @see http://tools.ietf.org/html/rfc2315 | |
8 | * @category PHPCryptLib | |
9 | * @package Encryption | |
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@@ | |
15 | */ | |
16 | ||
17 | namespace CryptLib\Encryption\PackingMode; | |
18 | ||
19 | /** | |
20 | * A packing mode implementation for PKCS7 padding | |
21 | * | |
22 | * PHP version 5.3 | |
23 | * | |
24 | * @see http://tools.ietf.org/html/rfc2315 | |
25 | * @category PHPCryptLib | |
26 | * @package Encryption | |
27 | * @subpackage PackingMode | |
28 | * @author Anthony Ferrara <ircmaxell@ircmaxell.com> | |
29 | */ | |
30 | class PKCS7 implements \CryptLib\Encryption\PackingMode { | |
31 | ||
32 | /** | |
33 | * Pad the string to the specified size | |
34 | * | |
35 | * @param string $string The string to pad | |
36 | * @param int $blockSize The size to pad to | |
37 | * | |
38 | * @return string The padded string | |
39 | */ | |
40 | public function pad($string, $blockSize = 32) { | |
41 | $pad = $blockSize - (strlen($string) % $blockSize); | |
42 | return $string . str_repeat(chr($pad), $pad); | |
43 | } | |
44 | ||
45 | /** | |
46 | * Strip the padding from the supplied string | |
47 | * | |
48 | * @param string $string The string to trim | |
49 | * | |
50 | * @return string The unpadded string | |
51 | */ | |
52 | public function strip($string) { | |
53 | $end = substr($string, -1); | |
54 | $last = ord($end); | |
55 | $len = strlen($string) - $last; | |
56 | if (substr($string, $len) == str_repeat($end, $last)) { | |
57 | return substr($string, 0, $len); | |
58 | } | |
59 | return false; | |
60 | } | |
61 | ||
62 | } |