php 到 iphone 代码 - CCHmac kCCHmacAlgSHA256
我正在尝试使用 hmac sha256 加密登录到我的服务器,我在 php 中有工作代码,但无法让它在 iphone 中工作,并追踪到 iphone 中的 hmac 在给定相同输入的情况下对 php 代码产生不同的输出 php代码是
$privatekey = '6-y6f"\%BjSM;HBo\'sPr")5#t2nb-LG*;])f^Si[';
$identity_arrow_getSecret = $privatekey;
$date_c = "2011-04-18T23:56:28+0800";
$uri = '/backend/1/User/Header';
$stringToSign = "GET\n\n\n" . $date_c . "\n" . $uri;
$signature = hash_hmac("sha256", utf8_encode($stringToSign), $identity_arrow_getSecret);
echo "stringToSign is $stringToSign <HR>";
echo "signature is $signature <HR>";
objective-c代码是
NSString* uri = @"/backend/1/User/Header";
NSString* date_c = @"2011-04-18T23:56:28+0800"; //[dateFormatter stringFromDate:[NSDate date]];
NSString* stringToSign = [NSString stringWithFormat:@"GET\n\n\n%@\n%@" , date_c , uri];
NSLog(@" stringToSign : %@ <>\r\n", stringToSign);
NSString* privatekey = @"6-y6f\"\%BjSM;HBo\'sPr\")5#t2nb-LG*;])f^Si[";
const char *cKey = [privatekey cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [stringToSign cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSString *hash = [HMAC base64EncodedString];
NSLog(@" hash : %@ \r\n", hash);
I am trying to login to my server using hmac sha256 encryption, i have working code in php, but can't get it working in iphone and traced it to that the hmac in iphone is yielding different output to php code, given same inputs
php code is
$privatekey = '6-y6f"\%BjSM;HBo\'sPr")5#t2nb-LG*;])f^Si[';
$identity_arrow_getSecret = $privatekey;
$date_c = "2011-04-18T23:56:28+0800";
$uri = '/backend/1/User/Header';
$stringToSign = "GET\n\n\n" . $date_c . "\n" . $uri;
$signature = hash_hmac("sha256", utf8_encode($stringToSign), $identity_arrow_getSecret);
echo "stringToSign is $stringToSign <HR>";
echo "signature is $signature <HR>";
objective-c code is
NSString* uri = @"/backend/1/User/Header";
NSString* date_c = @"2011-04-18T23:56:28+0800"; //[dateFormatter stringFromDate:[NSDate date]];
NSString* stringToSign = [NSString stringWithFormat:@"GET\n\n\n%@\n%@" , date_c , uri];
NSLog(@" stringToSign : %@ <>\r\n", stringToSign);
NSString* privatekey = @"6-y6f\"\%BjSM;HBo\'sPr\")5#t2nb-LG*;])f^Si[";
const char *cKey = [privatekey cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [stringToSign cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSString *hash = [HMAC base64EncodedString];
NSLog(@" hash : %@ \r\n", hash);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想检查您的 Base64 类。我使用 Kiichi Takeuchi 编写的 Base64 类给我的结果与我用 C# 编写的例程进行验证相同,因此我认为它是正确的。
我必须对您的代码进行一个小更改才能进行验证,因为 Base64 库仅对 NSData 结构进行编码。它看起来是这样的:
You may want to check your Base64 class. I use the Base64 class written by Kiichi Takeuchi and it gives me identical results to a routine I wrote in C# to verify, so I assume it's correct.
I had to make one small change to your code to verify, as the Base64 library only encodes an NSData structure. Here's what it looks like: