PHP code example of yiisoft / security

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

    

yiisoft / security example snippets


$randomString = Random::string(42);

$hash = (new PasswordHasher())->hash($password);

// save hash to database or another storage
saveHash($hash); 

// obtain hash from database or another storage
$hash = getHash();

$result = (new PasswordHasher())->validate($password, $hash); 

$encryptedData = (new Crypt())->encryptByPassword($data, $password);

// save data to database or another storage
saveData($encryptedData);

// obtain encrypted data from database or another storage
$encryptedData = getEncryptedData();

$data = (new Crypt())->decryptByPassword($encryptedData, $password);

$encryptedData = (new Crypt())->encryptByKey($data, $key);

// save data to database or another storage
saveData($encryptedData);

// obtain encrypted data from database or another storage
$encryptedData = getEncryptedData();

$data = (new Crypt())->decryptByKey($encryptedData, $key);

$signedMessage = (new Mac())->sign($message, $key);

sendMessage($signedMessage);

$signedMessage = receiveMessage($signedMessage);

try {
    $message = (new Mac())->getMessage($signedMessage, $key);
} catch (\Yiisoft\Security\DataIsTamperedException $e) {
    // data is tampered
}

$maskedToken = \Yiisoft\Security\TokenMask::apply($token);

$token = \Yiisoft\Security\TokenMask::remove($maskedToken);

hash_equals($expected, $actual);