Using CommonCrypto/CommonHMAC to encrypt some data and always comes back null

大兔子大兔子 提交于 2019-12-06 06:07:45

This data from the crypto is data and attempting to turn it into string is failing on the encoding. You are specifying UTF8 encoding and I have also tried UTF32 encoding and that fails as well. Just log the data returned as those hex values are more beneficial than a string representation.

If you would still like to see as much as the string as possible you can do this.

NSData *output = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];

    //This is useful
NSLog(@"encrypted data: %@", output);

    //Not useful but you may be able to visualize some of the string
char *outstr = malloc(sizeof(char) * (CC_SHA1_DIGEST_LENGTH + 1));
memcpy(outstr, [output bytes], CC_SHA1_DIGEST_LENGTH);
outstr[CC_SHA1_DIGEST_LENGTH] = 0;
NSLog(@"encrypted data string: %s", outstr);
free(outstr);

And I also had some success with the following line.(Prints a different string than above)

NSLog(@"encrypted data: %@", [[[NSString alloc] initWithData:output encoding:NSISOLatin2StringEncoding] autorelease]);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!