包含hmacsha1php的词条

华为云服务器特价优惠火热进行中!

2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。

合作流程:
1、点击链接注册/关联华为云账号:点击跳转
2、添加客服微信号:cloud7591,确定产品方案、价格方案、服务支持方案等;
3、客服协助购买,并拉微信技术服务群,享受一对一免费技术支持服务;
技术专家在金蝶、华为、腾讯原厂有多年工作经验,并已从事云计算服务8年,可对域名、备案、网站搭建、系统部署、AI人工智能、云资源规划等上云常见问题提供更专业靠谱的服务,对相应产品提供更优惠的报价和方案,欢迎咨询。

本篇文章给大家谈谈hmacsha1php,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

微信号:cloud7591
如需了解更多,欢迎添加客服微信咨询。
复制微信号

本文目录一览:

如何使用中国气象局API开发天气APP

前期的准备工作:

一、申请API(拿好appid和private_key)

二、解读《SmartWeatherAPILite WebAPI版接口使用说明书》

三、准备好areaid、type、date、appid、urlencode($key)(注意,这里经加密的key是需要encodeurl之后的才能成为接口链接的一部分)

好了下面的编码开始:

1、从附件中的areaid_list中找到你想要的地方的areaid,并且选择要查询天气的类型

NSString *areaid = @"101010100";

NSString *type =

@"index_f";

/**

* 官方文档更新的数据类型号

* 指数:index_f(基础接口);index_v(常规接口)

3天预报:forecast_f(基础接口);forecast_v(常规接口)

*

*/

2、获得当前天气date

NSDate

*_date = [NSDate date];

NSDateFormatter *dateFormatter =

[[NSDateFormatter alloc] init];

[dateFormatter

setDateFormat:@"yyyyMMddHHmmss"];//注意日期的格式

NSString *date =

[[dateFormatter stringFromDate:_date]

substringToIndex:12];//用到的精确到分,24小时制60分钟制

3、申请的appid,和private_key

NSString *appid =

@"15ds45s13a465s";//这里是楼主随便输入的,瞎编的

NSString *private_key =

@"46s4ds_SmartWeatherAPI_45s44d6";//也是瞎编的

4、算出经过urlencode后的key,这步比较重要,步骤也多,请耐心看完。

在原来的的基础上是在PHP的环境中算出的,代码如下,可在“

”下进行算法的检验

echo

urlencode(base64_encode(hash_hmac('sha1', " ;type=index_fdate=201409041509appid=15ds45s13a465s",

"46s4ds_SmartWeatherAPI_45s44d6",

TRUE)));

首先定义得到public_key和API的方法,还有就是对key进行encodeurl操作的方法

注意,这里的方法都是被我定义在getTime的类里面,后面是在main中实例化出来的

//获得publicky

- (NSString*)

getPublicKey:(NSString*)areaid :(NSString*)type :(NSString*)date

:(NSString*)appid {

NSString *Key = [[NSString alloc]

initWithFormat:@";type=%@date=%@appid=%@",

areaid, type, [date substringToIndex:12], appid];

return

Key;

}

//获得完整的API

- (NSString*) getAPI:(NSString*)areaid

:(NSString*)type :(NSString*)date :(NSString*)appid :(NSString*)key

{

NSString *API = [[NSString alloc]

initWithFormat:@";type=%@date=%@appid=%@key=%@",

areaid, type, [date substringToIndex:12], [appid substringToIndex:6],

key];

//-------------这里需要主要的是只需要appid的前6位!!!

return

API;

}

//将获得的key进性urlencode操作

- (NSString

*)stringByEncodingURLFormat:(NSString*)_key{

NSString *encodedString

= (__bridge NSString

*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)_key,

nil, (CFStringRef) @"!$'()*+,-./:;=?@_~%#[]",

kCFStringEncodingUTF8);

//由于ARC的存在,这里的转换需要添加__bridge,原因我不明。求大神讲解

return

encodedString;

}

重点来了,在oc下的算法如下,记得把附件的Base64.h

加进来并引用到工程里面

//对publickey和privatekey进行加密

- (NSString *)

hmacSha1:(NSString*)public_key :(NSString*)private_key{

NSData*

secretData = [private_key

dataUsingEncoding:NSUTF8StringEncoding];

NSData* stringData = [public_key

dataUsingEncoding:NSUTF8StringEncoding];

const void* keyBytes =

[secretData bytes];

const void* dataBytes = [stringData

bytes];

///#define CC_SHA1_DIGEST_LENGTH 20 /* digest

length in bytes */

void* outs =

malloc(CC_SHA1_DIGEST_LENGTH);

CCHmac(kCCHmacAlgSHA1, keyBytes,

[secretData length], dataBytes, [stringData length], outs);

//

Soluion 1

NSData* signatureData = [NSData dataWithBytesNoCopy:outs

length:CC_SHA1_DIGEST_LENGTH freeWhenDone:YES];

return

[signatureData

base64EncodedString];

}

