Exemple
Calculer le hachage SHA-1 du fichier texte "test.txt" :
<?php
$filename = "test.txt";
$sha1file = sha1_file($filename);
echo $sha1file;
?>
au-dessus de la sortie du code sera:
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
Définition et utilisation
Le sha1_file() calcule la valeur de hachage SHA-1 d'un fichier.
Le sha1_file() fonction utilise l'algorithme Secure Hash US 1.
De la RFC 3174 - L'algorithme Secure Hash US 1: "SHA-1 produces a 160-bit output called a message digest. The message digest can then, for example, be input to a signature algorithm which generates or verifies the signature for the message. Signing the message digest rather than the message often improves the efficiency of the process because the message digest is usually much smaller in size than the message. The same hash algorithm must be used by the verifier of a digital signature as was used by the creator of the digital signature." de "SHA-1 produces a 160-bit output called a message digest. The message digest can then, for example, be input to a signature algorithm which generates or verifies the signature for the message. Signing the message digest rather than the message often improves the efficiency of the process because the message digest is usually much smaller in size than the message. The same hash algorithm must be used by the verifier of a digital signature as was used by the creator of the digital signature." de "SHA-1 produces a 160-bit output called a message digest. The message digest can then, for example, be input to a signature algorithm which generates or verifies the signature for the message. Signing the message digest rather than the message often improves the efficiency of the process because the message digest is usually much smaller in size than the message. The same hash algorithm must be used by the verifier of a digital signature as was used by the creator of the digital signature." la "SHA-1 produces a 160-bit output called a message digest. The message digest can then, for example, be input to a signature algorithm which generates or verifies the signature for the message. Signing the message digest rather than the message often improves the efficiency of the process because the message digest is usually much smaller in size than the message. The same hash algorithm must be used by the verifier of a digital signature as was used by the creator of the digital signature."
Cette fonction retourne la valeur calculée de hachage SHA-1 en cas de succès, en cas d'échec.
Syntaxe
sha1_file( file,raw )
Paramètre | La description |
---|---|
file | Champs obligatoires. Le fichier à calculer |
raw | Optionnel. Une valeur booléenne qui spécifie le format de sortie hexadécimal ou binaire:
|
Détails techniques
Valeur de retour: | Renvoie la valeur calculée hachage SHA-1 en cas de succès, en cas d'échec |
---|---|
PHP Version: | 4.3.0+ |
changelog: | Le paramètre brut est devenu optionnel en PHP 5.0 PHP 5.1, il est possible d'utiliser sha1_file() avec des enveloppes, par exemple sha1_file("http://w3ii.com/..") |
autres exemples
Exemple 1
Stocker le hachage SHA-1 de "test.txt" dans un fichier:
<?php
$sha1file = sha1_file("test.txt");
file_put_contents("sha1file.txt",$sha1file);
?>
Test si "test.txt" a été modifiée (qui est si le hachage SHA-1 a été modifié):
<?php
$sha1file = file_get_contents("sha1file.txt");
if (sha1_file("test.txt") == $sha1file)
{
echo "The file is ok.";
}
else
{
echo "The file has been changed.";
}
?>
au-dessus de la sortie du code pourrait être:
The file is ok.
<PHP chaîne de référence