cryptal  latest
Cryptography Abstraction Layer
NonEmptyZero.php
1 <?php
2 
4 
6 
12 {
13  public function getPaddingData($blockSize, $expectedSize)
14  {
15  return str_repeat("\x00", $expectedSize);
16  }
17 
18  public function getPaddingSize($paddedData, $blockSize)
19  {
20  // We could use strspn(strrev($paddedData), "\x00") instead,
21  // but this would require additional memory allocations,
22  // which is undesirable as $paddedData gets larger.
23  $m = strlen($paddedData) - 1;
24  for ($i = 0; $i <= $blockSize && $i <= $m && "\x00" === $paddedData[$m - $i]; $i++) {
25  // Nothing to do
26  }
27 
28  if (0 === $i) {
29  // This should never happen.
30  throw new \Exception('Invalid data');
31  }
32 
33  return $i;
34  }
35 }
getPaddingData($blockSize, $expectedSize)
getPaddingSize($paddedData, $blockSize)