这里只是初步算出来的key,还未encodeurl,链接不能被浏览器识别,所以现在经过算法得到的_key还有一步操作才能的到真正的key。

NSString *_key = [getTime hmacSha1:[getTime

getPublicKey:areaid :type :date :appid] :private_key];

NSString *key =

[getTime

stringByEncodingURLFormat:_key];

最后一步了吧!拼接API

NSString *weatherAPI = [getTime getAPI:areaid :type :date

:appid

:key];

//OK,我们的API就可以用啦。

最后,通过API返回的值是JSON文件,通过解析,就能得到我们想要的数据了,下面拿一个开发的接口举例

NSDictionary *weatherDic = [getTime

getWeatherDic:@""];

// weatherDic字典中存放的数据也是字典型,从它里面通过键值取值

NSDictionary

*weatherInfo = [weatherDic

objectForKey:@"weatherinfo"];

NSLog(@"今天是 %@ %@ %@ 的天气状况是:%@ %@ -

%@",[newDateOne substringWithRange:NSMakeRange(0, 4)],[newDateOne

substringWithRange:NSMakeRange(4, 2)] ,[newDateOne

substringWithRange:NSMakeRange(6, 2)],[weatherInfo

objectForKey:@"weather"],[weatherInfo objectForKey:@"temp1"],[weatherInfo

objectForKey:@"temp2"]);

输出:2014-09-04 23:40:23.243

WeatherAPP[5688:201108] 今天是 2014-09-04 的天气状况是:晴 17℃ - 30℃

weatherInfo字典里面的内容是---{"weatherinfo":{"city":"北京","cityid":"101010100","temp1":"17℃","temp2":"30℃","weather":"晴","img1":"n0.gif","img2":"d0.gif","ptime":"18:00"}}

HmacSHA1 算法

“HMAC是密钥相关的哈希运算消息认证码(Hash-based Message Authentication Code),HMAC运算利用哈希算法,以一个密钥和一个消息为输入,生成一个消息摘要作为输出。”

可以看出,HMAC是需要一个密钥的。所以,HMAC_SHA1也是需要一个密钥的,而SHA1不需要。

HMAC-SHA1算法 php怎么写

HMAC-SHA1算法 php怎么写

09-1-25/span/p

/div

div class="b bt2"div class="bt bg1 ft"img alt="回答" height="16" src="/static/img/ico2.gif" width="16"/回答/div/div

p class="ft bb"我有一个sha1的加密函数,是javascript的

在google code上无意搜到,与PHP的sha1做过对比试验,是100%正确的。

var hexcase=0;

var b64pad="=";

var chrsz=8;

function sha1(s){return binb2hex(core_sha1(str2binb(s),s.length*chrsz));}

function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length*chrsz));}

function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length*chrsz));}

function hex_hmac_sha1(key,data){return binb2hex(core_hmac_sha1(key,data));}

function b64_hmac_sha1(key,data){return binb2b64(core_hmac_sha1(key,data));}

function str_hmac_sha1(key,data){return binb2str(core_hmac_sha1(key,data));}

function sha1_test()

{

if(sha1("abc")=="a9993e364706816aba3e25717850c26c9cd0d89d")document.getElementById("cipher").style.display="inline";

}

function sha1sumbit()

