我需要帮助从iPhone应用程序获得正确的Azure表存储请求标题。
我一直在用这两篇文章来尝试对密钥进行加密,但我仍然有问题:
iPhone and HMAC-SHA-1 encoding
Objective-C sample code for HMAC-SHA1
服务为我的请求返回一个错误:
服务器验证请求失败,出现此错误:
Make sure the value of Authorization header is formed correctly including the signature.
我使用以下代码提出请求:
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [self rfc1123String:now];
NSString *messageToSign = [NSString stringWithFormat:@"%@\n/%@/%@", dateString, AZURE_ACCOUNT_NAME, table];
[Base64 initialize];
//xxx in my code is my primary access shared key
NSString *key = @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding];
const char *cData = [messageToSign cStringUsingEncoding:NSUTF8StringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
NSString *hash = [Base64 encode:HMAC];
NSLog(@"Encoded hash: %@", hash);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request addValue:[NSString stringWithFormat:@"SharedKeyLite %@:%@", AZURE_ACCOUNT_NAME, hash] forHTTPHeaderField:@"Authorization"];
[request addValue:dateString forHTTPHeaderField:@"x-ms-date"];
[request addValue:@"application/atom+xml, application/xml" forHTTPHeaderField:@"Accept"];
[request addValue:@"UTF-8" forHTTPHeaderField:@"Accept-Charset"];
NSLog(@"Headers: %@", [request allHTTPHeaderFields]);
NSLog(@"URL: %@", [[request URL] absoluteString]);
return request;
这将导致为请求生成这些头文件:
Accept = "application/atom+xml, application/xml";
"Accept-Charset" = "UTF-8";
Authorization = "SharedKeyLite powderdayalarm:xwT1purDtREtxauVr6Bhvdz/2ObLh2J0lMw/prBBQBE=";
"X-Ms-Date" = "Fri, 05 Nov 2010 18:26:00 GMT";
请求的规格如下:
http://msdn.microsoft.com/en-us/library/dd179428.aspx
指的是:
此格式支持共享密钥和
所有版本的共享密钥Lite
表服务和共享密钥Lite
对于2009年0919版的BLUB
和队列服务。这种格式是
与以前使用的相同
存储服务的版本。
构造规范化的资源
字符串的格式如下:
-
以空字符串(“”)开头,附加正斜杠(/),
其次是账户名称
拥有正在访问的资源的。
-
附加资源的编码URI路径。如果请求的URI地址
资源的一个组件,append
适当的查询字符串。这个
查询字符串应包括
问号和comp参数
(例如,?)comp=元数据)。不
其他参数应包括在
查询字符串。
对签名进行编码
要对签名进行编码,请调用
上的HMAC-SHA256算法
utf-8编码的签名字符串和
将结果编码为base64。使用
以下格式(如图所示)
伪代码):复制
签名=base64(hmac-sha256(utf8(stringtosign)))
我似乎无法找到这个问题的根源。而且似乎没有太多人从iPhone发出Azure请求:)。
谢谢
斯科特