Sha1 function in php

PHP SHA1() Function

PHP has many built-in functions to secure the data of the application. The hashing algorithm is one of the ways to secure the string data by creating a hash string of the string data. The SHA1() is a built-in function of PHP to create the hash string of any string value. The full form of SHA1 is Secure Hash Algorithm 1. It generates a hash string that is called a message digest. The different uses of this function are shown in this tutorial.

Syntax:
sha1(string $string, bool $binary = false): string

The first argument is mandatory and the hash string is generated based on the first argument value. The second argument is optional that contains a Boolean value. If the value of the second argument is TRUE, a hash string of 20 characters of data in binary format is generated. If the second argument is FALSE, a hash string of 40 characters in hex format is generated. It returns the hash value of the string that is used in the first argument of the function.

Читайте также:  Атрибут lang

Different Examples of SHA1() Function

The different uses of the SHA1() function are shown in this part of the tutorial using multiple examples.

Example 1: Using SHA1() Function Without Optional Argument

Create a PHP file with the following script that creates 40 characters long hash value using the SHA1() function without the optional argument. The original string value and the generated hash value are printed later.

//Assign a string value
$strval = «Testing Message» ;
echo «The original string: $strval
» ;
//Print the generated hash string
echo «The hash string : » . sha1 ( $strval ) ;
?>

Output:
The following output appears after executing the previous script:

Example 2: Using SHA1() Function With Optional Argument

Create a PHP file with the following script that creates the hash value using the SHA1() function with the optional argument. Forty (40) characters hash value are generated when False is used in the optional argument value of the SHA1() function. Twenty (20) characters hash value are generated when True is used in the optional argument value of the SHA1() function. The original string value, the generated hash value of 40 characters, and 20 characters are printed later.

//Assign a string value
$strval = «Testing Message» ;
echo «The original string: $strval
» ;
//Print the generated hash string
echo «The hash string of 40 characters: » . sha1 ( $strval , False ) . «
» ;
//Print the generated hash string
echo «The hash string of 20 characters: » . sha1 ( $strval , True ) . «
» ;
?>

Output:
The following output appears after executing the previous script:

Example 3: Checking the Hash Value of the Particular String

Create a PHP file with the following script that checks if a hash value of a string is correct or incorrect. The string value is taken from the URL parameter and the hash value of that string value is compared with a particular hash value to confirm whether the taken string value is correct or not.

if ( isset ( $_GET [ ‘msg’ ] ) )
{
//Generate the hash value of the string read from the URL
$strval = sha1 ( $_GET [ ‘msg’ ] ) ;
//Print the original string value
echo «The original string: » . $_GET [ ‘msg’ ] . «
» ;
//Check the hash value of the corresponding string value
if ( $strval == ‘640ab2bae07bedc4c163f679a746f7ab7fb5d1fa’ )
echo «Correct hash value» ;
else
echo «Wrong hash value.» ;

}
else
echo «String value is not given.» ;

Output:
The following output appears after executing the previous script without the URL parameter:

The following output appears after executing the previous script by providing the valid string in the URL parameter:

Example 4: Using SHA1() Function for Validation

The SHA1() function can be used for validating the data. The way of validating the login information using the SHA1() function is shown in this tutorial. Create an HTML file named login.html with the following script that takes the username and password from the user using an HTML form. The form data is parsed using a PHP file named login.php.

Create a PHP file named login.php with the following script. The username and password values that are submitted by the form are stored in two variables. Next, the value of the username and password are checked. If the username contains the “admin” value and the password contains the “secretpassword” value, the “Valid User” message is printed. Otherwise, the “Invalid User” message is printed.

Output:
The following output appears after executing the login.html from the browser:

Type the valid username and password in the login form and click on the Login button.

The following output appears for the valid username and password values:

The following output appears for the invalid username or password values:

Conclusion

The different uses of the SHA1() function are described in this tutorial using this function in multiple ways. PHP has many other functions like the SHA1() function to generate the hash values but this function is so simple to use for validating the data or checking the data integrity. We hope that this tutorial helps the PHP users to know how to use the SHA1() function properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

PHP sha1()

PHP sha1()

PHP sha1() function is a very important function as part of PHP as it is a backed server-side scripting language that needs more emphasis on the security terms. PHP sha1() deals with the security and hashing function which calculates and computes a value of SHA-1 of the hash of the string. Internally PHP sha1() makes use of a subtype of the US Secure Hash Algorithm 1. Sha1() function produces a hash string with a value of 160 characters and then when this hash string is given as an input to the function it produces an output which is a highly secured message digest.

