Skip to content

Instantly share code, notes, and snippets.

@tomatolog
Created August 18, 2025 07:23
Show Gist options
  • Select an option

  • Save tomatolog/4ed54f5e86018d7853dcea5ef10d4f0a to your computer and use it in GitHub Desktop.

Select an option

Save tomatolog/4ed54f5e86018d7853dcea5ef10d4f0a to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
/**
* PHP Command-Line Tool to calculate SHA-256 hash.
* This script accepts a hexadecimal salt and a plaintext password as arguments.
* It then calculates the SHA-256 hash of the binary representation of the salt
* concatenated with the raw password string.
*
* Usage: php script_name.php <salt_hex_string> <password_string>
* Example: php generate_hash.php "509f4b3c50d7b0df729d299bc6f8e9ef9066971f" "test2"
*/
// Check if the correct number of arguments are provided.
// The script name itself is the first argument, so we need 3 total.
if ($argc !== 3) {
// Print a usage message if the arguments are incorrect.
echo "Usage: php " . basename(__FILE__) . " <salt_hex_string> <password_string>\n";
echo "Example: php " . basename(__FILE__) . " \"509f4b3c...\" \"test2\"\n";
exit(1);
}
// Get the hexadecimal salt string from the first argument.
$salt_hex = $argv[1];
// Get the plaintext password string from the second argument.
$password = $argv[2];
// Convert the hexadecimal salt string to its binary representation.
// This is the critical step to match the behavior of your C++ code.
$salt_bin = hex2bin($salt_hex);
// Concatenate the binary salt and the raw password string.
$data_to_hash = $salt_bin . $password;
// Calculate the SHA-256 hash of the combined data.
// The 'hash' function correctly handles binary input.
$hash = hash('sha256', $data_to_hash);
// Print the resulting hash to the console.
echo $hash . "\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment