Exemple
Calculer le hachage SHA-1 de la chaîne "Hello" :
<?php
$str = "Hello";
echo sha1($str);
?>
»Exécuter exemple Définition et utilisation
Le sha1() calcule la valeur de hachage SHA-1 d'une chaîne.
Le sha1() 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."
Astuce: Pour calculer le hachage SHA-1 d'un fichier, utilisez la sha1_file() fonction.
Syntaxe
sha1( string,raw )
Paramètre | La description |
---|---|
string | Champs obligatoires. La chaîne à calculer |
raw | Optionnel. Spécifiez 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 |
autres exemples
Exemple 1
Imprimer le résultat de sha1() :
<?php
$str = "Hello";
echo "The string: ".$str."<br>";
echo
"TRUE - Raw 20 character binary format: ".sha1($str, TRUE)."<br>";
echo
"FALSE - 40 character hex number: ".sha1($str)."<br>";
?>
»Exécuter exemple exemple 2
Imprimer le résultat de sha1() , puis le tester:
<?php
$str = "Hello";
echo sha1($str);
if (sha1($str) ==
"f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0")
{
echo "<br>Hello
world!";
exit;
}
?>
»Exécuter exemple <PHP chaîne de référence