Permalink
Browse files

added phpseclib keys generator submodule

  • Loading branch information...
talhaparacha committed Jul 8, 2016
1 parent dde7010 commit 770afd1d542417b2e86970d03ef7a7ad35b279d7
@@ -0,0 +1,8 @@
# Pubkey Encrypt PHPSecLib.
Provides a PHPSecLib-based Asymmetric Keys Generator to be used with Pubkey Encrypt.

# Installation.
1) Install composer.
2) Run the following command from the root directory of Drupal 8 installation:
composer require phpseclib/phpseclib:"^2.0.0"
3) Now install this module from the modules installation interface.
@@ -0,0 +1,8 @@
{
"name": "drupal/pubkey_encrypt_phpseclib",
"description": "Provides a PHPSecLib-based Asymmetric Keys Generator to be used with Pubkey Encrypt",
"type": "drupal-module",
"require": {
"phpseclib/phpseclib": "^2.0.0"
}
}
@@ -0,0 +1,7 @@
name: Pubkey Encrypt PHPSecLib
description: Provides a PHPSecLib-based Asymmetric Keys Generator to be used with Pubkey Encrypt.
core: 8.x
type: module
package: Encryption
dependencies:
- pubkey_encrypt
@@ -0,0 +1,27 @@
<?php

/**
* @file
* Install hooks for the pubkey_encrypt_phpseclib module.
*/

/**
* Implements hook_requirements().
*/
function pubkey_encrypt_phpseclib_requirements($phase) {
$requirements = array();

$requirements['pubkey_encrypt_phpseclib'] = array(
'title' => t('Pubkey Encrypt PHPSecLib'),
);

if (class_exists('\phpseclib\Crypt\RSA')) {
$requirements['pubkey_encrypt_phpseclib']['severity'] = REQUIREMENT_OK;
}
else {
$requirements['pubkey_encrypt_phpseclib']['severity'] = REQUIREMENT_ERROR;
$requirements['pubkey_encrypt_phpseclib']['description'] = t('Please install the PHPSecLib library via Composer. See README.txt');
}

return $requirements;
}
@@ -0,0 +1,106 @@
<?php
/**
* @file
* Contains \Drupal\pubkey_encrypt_phpseclib\Plugin\AsymmetricKeysGenerator\PHPSecLib.
*/
namespace Drupal\pubkey_encrypt_phpseclib\Plugin\AsymmetricKeysGenerator;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\pubkey_encrypt\Plugin\AsymmetricKeysGeneratorBase;
use phpseclib\Crypt\RSA;
/**
* Provides an asymmetric keys generator based on PHPSecLib.
*
* @AsymmetricKeysGenerator(
* id = "phpseclib",
* name = @Translation("PHPSecLib"),
* description = @Translation("RSA-based keys generated via PHPSecLib.")
* )
*/
class PHPSecLib extends AsymmetricKeysGeneratorBase implements PluginFormInterface {
/**
* {@inheritdoc}
*/
public function generateAsymmetricKeys() {
$rsa = new RSA();
$keys = $rsa->createKey($this->getConfiguration()['key_size']);
// Return the keys.
return array(
"public_key" => $keys['publickey'],
"private_key" => $keys['privatekey'],
);
}
/**
* {@inheritdoc}
*/
public function encryptWithPublicKey($original_data, $public_key) {
$rsa = new RSA();
$rsa->loadKey($public_key);
$encrypted = $rsa->encrypt($original_data);
return $encrypted;
}
/**
* {@inheritdoc}
*/
public function decryptWithPrivateKey($encrypted_data, $private_key) {
$rsa = new RSA();
$rsa->loadKey($private_key);
$decrypted = $rsa->decrypt($encrypted_data);
return $decrypted;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'key_size' => '1024',
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['key_size'] = array(
'#type' => 'select',
'#title' => t('Key size in bits'),
'#options' => [
'512' => '512',
'1024' => '1024',
'2048' => '2048',
'4096' => '4096',
],
'#default_value' => $this->getConfiguration()['key_size'],
'#required' => TRUE,
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$key_size = $form_state->getValue('key_size');
if ($key_size < 512) {
$form_state->setErrorByName('key_size', 'Key size too small.');
}
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->setConfiguration($form_state->getValues());
}
}

0 comments on commit 770afd1

Please sign in to comment.