PHP code example of daggerx / password-hasher

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

    

daggerx / password-hasher example snippets




ggerX\DaggerX;



$hash = DaggerX::hashPassword("mySecurePassword", "MySecretKey");
echo $hash;



$isValid = DaggerX::verifyPassword("mySecurePassword", $hash, "MySecretKey");
if ($isValid) {
    echo "Password is correct!";
} else {
    echo "Invalid password!";
}



$encrypted = DaggerX::encryptMessage("Hello, this is private!", "MySecretKey", "aes-256-gcm", "user_id:12345");
echo $encrypted;



$encryptedCBC = DaggerX::encryptMessage("Hello, this is private!", "MySecretKey", "aes-256-cbc");
echo $encryptedCBC;



$decrypted = DaggerX::decryptMessage($encrypted, "MySecretKey", "user_id:12345");
echo $decrypted; // Output: Hello, this is private!

$decryptedCBC = DaggerX::decryptMessage($encryptedCBC, "MySecretKey");
echo $decryptedCBC; // Output: Hello, this is private!



$newHash = DaggerX::rotateHashKey("mySecurePassword", $hash, "MySecretKey", "NewSecretKey");
echo $newHash;

// Verify with the new key
$isValid = DaggerX::verifyPassword("mySecurePassword", $newHash, "NewSecretKey");
echo $isValid ? "Password verified with new key!" : "Verification failed!";



$newEncrypted = DaggerX::rotateEncryptionKey($encrypted, "MySecretKey", "NewSecretKey", "user_id:12345", "aes-256-gcm");
echo $newEncrypted;

// Decrypt with the new key
$decrypted = DaggerX::decryptMessage($newEncrypted, "NewSecretKey", "user_id:12345");
echo $decrypted; // Output: Hello, this is private!



$hash = DaggerX::hashPassword("mySecurePassword", "MySecretKey", [
    'memory_cost' => 32768, // 32 MB
    'time_cost' => 2,
    'threads' => 2
]);
echo $hash;



ggerX\DaggerX;

// Disable session usage for consistency
DaggerX::setSessionUsage(false);

// Developer key (store securely in production)
$devKey = "MySecretKey1234567890";

// Database connection
$conn = new mysqli("localhost", "root", "08032494987", "test22");
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$success = $error = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $password = trim($_POST['password']);

    // Basic validation
    if (empty($name) || empty($email) || empty($password)) {
        $error = "All fields are error = "Failed to hash password: " . $e->getMessage();
            $hashedPassword = null;
        }

        if (!$error && $encryptedName && $hashedPassword) {
            // Insert into database
            $stmt = $conn->prepare("INSERT INTO users (name, email, password) VALUES (?, ?, ?)");
            $stmt->bind_param("sss", $encryptedName, $email, $hashedPassword);
            if ($stmt->execute()) {
                $success = "Registration successful! <a href='login.php'>Login here</a>.";
            } else {
                $error = "Registration failed: " . $conn->error;
            }
            $stmt->close();
        }
    }
}


session_start();
// Disable session usage to avoid session ID mismatches
DaggerX::setSessionUsage(false);

// Define the developer key (must be the same as in register.php)
$devKey = "MySecretKey1234567890";

// Database connection
$conn = new mysqli("localhost", "root", "08032494987", "test22");
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$success = $error = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $email = trim($_POST['email']);
    $password = trim($_POST['password']);

    // Basic validation
    if (empty($email) || empty($password)) {
        $error = "All fields are  $devKey)) {
                    // Decrypt the name
                    $decryptedName = DaggerX::decryptMessage($encryptedName, $devKey);
                    $_SESSION['user_name'] = $decryptedName;
                    $success = "Login successful! Welcome, " . htmlspecialchars($decryptedName) . "!";
                } else {
                    $error = "Invalid email or password.";
                }
            } catch (Exception $e) {
                $error = "Login failed: " . $e->getMessage();
            }
        } else {
            $error = "Invalid email or password.";
        }
        $stmt->close();
    }
}


session_start();
session_destroy();
header("Location: login.php");
exit;