cryptal  latest
Cryptography Abstraction Layer
Iso10126.php
1 <?php
2 
4 
6 
15 class Iso10126 implements PaddingInterface
16 {
23  protected static function getRandomBytes()
24  {
25  // We do not need true randomness here, so we just
26  // use uniqid() as it does not require any additional
27  // extension. The same reasoning goes for md5().
28  return md5(uniqid("", true), true);
29  }
30 
31  public function getPaddingData($blockSize, $expectedSize)
32  {
33  $padding = '';
34  for ($i = $expectedSize; $i > 1; $i -= 16) {
35  $padding .= static::getRandomBytes();
36  }
37  return ((string) substr($padding, 0, $expectedSize - 1)) . chr($expectedSize);
38  }
39 
40  public function getPaddingSize($paddedData, $blockSize)
41  {
42  $len = strlen($paddedData);
43  if (!$len) {
44  throw new \Exception('Invalid data');
45  }
46  return ord($paddedData[$len - 1]);
47  }
48 }
getPaddingSize($paddedData, $blockSize)
Definition: Iso10126.php:40
getPaddingData($blockSize, $expectedSize)
Definition: Iso10126.php:31