Web development, programming languages, Software testing & others

Syntax

Explanation: sha1() function makes use of two types of arguments like string and raw which is used for generating the string and calculating the length of the string with some value. raw is another argument or parameter which is optional in the sense if specified with the sha1() function then it passes the parameter with an optional value such as true or false and then it passes the remaining value to specify and describe the hex or binary value output format. If the optional value appears to be Raw 20 then it is a 20-character binary format otherwise it Is Default 40-character hex number with some specific value.

How does sha1() function work in PHP?

As PHP is a server-oriented scripting language it is used to keep the backend related data and manipulations preserved and secured. sha1() function has solved the problem of securing the data related to password and user management. sha1() function and its sub algorithms like US-Secure Hash algorithm 1 are used for the generation of a message digest and digital signature.

The SHA-1() function makes use of the US-Secure hash algorithm1 which is used in a way where the string is given as an input and then a message digest is given as an output. Input is fed to the signature algorithm which checks and verifies for the signature of the message. If a signature message is used as an input rather than the actual message, then it has a high chance of improving the efficiency of the overall algorithm.

This process will optimize and compress the message input and message output functionality compared to the normal hash string message as an input to the signature algorithm. Further if this algorithm is used by the verifier then a digital signature can be used by the creator of the digital signature.

sha_file() function is another subcomponent of the sha1() function which uses the hash of the file function.

This file function of the file name is used to calculate the hash of a file and one raw output will be given to it which will be used to return the message or the string with a value of whether true or false. Md5() algorithm within the function and crc32() with the function will also be used to generate the polynomial of the string. And will help in generating a more secured string with some refined digital signature.

Examples to Implement PHP sha1() Function

Below are mentioned the examples:

Example #1

his program represents the calculation of the SHA-1 hash of the string after passing one string value to get the hash of the string.

PHP sha1()1

Example #2

This program represents the calculation of the SHA-1 hash of the string after passing one string value to get the hash of the string and then it prints the value of the sha1 string as shown in the output. The input of the string is given as “Welcome to Educba” and the output shows the string value.

PHP sha1()2

Example #3

This program represents the calculation of the SHA-1 hash of the string after passing one string value to get the hash of the string and then it prints the value of the sha1 string as shown in the output. The input of the string is given as “Welcome to Educba” and the output shows the string value. Followed by a test of the input string being fed as an output.

PHP sha1()3

Example #4

This program is also a part of the sha1() function associated function of CRC 32 algorithm which takes a string “Hello World ” as input and then echoes the value without and with the string of % u value as shown in the output.

PHP sha1()4

Example #5

This program makes use of the password_hash function as part of the sha1() function and helps in generating the password_hash with an output value as shown and makes use of hashing function by putting the cost parameter as 12 to get the optimized message digest as the final output to optimize and increase the overall efficiency of the program.

 12, ]; echo password_hash("educba_is_a_laerning_portal", PASSWORD_BCRYPT, $options); ?>

password_hash

Example #6

This program makes use of the hash () function to generate the message digest of the given function which will be further used to convert into a digital signature for optimization.

, str_split(hash("tiger192,$rounds", $data, true), 8) ) ), 0, 48-(192-$width)/4 ); > echo hash('tiger192,3', 'a-string'), PHP_EOL; echo lion('a-string'), PHP_EOL; ?>

hash () function

Example #7

This program illustrated the md5 algorithm to be fed as an input string which is also counted as one of the complementary parts of the sha1() algorithm.

md5 algorithm

Conclusion

sha1() function is a part of PHP string references which includes a lot of security and cryptographic algorithms which is very necessary for the backend services and the servers for continuing the overall security breaches related issues and password and user management related data as secured.

This is a guide to PHP sha1(). Here we discuss an introduction, Syntax, and working of sha1() in PHP along with different examples and code implementation. You can also go through our other related articles to learn more –

25+ Hours of HD Videos
5 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

92+ Hours of HD Videos
22 Courses
2 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

83+ Hours of HD Videos
16 Courses
1 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

PHP Course Bundle — 8 Courses in 1 | 3 Mock Tests
43+ Hours of HD Videos
8 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

Источник

Оцените статью