我正在尝试从 Objective C(CCHmac) 和 PHP (hash_hmac) 实现 HMAC 身份验证。我得到了不同的哈希结果。有没有人可以帮助我?我不知道为什么要得到不同的哈希结果?以下是我的代码(Objective-c & PHP)供您参考。请!
PHP:
$APIConsumerSecret ="DcmzvkQC7Sno+lxnbDG0hTtZ0WTQn9T2T9DJxEmcB0";
$APIConsumerSecret = urlencode($APIConsumerSecret).'&';
$BaseString="GET&http%3A%2F%2Fboday.api.simppo.com%2Foauth%2Frequest_token&oauth_consumer_key%3DCN6W1I8E2CEWZJNQI2KA7KY3%26oauth_nonce%3DlxTSJL%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1357268295%26oauth_version%3D1.0";
echo base64_encode(hash_hmac('sha1',$BaseString,$APIConsumerSecret,true));
PHP结果:xJ5Ya4u4ghH4ugIieGIb9AcFpD0=
Objective-C
- (NSString*)flickr_oauthSignatureFor:(NSString*)dataString withKey:(NSString*)secret
{
NSData* secretData = [secret dataUsingEncoding:NSUTF8StringEncoding];
NSData* stringData = [dataString dataUsingEncoding:NSUTF8StringEncoding];
const void* keyBytes = [secretData bytes];
const void* dataBytes = [stringData bytes];
///#define CC_SHA1_DIGEST_LENGTH 20 /* digest length in bytes */
void* outs = malloc(CC_SHA1_DIGEST_LENGTH);
CCHmac(kCCHmacAlgSHA1, keyBytes, [secretData length], dataBytes, [stringData length], outs);
// Soluion 1
NSData* signatureData = [NSData dataWithBytesNoCopy:outs length:CC_SHA1_DIGEST_LENGTH freeWhenDone:YES];
NSLog(@"D1-0: signatureData %@", signatureData );
NSLog(@"base64:%@", [signatureData base64EncodedString] );
return [signatureData base64EncodedString];
}
Objective-C 结果:
D1-0: <59052771 e670a04b 3a2e87db 3d7965be 1aed112e>
base64: WQUnceZwoEs6LofbPXllvhrtES4=