View source
<?php
use Drupal\Core\Site\Settings;
function encryption_requirements($phase) {
$requirements = [];
if ($phase == 'runtime') {
$encryption = Drupal::service('encryption');
$encryption_key_raw = Settings::get('encryption_key', FALSE);
$key = base64_decode($encryption_key_raw);
$key_length = strlen($key);
$requirements['encryption'] = [
'title' => t('Encryption'),
];
if (empty($encryption_key_raw) || $key_length !== 32) {
$requirements['encryption'] += [
'severity' => REQUIREMENT_ERROR,
'description' => t('The encryption key must be a base 64 encoded 256 bit (32 byte) value.'),
'value' => $encryption_key_raw === FALSE ? t('The encryption key was not found.') : t('An encryption key of :length bits was found but a 256 bit key was expected.', [
':length' => $key_length * 8,
]),
];
}
else {
$requirements['encryption'] += [
'severity' => REQUIREMENT_OK,
'value' => t('The encryption key is OK.'),
];
}
$state = Drupal::state();
$result = $state
->get('encryption.test_value', FALSE);
$test_text = 'simple test value';
$reset = isset($_GET['encryption_reset_test_string']) && $_GET['encryption_reset_test_string'] === '1';
if ($result === FALSE || $reset) {
$state
->set('encryption.test_value', $encryption
->encrypt($test_text));
if ($reset) {
$requirements['encryption']['severity'] = REQUIREMENT_WARNING;
$requirements['encryption']['value'] = t('The encryption test value has been reset <a href="javascript:history.back()">(go back)</a>.');
}
}
else {
$decrypted_result = $encryption
->decrypt($result);
if ($test_text != $decrypted_result && $requirements['encryption']['severity'] == REQUIREMENT_OK) {
$requirements['encryption']['severity'] = REQUIREMENT_WARNING;
$requirements['encryption']['value'] = t('Unable to properly decrypt the test value. Possible due to a change of the encryption key. <a href="?encryption_reset_test_string=1">Reset</a>');
$requirements['encryption']['description'] = t('Attempted to decrypt a known encrypted value. The test value is encrypted when the encryption module is installed.');
}
}
}
return $requirements;
}