百科狗-知识改变命运!
--

hash_hmac() - php 密码散列哈希函数

百变鹏仔1年前 (2023-11-21)阅读数 25#技术干货
文章标签密钥

hash_hmac()

(PHP 5 >= 5.1.2, PHP 7, PECL hash >= 1.1)

使用 HMAC 方法生成带有密钥的哈希值

说明

hash_hmac(string $algo,string $data,string $key[,bool $raw_output=FALSE]): string

参数

$algo

要使用的哈希算法名称,例如:"md5","sha256","haval160,4"等。如何获取受支持的算法清单,请参见hash_hmac_algos()函数。

$data

hash_hmac() - php 密码散列哈希函数

要进行哈希运算的消息。

$key

使用 HMAC 生成信息摘要时所使用的密钥。

$raw_output

设置为TRUE输出原始二进制数据,设置为FALSE输出小写 16 进制字符串。

返回值

如果$raw_output设置为TRUE,则返回原始二进制数据表示的信息摘要,否则返回 16 进制小写字符串格式表示的信息摘要。如果$algo参数指定的不是受支持的算法,返回FALSE

更新日志

版本说明
7.2.0不再支持非加密的哈希函数(adler32,crc32,crc32b,fnv132,fnv1a32,fnv164,fnv1a64,joaat)。

范例

Example #1hash_hmac()例程

以上例程会输出:

b8e7ae12510bdfb1812e463a7f086122cf37e4f7

参见

  • hash() 生成哈希值(消息摘要)
  • hash_hmac_algos()Return a list of registered hashing algorithms suitable for hash_hmac
  • hash_init() 初始化增量哈希运算上下文
  • hash_hmac_file() 使用 HMAC 方法和给定文件的内容生成带密钥的哈希值
Very important notice, if you pass array to $data, php will generate a Warning, return a NULL and continue your application. Which I think is critical vulnerability as this function used to check authorisation typically.
Example:

Of course not documented feature.
Please be careful when comparing hashes. In certain cases, information can be leaked by using a timing attack. It takes advantage of the == operator only comparing until it finds a difference in the two strings. To prevent it, you have two options.
Option 1: hash both hashed strings first - this doesn't stop the timing difference, but it makes the information useless.

Option 2: always compare the whole string. 
As Michael uggests we should take care not to compare the hash using == (or ===). Since PHP version 5.6 we can now use hash_equals().
So the example will be: 
Here is an efficient PBDKF2 implementation:

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)