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


$crypt = new Pop\Crypt\Crypt();
$crypt->setSalt($crypt->generateRandomString(32));
$hash  = $crypt->create('my-password');

// Outputs 'Lx8nDfxK6TbQg'
echo $hash;

if ($crypt->verify('bad-password', $hash)) {} // Returns false
if ($crypt->verify('my-password', $hash))  {} // Returns true

$bcrypt = new Pop\Crypt\Bcrypt();
$bcrypt->setCost('12')
       ->setPrefix('$2y$');

$hash= $bcrypt->create('my-password');

// Outputs '$2y$12$OHZtOTlrNG1UTE5wUmpGQO2WJPFSzkhDErx2UHOvFEwU8/NosVYDe'
echo $hash;

if ($bcrypt->verify('bad-password', $hash)) {} // Returns false
if ($bcrypt->verify('my-password', $hash))  {} // Returns true

$md5  = new Pop\Crypt\Md5();
$hash = $md5->create('my-password');

// Outputs '$1$TlBKWGtw$zdivNfpOUPWwJ3a1cUM1E/'
echo $hash;

if ($md5->verify('bad-password', $hash)) {} // Returns false
if ($md5->verify('my-password', $hash))  {} // Returns true

$sha  = new Pop\Crypt\Sha();
$sha->setBits512()
    ->setRounds(10000);

$hash = $sha->create('my-password');

// Outputs a big long hash '$6$rounds=10000$QlZOMkJZQVNBeEN...'
echo $hash;

if ($sha->verify('bad-password', $hash)) {} // Returns false
if ($sha->verify('my-password', $hash))  {} // Returns true

$mcrypt = new Pop\Crypt\Mcrypt();

$mcrypt->setCipher(MCRYPT_RIJNDAEL_256)
       ->setMode(MCRYPT_MODE_CBC)
       ->setSource(MCRYPT_RAND);

$hash = $mcrypt->create('my-password');

// Outputs a big long hash 'NGGe/i6XPKFGY4cvxZrSb4zBj1J0OYh...'
echo $hash;

if ($mcrypt->verify('bad-password', $hash)) {} // Returns false
if ($mcrypt->verify('my-password', $hash))  {} // Returns true

$decrypted = $mcrypt->decrypt($hash);

// Outputs 'my-password'
echo $decrypted;