Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Objective-C中获取QuickBlox令牌_Ios_Objective C_Token_Quickblox_Hmacsha1 - Fatal编程技术网

Ios 如何在Objective-C中获取QuickBlox令牌

Ios 如何在Objective-C中获取QuickBlox令牌,ios,objective-c,token,quickblox,hmacsha1,Ios,Objective C,Token,Quickblox,Hmacsha1,由于工作限制,必须使用Quickblox的RESTful API而不是iOS SDK,并且根据生成有效签名时遇到问题 以下是我从电话中得到的回复: 2014-07-25 16:19:12.646 test[2247:60b]response: <NSHTTPURLResponse: 0x10c41ae40> { URL:https://api.quickblox.com/session.json } { status code: 422, headers { "Access-Cont

由于工作限制,必须使用Quickblox的RESTful API而不是iOS SDK,并且根据生成有效签名时遇到问题

以下是我从电话中得到的回复:

2014-07-25 16:19:12.646 test[2247:60b]response: <NSHTTPURLResponse: 0x10c41ae40> { URL:https://api.quickblox.com/session.json } { status code: 422, headers {
"Access-Control-Allow-Origin" = "*";
"Cache-Control" = "no-cache";
Connection = "keep-alive";
"Content-Type" = "application/json; charset=utf-8";
Date = "Fri, 25 Jul 2014 23:19:12 GMT";
"QuickBlox-REST-API-Version" = "0.1.1";
Server = "nginx/1.0.15";
Status = "422 Unprocessable Entity";
"Transfer-Encoding" = Identity;
"X-Rack-Cache" = "invalidate, pass";
"X-Request-Id" = 8413fb7182cee06857619b14f363ed78;
"X-Runtime" = "0.004821";
"X-UA-Compatible" = "IE=Edge,chrome=1";
} } data: {"errors":{"base":["Unexpected signature"]}} connectionError: (null)
最后,我的哈希方法:

- (NSString *)hmacsha1:(NSString *)data secret:(NSString *)key {

const char *cKey  = [key cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA1_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
NSString *hash = [HMAC base64EncodedStringWithOptions:0];

return hash;
}

看起来我生成签名的方式不好,但不确定我把事情搞砸了。有什么建议吗?

这里有一个合适的签名生成方法:

+ (NSString *)signData:(NSData *)data withSecret:(NSString *)secret{
    NSData *secretData = [secret dataUsingEncoding:NSUTF8StringEncoding];
    NSData *clearTextData = data;
    uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
    CCHmacContext hmacContext;
    CCHmacInit(&hmacContext, kCCHmacAlgSHA1, secretData.bytes, secretData.length);
    CCHmacUpdate(&hmacContext, clearTextData.bytes, clearTextData.length);
    CCHmacFinal(&hmacContext, digest);
    NSData *result = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
    NSString *hash = [result description];
    hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
    hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
    hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];

    return hash;
}

+ (NSString *)signData:(NSData *)data withSecret:(NSString *)secret{
    NSData *secretData = [secret dataUsingEncoding:NSUTF8StringEncoding];
    NSData *clearTextData = data;
    uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
    CCHmacContext hmacContext;
    CCHmacInit(&hmacContext, kCCHmacAlgSHA1, secretData.bytes, secretData.length);
    CCHmacUpdate(&hmacContext, clearTextData.bytes, clearTextData.length);
    CCHmacFinal(&hmacContext, digest);
    NSData *result = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
    NSString *hash = [result description];
    hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
    hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
    hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];

    return hash;
}
[SignHelper signData:[self rawBodyWithoutEncode] withSecret:[QBSettings authorizationSecret]]


- (NSData *)rawBodyWithoutEncode
{
    NSData *raw = nil;

    NSMutableString *params = [[NSMutableString alloc] init];

    // sort
    NSMutableArray *sortKeys = [NSMutableArray arrayWithArray:[parameters allKeys]];
    [sortKeys sortUsingSelector:@selector(compare:)];

    for (id s in sortKeys){
        [params appendFormat:@"%@=%@&", s, [parameters objectForKey:s]];
    }
    [params deleteCharactersInRange:NSMakeRange([params length] - 1, 1)];

    raw = [params dataUsingEncoding:NSUTF8StringEncoding];

    return raw;
}
NSMutableDictionary *params = [NSMutableDictionary 
                               dictionary];
NSUInteger appID = [QBSettings applicationID];
NSString *authKey = [QBSettings authorizationKey];
NSUInteger nonce = arc4random()%1000;
NSUInteger timestamp = [[NSDate date] timeIntervalSince1970];

[params setValue:[NSString stringWithFormat:@"%lu",(unsigned long)appID] forKey:@"application_id"];
[params setValue:authKey forKey:@"auth_key"];
[params setValue:[NSString stringWithFormat:@"%lu",(unsigned long)nonce] forKey:@"nonce"];
[params setValue:[NSString stringWithFormat:@"%lu",(unsigned long)timestamp] forKey:@"timestamp"];