cryptal  latest
Cryptography Abstraction Layer
IsoIec7816.php
1 <?php
2 
4 
6 
8 class IsoIec7816 implements PaddingInterface
9 {
10  public function getPaddingData($blockSize, $expectedSize)
11  {
12  return "\x80" . str_repeat("\x00", $expectedSize - 1);
13  }
14 
15  public function getPaddingSize($paddedData, $blockSize)
16  {
17  $len = strlen($paddedData);
18  if (!$len) {
19  throw new \Exception('Invalid data');
20  }
21 
22  for ($i = 1; $i < $blockSize && $i < $len && "\x00" === $paddedData[$len - $i]; $i++) {
23  // Nothing to do
24  }
25 
26  if ($i === $len) {
27  // The entire string was made of NUL bytes
28  throw new \Exception('Invalid data');
29  }
30 
31  if ("\x80" !== $paddedData[$len - $i]) {
32  throw new \Exception('Invalid data');
33  }
34 
35  return $i;
36  }
37 }
getPaddingSize($paddedData, $blockSize)
Definition: IsoIec7816.php:15
getPaddingData($blockSize, $expectedSize)
Definition: IsoIec7816.php:10
Pads a string using the scheme defined in ISO/IEC 7816-4.
Definition: IsoIec7816.php:8