HMAC SHA-512 generation discrepancies between iOS and javascript libraries

后端 未结 1 1671
萌比男神i
萌比男神i 2021-01-15 17:40

I am trying to replicate the Hmac generation behavior from my iOS app using SHA-512 algorithm referring to this link Objective-C sample code for HMAC-SHA1.

For this

1条回答
  •  别那么骄傲
    2021-01-15 17:54

    HMAC takes a data key and a key and data parameters which are bytes and returns bytes of a length determined by the hash function specified.

    Example:

    + (NSData *)doHmacSha512:(NSData *)dataIn
                         key:(NSData *)key
    {
        NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA512_DIGEST_LENGTH];
    
        CCHmac( kCCHmacAlgSHA512,
                key.bytes,
                key.length,
                dataIn.bytes,
                dataIn.length,
                macOut.mutableBytes);
    
        return macOut;
    }
    

    Test:

    NSData *keyData  = [@"MyTestKey" dataUsingEncoding:NSASCIIStringEncoding];
    NSData *data     = [@"Now is the time for all good computers to come to the aid of their masters." dataUsingEncoding:NSASCIIStringEncoding];
    NSData *hamcData = [Crypto doHmacSha512:data key:keyData]; // Where "Crypto" is the class "doHmacSha512" is defined in.
    

    0 讨论(0)
提交回复
热议问题