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.
Calculate the SHA-1 hash of the string "Hello":
<?php $str = "Shanghai" ; echo sha1 ( $str ) ; ?>
Try it yourself
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
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
sha1 ( string , raw )
parameter | describe |
---|---|
string | Required. Specifies the string to be calculated. |
raw |
Optional. Specify hexadecimal or binary output format:
|