';
$algos = hash_algos(); //列出所有支持的hash算法
// print_r($algos);
// ------------------------------------------------------
// 字符串hash
$data = 'The quick brown fox jumped over the lazy dog.';
echo hash('md5', $data); //md5 哈希
$key = 'md5-key';
echo hash_hmac('md5', $data, $key); //使用 HMAC 方法生成带有密钥的哈希值
// ------------------------------------------------------
// 文件hash
$file = 'hmac.txt';
echo hash_file('md5', $file);
echo hash_hmac_file('md5', $file, $key);
// ------------------------------------------------------
/**
* @param $algo hash算法
* @param $data string|array 字符串或者字符串数组
* @param $options 进行哈希运算的可选设置,目前仅支持:HASH_HMAC。当指定此选项时,必须指定 key 参数
* @param $key 当 options 参数为 HASH_HMAC 时,使用此参数传入进行 HMAC 哈希运算时的共享密钥
*/
function my_hash_data($algo, $data, $options = 0, $key = NULL) {
// resource hash_init ( string $algo [, int $options = 0 [, string $key = NULL ]] )
$ctx = hash_init($algo, $options, $key);
if(is_string($data)) {
hash_update($ctx, $data);
} else if(is_array($data)) {
foreach($data as $s) {
hash_update($ctx, $s); //填充数据, 可以多次调用, 和拼接字符串效果一样
}
}
return hash_final($ctx); //输出最后的数据
}
// test code
echo my_hash_data('md5', $data);
// ------------------------------------------------------
/**
* 文件类型 hash
*/
function my_hash_file($algo, $filename, $options = 0, $key = NULL) {
$ctx = hash_init($algo, $options, $key);
/*
两个函数的不同之处:
1. hash_update_stream 第二个参数是一个打开的文件句柄
2. hash_update_file 第二个参数是一个文件名
*/
hash_update_file($ctx, $filename);
return hash_final($ctx);
}
// test code
echo my_hash_file('sha1', $file); 0
0
相关文章
php实现班级通信录导入邮箱格式错_php校验邮箱导入法【技巧】
如何在 PHP 中动态构建 URL 并安全获取并展示 JSON 数据
php怎样二维转一维转枚举值_php二维降维生成枚举数组【方法】
php获取本机ip含端口吗_php分离ip与端口方法【步骤】
如何在 PHP 中为数组中每对不重复元素生成源-目标关系对
相关标签:
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具











