Cryptography Library for PHP
Table of Contents
-
Package manager
- Composer
composer require ialopezg/encryption
- Composer
-
Manually
- Github
git clone https://github.com/ialopezg/encryption
- Github
Library | Description |
---|---|
Password |
Library for password manipulation. |
- PHP v5.6+
- ext-mbstring PHP extension
- ext-openssl PHP extension
- ialopezg/core v0.0.1+
- ialopezg/logger v0.0.4+
Method | Description |
---|---|
createKey() |
Creates a random key with the lengh provided. Optional the result could be returned with capital letters. |
getOption() |
Gets an option value. If value does not exists will return the default value. |
hash() |
Encrypts a simple text password into a ciphered password. |
setOption() |
Sets an option value. |
verify() |
Encrypts a plain text into cipher text. |
/**
* Creates a random key with the length specified. Optional the result could be returned with capital letters.
*
* @param int $length Output length.
* @param bool $capitalize Whether if the result key will be returned with capital letters.
*
* @throws Exception If it was not possible to gather sufficient entropy.
* @return string A random key.
*/
public static function createKey($length, $capitalize = false): string
Examples
$key = \ialopezg\Libraries\Encryption\Password::createKey(8, true);
// Display the encryption key string
echo "Encryption key: <strong>{$key}</strong><br>";
/**
* Gets an option value. If value does not exists will return the default value.
*
* @param string $key Option key name.
* @param null $default Option default value.
*
* @return mixed
*/
public function getOption($key, $default = null): string
Examples
$encrypter->setOption('key', Password::createKey(8, true);
// Display the encryption key string
echo "Encryption key: <strong>{$encrypter->getOption('key')}</strong><br>";
/**
* Encrypts a simple text password into a ciphered password.
*
* @param string $password Plain text password.
*
* @return string Ciphered password.
* @throws Exception If data provided is not a string.
*/
public function hash($password): string
Examples
$encrypter = new \ialopezg\Libraries\Encryption\Password([
'cipher' => 'AES-128-CTR',
'digest' => 'SHA512',
'options' => OPENSSL_CIPHER_RC2_40,
'key' => 'YOUR_SECRET_KEY'
]);
$encrypted_password = $encrypter->encrypt($password);
// Display the encrypted string
echo "Encrypted password: <strong>{$encrypted_password}</strong><br>";
/**
* Sets an option value.
*
* @param string $key Option key name.
* @param mixed $value Option value.
*
* @return void
*/
public function setOption($key, $value): string
Examples
$encrypter = new \ialopezg\Libraries\Encryption\Password();
$encrypter->setOption('key', $key);
/**
* Verifies if a hashed password is equal to the plain tex provided.
*
* @param string $password Plain text password.
* @param string $hash Ciphered text
*
* @return bool true If password equals to hash, false otherwise.
* @throws Exception If data provided is not a string.
*/
public function verify($password, $hash): bool
Examples
$encrypter = new \ialopezg\Libraries\Encryption\Password();
$password = 'YOUR_PASSWORD';
$encrypted_password = 'YOUR_ENCRYPTED_PASSWORD';
// Display the encrypted string
echo 'Password are equals: <strong>' . ($encrypter->verify($password, $encrypted_password) ? 'true' : 'false') . '</strong>';
For more examples or options, see examples directory. For live examples, run:
### linux bash
./server.sh
or
### windows prompt
server.bat
This project is under the MIT license. For more information see LICENSE.
Copyright (c) Isidro A. López G.