php实现hmac sha1,PHP:如何生成字符串的hmac SHA1签名?

news/2025/2/23 16:00:48

我正在尝试使用PHP连接到API并需要正确的签名.

他们的文件逐字记录:

The command string needs to hashed using HMAC SHA-1 hashing algorithm

against the API secret key. The resulting byte array should be Base64

encoded in UTF-8 format so that it can be passed via http.

To generate the signature you have to lower case the complete list of

request parameters and sort them alphabetically via the field for each

field-value pair. The resulting string to sign from the previous

example with a secret key VDaACYb0LV9eNjTetIOElcVQkvJck_J_QljX would

be:

apikey=mivr6x7u6bn_sdahobpjnejpgest35exqjb8cg20&command=deployvirtualmachine&serviceofferingid=21624abb-764e-4def-81d7-9fc54b5957fb&templateid=54c83a5e-c548-4d91-8b14-5cf2d4c081ee&zoneid=1128bd56-b4d9-4ac6-a7b9-c715b187ce11

Resulting in a signature value of:

ahlpA6J1Fq6OYI1HFrMSGgBt0WY%3D

示例尝试:

$string = 'apikey=mivr6x7u6bn_sdahobpjnejpgest35exqjb8cg20&command=deployvirtualmachine&serviceofferingid=21624abb-764e-4def-81d7-9fc54b5957fb&templateid=54c83a5e-c548-4d91-8b14-5cf2d4c081ee&zoneid=1128bd56-b4d9-4ac6-a7b9-c715b187ce11

';

$string = utf8_encode(strtolower($string));

$key = 'VDaACYb0LV9eNjTetIOElcVQkvJck_J_QljX';

$signature = hash_hmac('sha1', $string , $key);

print 'SIGNATURE:'.$signature.'
';

if($signature=='ahlpA6J1Fq6OYI1HFrMSGgBt0WY%3D'){

print 'SUCCESS';

}else{

print 'FAIL';

}

结果:9077d90baa7ab8913811b64a50814b640dce60eb

假设是:ahlpA6J1Fq6OYI1HFrMSGgBt0WY =

问题:结果与他们的文档不符.知道我做错了什么吗?

解决方法:

您的签名应该像这样生成:

$signature = urlencode(base64_encode(hash_hmac('sha1', $string , $key, true)));

最后一个参数的默认值为false.然后它将返回十六进制编码的字符串而不是原始字节.然后你必须按照文档中的说明对字节进行base64_encode.然后你必须对它进行urlencode,因为=必须进行转换

标签:hmac,php,hmacsha1

来源: https://codeday.me/bug/20190827/1744948.html


http://www.niftyadmin.cn/n/529804.html

相关文章

房贷计算公式

//本息还款的月还款额(参数: 年利率/贷款总额/贷款总月份) //function getMonthMoney1(lilv,total,month){ // var lilv_month lilv / 12;//月利率 // return total * lilv_month * Math.pow(1 lilv_month, month) / ( Math.pow(1 lilv_month, m…

二分搜索树实现Java的Map(下)

二分搜索树Map public class BSTMap<K extends Comparable<K>,V> implements Map<K,V> {private class Node{public K key;public V value;public Node left,right;public Node(K key,V value){this.key key;this.value value;left null;right null;}}pr…

使用RSL来瘦身Flex生成的SWF文件

1----------------------减小应用SWF文件大小的一个方法就是将一些共享的外部资源拆分出去, 成为一个独立的文件, 这样可以单独地加载缓存到客户端. 这些共享资源可以由多个应用在运行时进行加载, 但是传递到客户端的动作只会发生一次. 这些共享文件被称为运行时共享库(Runtime…

matlab 一维 平滑,MATLAB中数据平滑处理

参考链接&#xff1a;http://bbs.pinggu.org/thread-3777396-1-1.htmlsmoothts函数调用格式&#xff1a;output smoothts(input)output smoothts(input, ‘b’, wsize) % 盒子法output smoothts(input, ‘g’, wsize, stdev) % 高斯窗方法output smoothts(input, ‘e’, n)…

php异常处理

PHP具有很多异常处理类&#xff0c;其中Exception是所有异常处理的基类。Exception具有几个基本属性与方法&#xff0c;其中包括了&#xff1a;message 异常消息内容code 异常代码file 抛出异常的文件名line 抛出异常在该文件的行数其中常用的方法有&#xff1a;getTrace 获取异…

XML CDATA的作用

当你用FLASH和xml结合做网站应用程序时&#xff0c;例如你做在我研究游戏排行榜中&#xff0c;当让人自由输入姓名时&#xff0c;人们可以输入一些符号&#xff0c;例如∶"<"、">"、"/"、"&#xff1f;"等&#xff0c;当生成XML…

《剑指offer》第三十八题(字符串的排列)

// 面试题38&#xff1a;字符串的排列 // 题目&#xff1a;输入一个字符串&#xff0c;打印出该字符串中字符的所有排列。例如输入字符串abc&#xff0c; // 则打印出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab和cba。#include <iostream>void Permu…

有关AS3编程的一些总结

最近用AS3写一些项目&#xff0c;在编程过程中&#xff0c;遇到不少问题&#xff0c;同时也有一些收获和心得。现在贴出来希望对大家在AS3编程有一些帮助。如果你发现有说得不对的地方&#xff0c;你可以指出来&#xff0c;大家一起来讨论。1、AS3的强制类型转换我之前一直以为…