sha1() function in PHP


The sha1() function in PHP is used to calculate the sha1 hash of a string. Let us first see what is SHA-1 −

The US Secure Hash Algorithm 1 − "SHA-1 produces a 160-bit output called a message digest. The message digest can then, for example, be input to a signature algorithm which generates or verifies the signature for the message. Signing the message digest rather than the message often improves the efficiency of the process because the message digest is usually much smaller in size than the message. The same hash algorithm must be used by the verifier of a digital signature as was used by the creator of the digital signature."

-- Ref − RFC 3174

Syntax

sha1(str, raw)

Parameters

  • str  − Specify the string. Required.

  • raw  − A boolean value that specifies hex or binary output format. Optional.

  • TRUE  − Raw 20 character binary format

  • FALSE  − 40 character hex number

Return

The sha1() function returns the calculated SHA-1 hash on success, or FALSE on failure.

Example

The following is an example −

 Live Demo

<?php
$s = "Welcome";
echo sha1($s);
?>

Output

ca4f9dcf204e2037bfe5884867bead98bd9cbaf8

Example

The following is an example −

 Live Demo

<?php
$s = "Welcome!";
echo sha1($s);
if (sha1($s) == "ca4f9dcf204e2037bfe5884867bead98bd9cbaf8") {
   echo "<br>Hello Welcome!";
   exit;
}
?>

Output

e52e5e6cd50ef4de30d8a4fafbbfab41180cc200

Updated on: 24-Jun-2020

130 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements