Current Location: Home> Function Categories> sha1

sha1

Calculate the sha1 hash value of a string
Name:sha1
Category:String
Programming Language:php
One-line Description:Calculate the SHA-1 hash of the string.

Definition and usage

The sha1() function calculates the SHA-1 hash of a string.

The sha1() function uses the American Secure Hash algorithm 1.

Interpretation from RFC 3174 - US Secure Hash Algorithm 1: SHA-1 produces a 160-bit output called message digest. The message digest can be input into a signature algorithm that can generate or verify the message signature. Signing the message digest instead of signing the message can improve process efficiency, because the size of the message digest is usually much smaller than that of the message. The verifier of a digital signature must use the same hashing algorithm as the creator of a digital signature.

Tip: If you want to calculate the SHA-1 hash of the file, please use sha1_file() function.

Example

Example 1

Calculate the SHA-1 hash of the string "Hello":

 <?php
$str = "Shanghai" ;
echo sha1 ( $str ) ;
?>

Try it yourself

Example 2

Output result of sha1():

 <?php
$str = "Shanghai" ;
echo "String:" . $str . "<br>" ;
echo "TRUE - original 20-character binary format: " . sha1 ( $str , TRUE ) . "<br>" ;
echo "FALSE - 40 character hexadecimal number: " . sha1 ( $str ) . "<br>" ;
?>

Try it yourself

Example 3

Output the result of sha1() and test it:

 <?php
$str = "Shanghai" ;
echo sha1 ( $str ) ;

if ( sha1 ( $str ) == "b99463d58a5c8372e6adbdca867428961641cb51" )
  {
  echo "<br>I love Shanghai!" ;
  exit ;
  }
?>

Try it yourself

grammar

 sha1 ( string , raw )
parameter describe
string Required. Specifies the string to be calculated.
raw

Optional. Specify hexadecimal or binary output format:

  • TRUE - Original 20-character binary format
  • FALSE - Default. 40 characters hexadecimal number
Similar Functions
Popular Articles