пример
Вычислить MD5 хеш текстового файла "test.txt" :
<?php
$filename = "test.txt";
$md5file = md5_file($filename);
echo $md5file;
?>
Выход кода выше:
d41d8cd98f00b204e9800998ecf8427e
Определение и использование
md5_file() функция вычисляет MD5 хэш файла.
md5_file() функция использует RSA Data Security, Inc. MD5 Message-Digest Algorithm.
Из RFC 1321 - The MD5 Message-Digest Algorithm: "The MD5 message-digest algorithm takes as input a message of arbitrary length and produces as output a 128-bit "fingerprint" or "message digest" of the input. The MD5 algorithm is intended for digital signature applications, where a large file must be "compressed" in a secure manner before being encrypted with a private (secret) key under a public-key cryptosystem such as RSA." - "The MD5 message-digest algorithm takes as input a message of arbitrary length and produces as output a 128-bit "fingerprint" or "message digest" of the input. The MD5 algorithm is intended for digital signature applications, where a large file must be "compressed" in a secure manner before being encrypted with a private (secret) key under a public-key cryptosystem such as RSA." в "The MD5 message-digest algorithm takes as input a message of arbitrary length and produces as output a 128-bit "fingerprint" or "message digest" of the input. The MD5 algorithm is intended for digital signature applications, where a large file must be "compressed" in a secure manner before being encrypted with a private (secret) key under a public-key cryptosystem such as RSA." в "The MD5 message-digest algorithm takes as input a message of arbitrary length and produces as output a 128-bit "fingerprint" or "message digest" of the input. The MD5 algorithm is intended for digital signature applications, where a large file must be "compressed" in a secure manner before being encrypted with a private (secret) key under a public-key cryptosystem such as RSA." , "The MD5 message-digest algorithm takes as input a message of arbitrary length and produces as output a 128-bit "fingerprint" or "message digest" of the input. The MD5 algorithm is intended for digital signature applications, where a large file must be "compressed" in a secure manner before being encrypted with a private (secret) key under a public-key cryptosystem such as RSA."
Чтобы вычислить MD5 хэш строки, используйте md5() функцию.
Синтаксис
md5_file( file,raw )
параметр | Описание |
---|---|
file | Необходимые. Файл необходимо рассчитать |
raw | Необязательный. Логическое значение, которое определяет формат гекс или двоичный выход:
|
Технические подробности
Возвращаемое значение: | Возвращает вычисленный хэш MD5 на успех, или FALSE при неудаче |
---|---|
PHP версии: | 4.2.0+ |
Changelog: | Сырья параметр был добавлен в PHP 5.0 По состоянию на PHP 5.1, можно использовать md5_file() с обертками, например md5_file("http://w3ii.com/..") |
Еще примеры
Пример 1
Храните MD5 - хэш "test.txt" в файле:
<?php
$md5file = md5_file("test.txt");
file_put_contents("md5file.txt",$md5file);
?>
Тест , если "test.txt" был изменен (то есть , если MD5 хэш был изменен):
<?php
$md5file = file_get_contents("md5file.txt");
if (md5_file("test.txt") == $md5file)
{
echo "The file is ok.";
}
else
{
echo "The file has been changed.";
}
?>
Выход выше код может быть:
The file is ok.
<String Reference PHP