Created
March 1, 2017 07:09
-
-
Save wudi/fcb452b93f9ed3c24eea73ca3b9be3f6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $SECRET_KEY = '387a1a3091c29cf7ff253621c347f6df'; | |
| $args = $_POST; | |
| // 校验必要参数是否存在 | |
| if((!isset($args['sign'])) || (!isset($args['timestamp']))){ | |
| echo "缺少必要参数"; | |
| exit(1); | |
| } | |
| // 校验请求是否过期,自定义延迟10分钟以上的消息丢弃 | |
| if($args['timestamp'] < (time() - 10*60)){ | |
| echo "消息已过期"; | |
| exit(1); | |
| } | |
| // 获取原始 签名 保存 | |
| $sourceSign = $args['sign']; | |
| // 移除签名字段 | |
| unset($args['sign']); | |
| // 所有字段升序排列 | |
| ksort($args); | |
| // 拼接 URL query string 字符串 (RFC3986 规范) | |
| $signStr = http_build_query($args, null, '&', PHP_QUERY_RFC3986); | |
| // 对signstr 进行 sha1 签名 | |
| $sign = hash_hmac('sha1', $signStr, $SECRET_KEY); | |
| // 对比自签名和服务器发送过来的签名一致 | |
| if($sourceSign == $sign) { | |
| echo "验签通过"; | |
| // 数据可信赖 | |
| }else{ | |
| echo "签名失败"; | |
| exit(1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment