Jump to: navigation, search

Php sha1 file

From w3cyberlearnings

Contents

PHP function sha1_file

This function calculates the SHA-1 hash of a file.

Syntax sha1_file

  • Filename: name of a file
  • Raw (Option): True (20 character binary format), False (Default- 40 character hex number)
sha1_file(filename, RAW)

SHA1 Encryption

In cryptography, SHA-1 is a cryptographic hash function designed by the United States National Security Agency and published by the United States NIST as a U.S. Federal Information Processing Standard. SHA stands for "secure hash algorithm". The three SHA algorithms are structured differently and are distinguished as SHA-0, SHA-1, and SHA-2. SHA-1 is very similar to SHA-0, but corrects an error in the original SHA hash specification that led to significant weaknesses. The SHA-0 algorithm was not adopted by many applications. SHA-2 on the other hand significantly differs from the SHA-1 hash function.

SHA-1 is the most widely used of the existing SHA hash functions, and is employed in several widely used applications and protocols. In 2005, security flaws were identified in SHA-1, namely that a mathematical weakness might exist, indicating that a stronger hash function would be desirable.[2] Although no successful attacks have yet been reported on the SHA-2 variants, they are algorithmically similar to SHA-1 and so efforts are underway to develop improved alternatives.[3][4] A new hash standard, SHA-3, is currently under development — an ongoing NIST hash function competition is scheduled to end with the selection of a winning function in 2012.


Example 1: You need to have a text file called:myprofile.txt

<?php

$file = "myprofile.txt";
$sha1file =  sha1_file($file);
echo $sha1file;
?>

Output

5ed51ab03846591bf8fe880c23be1fe1c8e7bd19

Example 2

  • Test whether the file has been modified by someone.
  • If you modified the 'myprofile.txt', and add additional content.
<?php

$file = "myprofile.txt";
$top_secret = '5ed51ab03846591bf8fe880c23be1fe1c8e7bd19';
$sha1file = sha1_file($file);

if (strcmp($top_secret, $sha1file) == 0) {
	echo 'Match, it is safe. Nobody modified this file before!';
} else {
	echo 'Have been modified the file';
}
?>

Output 1: You must modified the file in order to see this

Have been modified the file

Output 2: Never modified the file

Match, it is safe. Nobody modified this file before!

Related Functions


Navigation
Web
SQL
MISC
References