-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathPaddingTrait.php
46 lines (41 loc) · 1.03 KB
/
PaddingTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
namespace PubNub\Crypto;
trait PaddingTrait
{
/**
* Pad $text to multiple of $blockSize lenght using PKCS5Padding schema
*
* @param string $text
* @param int $blockSize
* @return string
*/
public function pad(string $text, int $blockSize)
{
$pad = $blockSize - (strlen($text) % $blockSize);
return $text . str_repeat(chr($pad), $pad);
}
/**
* Remove padding from $text using PKCS5Padding schema
*
* @param string $text
* @param int $blockSize
* @return string
*/
public function depad($data, $blockSize)
{
$length = strlen($data);
if ($length == 0) {
return $data;
}
$padLength = substr($data, -1);
if (ord($padLength) <= $blockSize) {
for ($i = $length - 2; $i > 0; $i--) {
if (ord($data [$i] != $padLength)) {
break;
}
}
return substr($data, 0, $i + 1);
}
return $data;
}
}