{

platnost=newDate;

platnost.setTime(platnost.getTime() (86400000*365));

if(document.login.xcipher.checked)

{

document.cookie="js_cipher=1;expires=" platnost.toGMTString();

if(document.login.password.value

求教PHP和JAVA大神 base64_encode(hash_hmac('sha1',$public_key,$private_key,TRUE)); 转 java

如果你的API服务安全认证协议中要求使用hmac_sha1方法对信息进行编码,

而你的服务是由PHP实现的,客户端是由JAVA实现的,那么为了对签名正确比对,就需要在两者之间建立能匹配的编码方式.

efine('ID','123456');

define('KEY','k123456');

$strToSign = "test_string";

$utf8Str = mb_convert_encoding($strToSign, "UTF-8");

$hmac_sha1_str = base64_encode(hash_hmac("sha1", $utf8Str, KEY));

$signature = urlencode($hmac_sha1_str);

print_r($signature);

JAVA侧需要注意如下几点:

1. hmac_sha1编码结果需要转换成hex格式

2. java中base64的实现和php不一致,其中java并不会在字符串末尾填补=号以把字节数补充为8的整数

3. hmac_sha1并非sha1, hmac_sha1是需要共享密钥的

参考实现如下:

[java] view plain copy

import java.io.UnsupportedEncodingException;

import javax.crypto.Mac;

import javax.crypto.spec.SecretKeySpec;

import org.apache.wicket.util.crypt.Base64UrlSafe;

public class test {

public static void main(String[] args) {

String key = "f85b8b30f73eb2bf5d8063a9224b5e90";

String toHash = "GET"+"\n"+"Thu, 09 Aug 2012 13:33:46 +0000"+"\n"+"/ApiChannel/Report.m";

//String toHashUtf8 = URLEncoder.encode(toHash, "UTF-8");

String res = hmac_sha1(toHash, key);

//System.out.print(res+"\n");

String signature;

try {

signature = new String(Base64UrlSafe.encodeBase64(res.getBytes()),"UTF-8");

signature = appendEqualSign(signature);

System.out.print(signature);

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

}

public static String hmac_sha1(String value, String key) {

try {

// Get an hmac_sha1 key from the raw key bytes

byte[] keyBytes = key.getBytes();

SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");

// Get an hmac_sha1 Mac instance and initialize with the signing key

Mac mac = Mac.getInstance("HmacSHA1");

mac.init(signingKey);

// Compute the hmac on input data bytes

byte[] rawHmac = mac.doFinal(value.getBytes());

// Convert raw bytes to Hex

String hexBytes = byte2hex(rawHmac);

return hexBytes;

} catch (Exception e) {

throw new RuntimeException(e);

}

}

private static String byte2hex(final byte[] b){

String hs="";

String stmp="";

for (int n=0; nb.length; n++){

stmp=(java.lang.Integer.toHexString(b[n] 0xFF));

if (stmp.length()==1) hs=hs+"0"+stmp;

else hs=hs+stmp;

}

return hs;

}

private static String appendEqualSign(String s){

int len = s.length();

int appendNum = 8 - (int)(len/8);

for (int n=0; nappendNum; n++){

s += "%3D";

}

return s;

}

}

参考:

函数HMAC-SHA1

HMAC

根据RFC 2316(Report of the IAB,April 1998),HMAC(散列消息身份验证码: Hashed Message Authentication Code)以及IPSec被认为是Interact安全的关键性核心协议。它不是散列函数,而是采用了将MD5或SHA1散列函数与共享机密密钥(与公钥/私钥对不同)一起使用的消息身份验证机制。基本来说,消息与密钥组合并运行散列函数。然后运行结果与密钥组合并再次运行散列函数。这个128位的结果被截断成96位,成为MAC.

hmac主要应用在身份验证中,它的使用方法是这样的:

1. 客户端发出登录请求(假设是浏览器的GET请求)

2. 服务器返回一个随机值,并在会话中记录这个随机值

3. 客户端将该随机值作为密钥,用户密码进行hmac运算,然后提交给服务器

4. 服务器读取用户数据库中的用户密码和步骤2中发送的随机值做与客户端一样的hmac运算,然后与用户发送的结果比较,如果结果一致则验证用户合法

在这个过程中,可能遭到安全攻击的是服务器发送的随机值和用户发送的hmac结果,而对于截获了这两个值的黑客而言这两个值是没有意义的,绝无获取用户密码的可能性,随机值的引入使hmac只在当前会话中有效,大大增强了安全性和实用性。大多数的语言都实现了hmac算法,比如php的mhash、python的hmac.py、java的MessageDigest类,在web验证中使用hmac也是可行的,用js进行md5运算的速度也是比较快的。

SHA

安全散列算法SHA (Secure Hash Algorithm)是美国国家标准和技术局发布的国家标准FIPS PUB 180-1,一般称为SHA-1。其对长度不超过264二进制位的消息产生160位的消息摘要输出,按512比特块处理其输入。

SHA是一种数据加密算法,该算法经过加密专家多年来的发展和改进已日益完善,现在已成为公认的最安全的散列算法之一,并被广泛使用。该算法的思想是接收一段明文,然后以一种不可逆的方式将它转换成一段(通常更小)密文,也可以简单的理解为取一串输入码(称为预映射或信息),并把它们转化为长度较短、位数固定的输出序列即散列值(也称为信息摘要或信息认证代码)的过程。散列函数值可以说时对明文的一种“指纹”或是“摘要”所以对散列值的数字签名就可以视为对此明文的数字签名。

HMAC_SHA1

HMAC_SHA1(Hashed Message Authentication Code, Secure Hash Algorithm)是一种安全的基于加密hash函数和共享密钥的消息认证协议。它可以有效地防止数据在传输过程中被截获和篡改,维护了数据的完整性、可靠性和安全性。HMAC_SHA1消息认证机制的成功在于一个加密的hash函数、一个加密的随机密钥和一个安全的密钥交换机制。

HMAC_SHA1 其实还是一种散列算法,只不过是用密钥来求取摘要值的散列算法。

HMAC_SHA1算法在身份验证和数据完整性方面可以得到很好的应用,在目前网络安全也得到较好的实现。

php hash_hmac如何解密

hmac算法的主体还是散列函数,散列算法本身是抽取数据特征,是不可逆的。

所以“再得到aaa”——“逆运算获得原数据”这种想法,是不符合hmac设计初衷,可以看成是对hmac安全性的直接挑战,属于解密,属于误用。

类似的需求,应该使用AES加密算法实现

hmacsha1php的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、hmacsha1php的信息别忘了在本站进行查找喔。

发布于 2023-04-05 14:04:54
收藏
分享
海报
36
目录

    忘记密码?

    图形验证码

    复制成功
    微信号: cloud7591
    如需了解更多,欢迎添加客服微信咨询。
    我知道了