You are here

function lockr_migrate_keys_form in Lockr 7.2

Same name and namespace in other branches
  1. 7.3 lockr.forms.inc \lockr_migrate_keys_form()
1 string reference to 'lockr_migrate_keys_form'
lockr_menu in ./lockr.module
Implements hook_menu().

File

./lockr.forms.inc, line 3

Code

function lockr_migrate_keys_form($form, &$form_state) {
  $migrate_key = random_bytes(32);
  $key_data = hash('sha512', $migrate_key, TRUE);
  $enc_key = substr($key_data, 0, 32);
  $hmac_key = substr($key_data, 32);
  $to_migrate = [];
  $backups = [];
  $keys = key_get_keys_by_provider('lockr');
  foreach ($keys as $key) {
    if (strpos($key['key_provider_settings']['encoded'], 'rijndael-256$cbc$') === 0) {
      $to_migrate[] = $key['id'];
      if (strpos($key['key_type'], 'encryption') !== FALSE) {
        $value = key_get_key_value($key);
        $ciphertext = lockr_migrate_encrypt($value, $enc_key, $hmac_key);
        $ciphertext = base64_encode($ciphertext);
        $backups[] = "{$key['id']}:{$ciphertext}";
      }
    }
  }
  if (!$to_migrate) {
    drupal_goto('admin/config/system/lockr');
  }
  if ($backups) {
    $form['backups'] = [
      'header' => [
        '#theme' => 'html_tag',
        '#tag' => 'h2',
        '#value' => t('Backup Values'),
      ],
      'description' => [
        '#theme' => 'html_tag',
        '#tag' => 'p',
        '#value' => t("While we migrate your values to the latest encryption libraries, we want to make sure in the unlikely event that your system crashes no data is lost. So keep these are encrypted values for you to keep during this process. We'll throw away the key to them once everything is confirmed to be ok. If something does go wrong, please contact our support immediately and we'll assist in getting you migrated without data loss."),
      ],
      'content' => [
        '#theme' => 'html_tag',
        '#tag' => 'pre',
        '#value' => implode("\n", $backups),
      ],
    ];
  }
  $form_state['migrate_key'] = $migrate_key;
  $form_state['to_migrate'] = $to_migrate;
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => t('Migrate keys'),
  ];
  return $form;
}