|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2007-2016 Abstrium <contact (at) pydio.com> |
| 4 | + * This file is part of Pydio. |
| 5 | + * |
| 6 | + * Pydio is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * Pydio is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with Pydio. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + * |
| 19 | + * The latest code can be found at <https://pydio.com/>. |
| 20 | + */ |
| 21 | + |
| 22 | +namespace Pydio\Core\Utils\Crypto; |
| 23 | + |
| 24 | +defined('AJXP_EXEC') or die('Access not allowed'); |
| 25 | + |
| 26 | +use \phpseclib\Crypt\Rijndael; |
| 27 | + |
| 28 | +/** |
| 29 | + * Class ZeroPaddingRijndael |
| 30 | + * @package Pydio\Core\Utils\Crypto |
| 31 | + */ |
| 32 | +class ZeroPaddingRijndael extends Rijndael { |
| 33 | + /** |
| 34 | + * Pads a string |
| 35 | + * |
| 36 | + * Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize. |
| 37 | + * $this->block_size - (strlen($text) % $this->block_size) bytes are added, each of which is equal to |
| 38 | + * chr($this->block_size - (strlen($text) % $this->block_size) |
| 39 | + * |
| 40 | + * If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless |
| 41 | + * and padding will, hence forth, be enabled. |
| 42 | + * |
| 43 | + * @see self::_unpad() |
| 44 | + * @param string $text |
| 45 | + * @throws \LengthException if padding is disabled and the plaintext's length is not a multiple of the block size |
| 46 | + * @access private |
| 47 | + * @return string |
| 48 | + */ |
| 49 | + function _pad($text) |
| 50 | + { |
| 51 | + $length = strlen($text); |
| 52 | + |
| 53 | + if (!$this->padding) { |
| 54 | + if ($length % $this->block_size == 0) { |
| 55 | + return $text; |
| 56 | + } else { |
| 57 | + throw new \LengthException("The plaintext's length ($length) is not a multiple of the block size ({$this->block_size}). Try enabling padding."); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + $pad = $this->block_size - ($length % $this->block_size); |
| 62 | + return str_pad($text, $length + $pad, "\0"); |
| 63 | + } |
| 64 | + /** |
| 65 | + * Unpads a string. |
| 66 | + * |
| 67 | + * If padding is enabled and the reported padding length is invalid the encryption key will be assumed to be wrong |
| 68 | + * and false will be returned. |
| 69 | + * |
| 70 | + * @see self::_pad() |
| 71 | + * @param string $text |
| 72 | + * @throws \LengthException if the ciphertext's length is not a multiple of the block size |
| 73 | + * @access private |
| 74 | + * @return string |
| 75 | + */ |
| 76 | + function _unpad($text) { |
| 77 | + $trimed = trim($text, "\0"); |
| 78 | + while($text === $trimed){ |
| 79 | + $trimed = trim($text, "\0"); |
| 80 | + } |
| 81 | + return $trimed; |
| 82 | + } |
| 83 | +} |
0 commit comments