lockr_encryption.inc in Lockr 7.3
File
plugins/key_type/lockr_encryption.inc
View source
<?php
$plugin = [
'label' => t('Lockr Encryption'),
'description' => t('A key type that generates keys via Lockr.'),
'group' => 'encryption',
'key value' => [
'plugin' => 'generate',
],
'default configuration' => 'key_type_lockr_encryption_default_configuration',
'build configuration form' => 'key_type_lockr_encryption_build_configuration_form',
'generate key value' => 'key_type_lockr_encryption_generate_key_value',
'validate key value' => 'key_type_lockr_encryption_validate_key_value',
];
function key_type_lockr_encryption_default_configuration() {
return [
'key_size' => '256',
];
}
function key_type_lockr_encryption_build_configuration_form($form, &$form_state) {
$config = $form_state['storage']['key_config'];
$plugin_config = $config['key_type_settings'] + key_type_lockr_encryption_default_configuration();
$form['key_size'] = [
'#type' => 'select',
'#title' => t('Key size'),
'#description' => t('The size of the key in bits.'),
'#options' => [
'128' => 128,
'192' => 192,
'256' => 256,
],
'#default_value' => $plugin_config['key_size'],
'#required' => TRUE,
];
return $form;
}
function key_type_lockr_encryption_generate_key_value(array $config) {
$bits = isset($config['key_type_settings']['key_size']) ? $config['key_type_settings']['key_size'] : 256;
$client = lockr_client();
return $client
->generateKey((int) $bits);
}
function key_type_lockr_encryption_validate_key_value($form, &$form_state, $key_value) {
if (empty($form_state['values']['key_size'])) {
return;
}
$bytes = $form_state['values']['key_size'] / 8;
if (strlen($key_value) != $bytes) {
form_set_error('key_size', t('The selected key size does not match the actual size of the key.'));
return;
}
}