PHP code example of popphp / pop-crypt

1. Go to this page and download the library: Download popphp/pop-crypt 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/ */

    

popphp / pop-crypt example snippets


use Pop\Crypt\Hashing;

$hasher      = Hashing\BcryptHasher::create();
$hashedValue = $hasher->make('password');

if ($hasher->verify('password', $hashedValue)) {
    echo 'You shall pass!' . PHP_EOL;
} else {
    echo 'YOU SHALL NOT PASS!' . PHP_EOL;
}

use Pop\Crypt\Encryption;

$encrypter = new Encryption\Encrypter($mySecureKey, 'aes-256-cbc');
// Returns a base-64 encoded string of the encrypted data
$encryptedData = $encrypter->encrypt('SENSITIVE_DATA');

// Returns the valid, decrypted data
try {
    $decryptedData = $encrypter->decrypt($encryptedData);
// Else, throws an error exception is something is incorrect or invalid
} catch (\Exception $e) {
    echo $e->getMessage(); 
}

use Pop\Crypt\Hashing;

$hasher = Hashing\Hasher::create(PASSWORD_ARGON2ID, [
    'memory_cost' => 131072,
    'time_cost'   => 4,
]);

use Pop\Crypt\Encryption;

$key = Encryption\Encrypter::generateKey($cipher, false);

use Pop\Crypt\Encryption;

$encrypter = Encryption\Encrypter::create('aes-256-gcm');

use Pop\Crypt\Encryption;

// APP_CIPHER_METHOD=aes-256-gcm
// APP_KEY=<base64-encoded key>
// APP_PREVIOUS_KEYS=<base64-encoded key>,<base64-encoded key>
$encrypter = Encryption\Encrypter::load();

use Pop\Crypt\Encryption;

$encrypter = new Encryption\Encrypter($currentKey, 'aes-256-cbc');
$encrypter->setPreviousKeys([$oldKey1, $oldKey2, $oldKey3]);

use Pop\Crypt\Encryption;

$encrypter     = Encryption\SodiumEncrypter::create();
$encryptedData = $encrypter->encrypt('SENSITIVE_DATA');
$decryptedData = $encrypter->decrypt($encryptedData);

use Pop\Crypt\Encryption;

$key       = Encryption\SodiumEncrypter::generateKey(false);
$encrypter = new Encryption\SodiumEncrypter($key, false);
$encrypter->setPreviousKeys([$oldKey1, $oldKey2]);

// APP_KEY=<base64-encoded key>
// APP_PREVIOUS_KEYS=<base64-encoded key>,<base64-encoded key>
$encrypter = Encryption\SodiumEncrypter::load();

use Pop\Crypt\Signature;

$data      = 'The quick brown fox';
$secret    = 'my-secret-key';
$signature = hash_hmac('sha256', $data, $secret, true);

if (Signature\Verifier::hmac($data, $signature, $secret, 'sha256')) {
    echo 'Signature is valid';
} else {
    echo 'Signature is invalid';
}

use Pop\Crypt\Signature;

$data      = 'The quick brown fox';
$publicKey = file_get_contents('/path/to/public.pem');
$signature = file_get_contents('/path/to/signature.bin'); // the raw signature bytes to verify

try {
    if (Signature\Verifier::rsa($data, $signature, $publicKey, 'sha256')) {
        echo 'Signature is valid';
    } else {
        echo 'Signature is invalid';
    }
} catch (\Pop\Crypt\Exception $e) {
    echo 'Error: ' . $e->getMessage();
}

use Pop\Crypt\Signature;

$data      = 'The quick brown fox';
$publicKey = file_get_contents('/path/to/ec-public.pem');
$signature = file_get_contents('/path/to/signature.bin'); // the raw signature bytes to verify

try {
    if (Signature\Verifier::ec($data, $signature, $publicKey, 'sha256')) {
        echo 'Signature is valid';
    } else {
        echo 'Signature is invalid';
    }
} catch (\Pop\Crypt\Exception $e) {
    echo 'Error: ' . $e->getMessage();
}