PHP code example of sham3r / cipher-bsk

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

    

sham3r / cipher-bsk example snippets



ipherBsk\CipherBsk;

$message = "Hajee Mohammad Danesh Science and Technology University";
$key = "Hstu@5200";

$cipherBsk = new CipherBsk();

$encryptedMessage = $cipherBsk->encrypt($message, $key);
echo "Encrypted Message: $encryptedMessage\n";

$decryptedMessage = $cipherBsk->decrypt($encryptedMessage, $key);
echo "Decrypted Message: $decryptedMessage\n";

// Example output:
// Encrypted Message: 7B4D5249784F285E6433232166594C6A5A5A3322532A7C4E4F72697D2B4C52515F7400753644674061576C254E47724635612920714C6A21
// Decrypted Message: Hajee Mohammad Danesh Science and Technology University


ipherBsk\CipherBsk;

// Set environment variable for key (REQUIRED)
putenv('CIPHER_BSK_KEY=Hstu@5200');
// OR use APP_KEY
putenv('APP_KEY=your-application-key');

$cipherBsk = new CipherBsk();

// Hash a password with dual-layer encryption
$password = "mysecretpassword";
$hashedPassword = $cipherBsk->secureHash($password);
echo "Hashed Password: $hashedPassword\n";

// Verify password
$isValid = $cipherBsk->verifyHash($password, $hashedPassword);
echo "Password Valid: " . ($isValid ? "Yes" : "No") . "\n";

// Wrong password verification
$isInvalid = $cipherBsk->verifyHash("wrongpassword", $hashedPassword);
echo "Wrong Password Valid: " . ($isInvalid ? "Yes" : "No") . "\n";


use CipherBsk;

// Basic encryption/decryption
$message = "Hello, World!";
$key = "Hstu@5200";

$encrypted = CipherBsk::encrypt($message, $key);
$decrypted = CipherBsk::decrypt($encrypted, $key);

// Secure password hashing (uses your Laravel config)
$hash = CipherBsk::secureHash('Hstu@5200');
dd(CipherBsk::verifyHash('Hstu@5200', $hash)); // true

$wrongHash = CipherBsk::verifyHash('wrongpassword', $hash);
dd($wrongHash); // false


return [
    'key' => env('CIPHER_BSK_KEY', env('APP_KEY')),
];


// Inject via service container
class UserController extends Controller
{
    public function hashPassword(Request $request, CipherBsk $cipher)
    {
        $password = $request->input('password');
        $hash = $cipher->secureHash($password);
        
        // Store $hash in database
        User::create(['password' => $hash]);
        
        return response()->json(['success' => true]);
    }
    
    public function verifyPassword(Request $request, CipherBsk $cipher)
    {
        $user = User::find($request->user_id);
        $inputPassword = $request->input('password');
        
        $isValid = $cipher->verifyHash($inputPassword, $user->password);
        
        return response()->json(['valid' => $isValid]);
    }
}

// This still works exactly the same
$cipher = new CipherBsk();
$encrypted = $cipher->encrypt($message, $key);
$decrypted = $cipher->decrypt($encrypted, $key);

// Original features
$cipher = new CipherBsk();
$encrypted = $cipher->encrypt($message, $key);

// New secure hashing (hash);
bash
php artisan vendor:publish --tag=config
bash
# Test basic encryption/decryption
vendor/bin/phpunit tests/CipherBskTest.php --testdox

# Test secure hashing features
vendor/bin/phpunit tests/SecureHashTest.php --testdox

# Test key generation
vendor/bin/phpunit tests/KeyGeneratorTest.php --testdox