ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

PHP 中hash_hmac是什么?

2024-11-01 19:38:41  阅读:28  来源: 互联网

标签:


hash_hmac 是 PHP 中用于生成基于密钥的哈希(HMAC)的方法。它结合了哈希函数和一个密钥,可以用于数据完整性和身份验证的场景。

基本用法

以下是如何使用 hash_hmac 的基本示例:

<?php
// 输入数据
$data = '1730187103326{"id":"evt_hkpd6zzxgh18sigbqh4_i5o34e","name":"payment_intent.succeeded","account_id":"acct_rGL_IoGyNzCfKN59a_mlDg","data":{"object":{"amount":38,"base_amount":38,"base_currency":"HKD","captured_amount":38,"created_at":"2024-10-27T11:42:14+0000","currency":"HKD","customer_id":"cus_hkpd78g4xh1141c0hf5","descriptor":"thegameclear","id":"int_hkpd4fmk6h18si5o34e","latest_payment_attempt":{"amount":38,"authentication_data":{"avs_result":"U","cvc_result":"U","ds_data":[],"fraud_data":{"action":"ACCEPT","risk_factors":[],"score":"0"}},"captured_amount":38,"created_at":"2024-10-27T11:42:18+0000","currency":"HKD","expires_at":"2024-10-27T11:56:18+0000","id":"att_hkpdwrjb6h18si7vewq_i5o34e","merchant_order_id":"F2024102719421427499854741005292","payment_intent_id":"int_hkpd4fmk6h18si5o34e","payment_method":{"alipayhk":{"flow":"mobile_web","os_type":"android","user_id":"210220900023343780022"},"type":"alipayhk"},"payment_method_options":{"card":[]},"payment_method_transaction_id":"20241027194010800100160410255062588","provider_transaction_id":"2024102719074101000990013366064","refunded_amount":0,"settle_via":"airwallex","status":"CAPTURE_REQUESTED","updated_at":"2024-10-27T11:42:32+0000"},"merchant_order_id":"F2024102719421427499854741005292","request_id":"241027194214322302","return_url":"https://api-pay.thegameclear.com/pay_for_web_third/notify/airwallex/sync_pay_notify?order_number=F2024102719421427499854741005292&sign=53a3b64ce1b05a6ca24c8b3218b00716","status":"SUCCEEDED","updated_at":"2024-10-27T11:42:32+0000"}},"created_at":"2024-10-27T11:42:32+0000","version":"2024-06-30"}2881bebb387796d05c8931156a822ec151dfe57b0b41cc4d35b42e15ac41ac18';

// 密钥
$key = 'your_secret_key';

// 计算 HMAC
$hmac = hash_hmac('sha256', $data, $key);

// 输出 HMAC
echo $hmac;
?>

PHP

说明

  • hash_hmac 的第一个参数是你希望使用的哈希算法,这里使用的是 sha256
  • 第二个参数是需要进行哈希的数据。
  • 第三个参数是密钥。

请确保将 'your_secret_key' 替换为你实际使用的密钥。运行该代码,你将获得输入数据的 HMAC 哈希值。

标签:
来源:

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有