sha1() Function in PHP 8.2, PHP 8.3 & PHP 8.4


The `sha1()` function in PHP generates a SHA-1 hash of a string. While SHA-1 is no longer considered secure for cryptographic purposes (it's vulnerable to collision attacks), it's still available in PHP 8.2 for legacy compatibility and non-security uses.PHP 8. PHP 8.1, PHP 8.2, PHP 8.3 and PHP 8.4.Basic Example
<?php$string = "Hello, World!";$hash = sha1($string);echo "Original string: " . $string . "\n";echo "SHA-1 hash: " . $hash;?>
Output:
Original string: Hello, World!SHA-1 hash: 0a0a9f2a6772942557ab5355d76af442f8f65e01Security Note
For password hashing, you should use `password_hash()` instead:
<?php$password = "secret123";$secure_hash = password_hash($password, PASSWORD_DEFAULT);echo "Secure hash: " . $secure_hash;?>
File Hashing ExampleYou can also hash file contents with `sha1_file()`:
<?php$filename = "example.txt";$file_hash = sha1_file($filename)echo "SHA-1 hash of " . $filename . ": " . $file_hash;?>
Remember that SHA-1 should not be used for security-sensitive applications in modern PHP development. Consider using SHA-256 or SHA-3 algorithms via `hash()` function instead.