PHP sha1() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, sha1() calculates the SHA-1 hash of a string.

Pass the string to hash. The optional second argument returns raw binary instead of hex text.

Syntax

Call sha1() with the values you need. Start with this line.

PHP
$hash = sha1($text);
CallWhat it does
sha1("hello")Returns a 40-character hex string.
sha1("hello", true)Returns 20 bytes of raw binary data.

SHA-1 is no longer safe for passwords or security checks. Use password_hash() for passwords and hash("sha256", ...) for stronger checksums.

Library declarations
PHP
sha1(string $string, bool $binary = false): string

Parameters

ParameterDescription
stringRequired. The input string to hash.
binaryOptional. If true, returns raw 20-byte binary. Default is false (40-character hex).

Return Value

Return ValueDescription
stringThe SHA-1 hash as a 40-character hex string, or 20 raw bytes when binary is true.

PHP Examples

Example 1 — Hash a Simple String

Pass a string and get back a 40-character hex hash.

PHP
<?php
$text = "hello";
$hash = sha1($text);

echo $hash, PHP_EOL;
echo strlen($hash), PHP_EOL;

How It Works

  1. Pick a string. Here it is "hello".
  2. Call sha1(). It returns a hex string.
  3. Print the hash. The same input always gives the same hash.
  4. Check the length. A hex SHA-1 hash is always 40 characters.

Example 2 — Empty String and Binary Output

Even an empty string has a hash. Set the second argument to true for raw bytes.

PHP
<?php
echo sha1(""), PHP_EOL;
echo strlen(sha1("hello", true)), PHP_EOL;
echo bin2hex(sha1("hello", true)) === sha1("hello") ? "same" : "different", PHP_EOL;

How It Works

  1. Hash "". You still get a valid 40-character hash.
  2. Ask for binary. true returns 20 raw bytes.
  3. Convert back with bin2hex(). Raw bytes become the usual hex text.
  4. Compare. Both forms describe the same hash.

Example 3 — Compare with hash()

The hash() function can produce the same result using the algorithm name.

PHP
<?php
$a = sha1("abc");
$b = hash("sha1", "abc");

echo $a, PHP_EOL;
echo $a === $b ? "Match" : "No match", PHP_EOL;

How It Works

  1. Use sha1(). A short call for one algorithm.
  2. Use hash(). Pass "sha1" as the algorithm name.
  3. Print one hash. It is the well-known SHA-1 of "abc".
  4. Compare both. They are identical, so you can switch to hash() later.

Frequently Asked Questions

sha1() calculates the SHA-1 hash of a string and returns it as a 40-character hexadecimal string. The same input always gives the same output.
sha1(string $string, bool $binary = false): string. The first argument is the text to hash. The optional second argument returns raw binary when set to true.
No. SHA-1 is fast and has known collision weaknesses. Use password_hash() to store passwords and password_verify() to check them.
A hex result is always 40 characters. With the binary option set to true, you get 20 raw bytes.
Use sha1_file() with the file path. It reads the file for you and returns the same kind of 40-character hash.

Next: sha1_file()

sha1_file() calculates the SHA-1 hash of a file's contents.

sha1_file() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful