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