xcode ios HMAC SHA 256 hashing

后端 未结 3 1829
星月不相逢
星月不相逢 2021-02-01 20:30

So I\'m trying to figure out how to do a hmacshad256 hash on ios as that\'s the hash I did for the wcf service api I made. I\'ve been trying to look for some info about it but w

3条回答
  •  醉话见心
    2021-02-01 21:03

    + (NSString *)hmacSHA256EncryptString{
    
    
        NSString * parameterSecret = @"input secret key";
        NSString *plainString = @"input encrypt content string";
        const char *secretKey  = [parameterSecret cStringUsingEncoding:NSUTF8StringEncoding];
        const char *plainData = [plainString cStringUsingEncoding:NSUTF8StringEncoding];
        unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
        CCHmac(kCCHmacAlgSHA256, secretKey, strlen(secretKey), plainData, strlen(plainData), cHMAC);
        NSData *HMACData = [NSData dataWithBytes:cHMAC length:sizeof(cHMAC)];
        const unsigned char *bufferChar = (const unsigned char *)[HMACData bytes];
        NSMutableString *hmacString = [NSMutableString stringWithCapacity:HMACData.length * 2];
        for (int i = 0; i < HMACData.length; ++i){
            [hmacString appendFormat:@"%02x", bufferChar[i]];
        }
        return hmacString;
        
    }

提交回复
热议问题