You are here

public static function AES::make_key in AES encryption 8.2

Generate a random key, containing uppercase, lowercase and digits.

Return value

string encryption key.

1 call to AES::make_key()
aes_install in ./aes.install
Implements hook_install().

File

src/AES.php, line 88

Class

AES

Namespace

Drupal\aes

Code

public static function make_key() {
  $keylen = 32;
  $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  while (TRUE) {
    $key = '';
    while (strlen($key) < $keylen) {
      $key .= substr($chars, rand(0, strlen($chars)), 1);
    }

    // is there at least one lowercase letter?
    if (!preg_match('/.*[a-z].*/', $key)) {
      continue;
    }

    // is there at least one uppercase letter?
    if (!preg_match('/.*[A-Z].*/', $key)) {
      continue;
    }

    // is there at least one numeric?
    if (!preg_match('/.*[0-9].*/', $key)) {
      continue;
    }
    break;
  }
  \Drupal::logger('aes')
    ->notice('Generated new AES key: ' . substr($key, 0, 4) . str_repeat('*', $keylen - 8) . substr($key, $keylen - 4, 4));
  return $key;
}