PHP code example of ialopezg / encryption

1. Go to this page and download the library: Download ialopezg/encryption library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ialopezg / encryption example snippets


/**
 * 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

$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

$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

$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

$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

$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>';