PHP code example of effectra / security

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

    

effectra / security example snippets


use Effectra\Security\Hash;

$data = 'Hello, World!';
$key = 'secret-key';

$hash = Hash::set($data, $key);
echo "Hashed value: " . $hash;

use Effectra\Security\Hash;

$password = 'password123';

$hashedPassword = Hash::setPassword($password);
echo "Hashed password: " . $hashedPassword;

$isPasswordValid = Hash::verifyPassword($password, $hashedPassword);
if ($isPasswordValid) {
    echo "Password is valid.";
} else {
    echo "Password is invalid.";
}

use Effectra\Security\Csrf;
use Effectra\Session\Session; // Replace with your own session implementation

// Initialize the CSRF class with a session instance
$session = new Session();
$csrf = new Csrf($session);

// Generate and insert a CSRF token in your HTML form
$html = '<form>';
$html .= $csrf->insertHiddenToken();
$html .= '<input type="submit" value="Submit">';
$html .= '</form>';

echo $html;

// Validate the CSRF token on form submission
if ($csrf->validate()) {
    echo "CSRF token is valid.";
} else {
    echo "CSRF token is invalid.";
}

use Effectra\Security\Token;

$data = ['user_id' => 123];

$config = (object) [
    'issued_at' => time(),
    'expirationTime' => time() + 3600, // Expiration time 1 hour from now
    'issuer' => 'example.com',
    'key' => 'your-secret-key'
];

$token = new Token();
$encodedToken = $token->set($data, $config);
echo "Encoded token: " . $encodedToken;

$decodedToken = $token->get($encodedToken, $config);
echo "Decoded token: ";
print_r($decodedToken);