php tutorial - PHP sha1 Function - php programming - learn php - php code - php script



  • The sha1 string function is used to calculate the SHA-1 (Secure Hash Algorithm 1) hash of a string value.
  • It is mainly used for converting a string into 40-bit hexadecimal number, which is so secured.
  • "SHA-1" produces a 160-bit output called a message digest.

php developer Syntax :

string sha1( string source [, bool raw_output]);
click below button to copy the code. php tutorial - team

Syntax Explanation:

  • string source- defines the input string.
  • raw_output – The raw output is like a Boolean function that is: If TRUE , it sets raw 20-character binary format. If FALSE (default) then it sets raw 40-character hex number format.

Advantage of Using sha-1 function :

  • When you use the sha1 function at first time while running the string it will generate a different hash value for the string. If you run it again and again the hash vale for the string will be different every time.
  1. Produces a longer hash value than MD5
  2. Collision Resistant
  3. Widespread Use
  4. Provides a one-way hash

Disadvantage of Using sha-1 function :

  1. Sha-1 algorithm is the slower computational algorithm than the MD5 algorithm.
  2. Has known security vulnerabilities

php 7 Sample Code : sample php program

<!DOCTYPE html>
<html>
    <body>
        <?php
            echo sha1('wikitechy');
            echo "<br>";
            echo sha1('wikitechy',TRUE);
        ?>
    </body>
</html>
click below button to copy the code. php tutorial - team

php program Code Explanation :

Code Explanation for sha1 Function In PHP

  1. In this echo statement we call the “sha1” string function that will be used for calculating the hash function value for the term “wikitechy” .
  2. In this echo statement we call the “sha1” string function that will be used for calculating the hash function value of the term “wikitechy “and here we use the Boolean function set as true so the raw output of the encrypted text has been displayed only the 20 characters in your browser.

php coding Sample Output :

output for sha1 Function In PHP

  1. Here in this output the term “wikitechy” was encrypted using the sha1 algorithm which encrypts by setting the raw 40-character hex number format.
  2. Similarly, over here the term “wikitechy” was encrypted using the sha1 algorithm since we setted the argument value set as “true” so the encryption was done in 20-character binary format.


Related Searches to php sha1 function