简体   繁体   English

Facebook签署iOS请求(HMAC SHA256)

[英]Facebook Signed Request for iOS (HMAC SHA256)

I've been trying to generate HMAC SHA256 for Facebook signed request on iOS for weeks now. 我一直在努力为iOS上的Facebook签名请求生成HMAC SHA256几周。 I am desperate need of help. 我迫切需要帮助。

Facebook signed requests have two parts which are separated by a period. Facebook签署的请求有两个部分,由一个句点分隔。 First part is an HMAC256 of the payload while the 2nd part is Base64 encoded string of the payload. 第一部分是有效载荷的HMAC256,而第二部分是有效载荷的Base64编码串。 I've only been able to recreate the second part. 我只能重新创建第二部分。

vlXgu64BQGFSQrY0ZcJBZASMvYvTHu9GQ0YM9rjPSso.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsIjAiOiJwYXlsb2FkIn0

I've been using the following code that everybody uses but it's generating a different hash: 我一直在使用以下代码,每个人都使用它,但它生成一个不同的哈希:

#import <CommonCrypto/CommonHMAC.h>
#import "NSData+Base64.h"

+(NSString*) hmacForSecret:(NSString*)secret data:(NSString*)data {

     const char *cKey  = [secret cStringUsingEncoding:NSASCIIStringEncoding];
     const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];
     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)];

     return [HMAC base64EncodedString];
}

According to Facebook docs: https://developers.facebook.com/docs/authentication/signed_request/ 根据Facebook文档: https//developers.facebook.com/docs/authentication/signed_request/

Correct HMAC256 output should be when using " secret " as the key: 当使用“ secret ”作为密钥时,应该正确输入HMAC256:

 vlXgu64BQGFSQrY0ZcJBZASMvYvTHu9GQ0YM9rjPSso

The payload to be encoded: 要编码的有效负载:

 {
     "algorithm": "HMAC-SHA256",
     "0": "payload"
 }

NOTE: You can Base64 decode the 2nd part of the signed request to get this payload. 注意:您可以Base64解码签名请求的第二部分以获取此有效负载。

The problem was with the Base64 encoder. 问题出在Base64编码器上。 It needs to be encoded as Base64Url see: http://en.wikipedia.org/wiki/Base64#URL_applications 它需要编码为Base64Url,请参阅: http//en.wikipedia.org/wiki/Base64#URL_applications

Here's the modifed base64EncodedString category method: 这是修改后的base64EncodedString类别方法:

//NSData+Base64.h
 - (NSString *)base64EncodedString
 {
    size_t outputLength;

    char *outputBuffer = NewBase64Encode([self bytes], [self length], true, &outputLength);
    NSString *result = [[[NSString alloc] initWithBytes:outputBuffer length:outputLength encoding:NSASCIIStringEncoding] autorelease];
    free(outputBuffer);

     NSString *b64PayloadClean = [[result componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@""];

     //do URL encoding by replacing "+" and "/" to "-" and "_" respectively
     b64PayloadClean = [b64PayloadClean stringByReplacingOccurrencesOfString:@"=" withString:@""];
     b64PayloadClean = [b64PayloadClean stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
     b64PayloadClean = [b64PayloadClean stringByReplacingOccurrencesOfString:@"/" withString:@"_"];

    return b64PayloadClean;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM