php 到 iphone 代码 - CCHmac kCCHmacAlgSHA256

发布于 2024-11-02 07:59:00 字数 1336 浏览 5 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿哥们儿 2024-11-09 07:59:00

您可能想检查您的 Base64 类。我使用 Kiichi Takeuchi 编写的 Base64 类给我的结果与我用 C# 编写的例程进行验证相同,因此我认为它是正确的。

我必须对您的代码进行一个小更改才能进行验证,因为 Base64 库仅对 NSData 结构进行编码。它看起来是这样的:

CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSData *nsd = [[NSData alloc] initWithBytes: cHMAC length:CC_SHA256_DIGEST_LENGTH];

NSString *hash = [Base64 encode:nsd];
[nsd release];
NSLog(@" hash : %@ \r\n", hash);

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:

CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSData *nsd = [[NSData alloc] initWithBytes: cHMAC length:CC_SHA256_DIGEST_LENGTH];

NSString *hash = [Base64 encode:nsd];
[nsd release];
NSLog(@" hash : %@ \r\n", hash);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文