本文介绍了如何SHA哈希可可/ iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定消息和salt如何编码它返回散列的字符串?

Given the message and the salt how can I encode it returning the hashed string?

我需要重现php函数:

I need reproduce the php function:

hash_hmac('sha256','message','salt');

感谢

推荐答案

找到答案:

#import <CommonCrypto/CommonHMAC.h>

-(NSString *) hashString :(NSString *) data withSalt: (NSString *) salt {


   const char *cKey  = [salt cStringUsingEncoding:NSUTF8StringEncoding];
   const char *cData = [data cStringUsingEncoding:NSUTF8StringEncoding];
   unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
   CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);

   NSString *hash;

   NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];

   for(int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++)
       [output appendFormat:@"%02x", cHMAC[i]];
   hash = output;
   return hash;

}

这篇关于如何SHA哈希可可/ iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 15:13