Back to AbstractAES class

Method resizeKey

public null|string
resizeKey
(mixed $key, mixed $size)
Trims or zero-pads a key / IV
Parameters
  • string $key The key or IV to treat
  • int $size The block size of the currently used algorithm
Returns
  • null|string Null if $key is null, treated string of $size byte length otherwise
Class: AbstractAES
Project: Joomla

Method resizeKey - Source code

/**
 * Trims or zero-pads a key / IV
 *
 * @param   string $key  The key or IV to treat
 * @param   int    $size The block size of the currently used algorithm
 *
 * @return  null|string  Null if $key is null, treated string of $size byte length otherwise
 */
public function resizeKey($key, $size)
{
    if (empty($key)) {
        return null;
    }
    $keyLength = \strlen($key);
    if (\function_exists('mb_strlen')) {
        $keyLength = mb_strlen($key, 'ASCII');
    }
    if ($keyLength == $size) {
        return $key;
    }
    if ($keyLength > $size) {
        if (\function_exists('mb_substr')) {
            return mb_substr($key, 0, $size, 'ASCII');
        }
        return substr($key, 0, $size);
    }
    return $key . str_repeat("\x00", $size - $keyLength);
}