iOS使用GBK编码的hmacMD5算法

+ (NSString *)getHmacmd5:(NSString *)clearTextwithSecret:    (NSString *)secret
{
   CCHmacContext ctx;
  //使用GBK编码
  unsigned long encode =   CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);

  const char *key = [secret cStringUsingEncoding:encode];
  const char *str = [clearText cStringUsingEncoding:encode];
  unsigned char mac[CC_MD5_DIGEST_LENGTH];
  char hexmac[2 * CC_MD5_DIGEST_LENGTH + 1];
  char *p;
  CCHmacInit(&ctx, kCCHmacAlgMD5, key, strlen(key));
  CCHmacUpdate(&ctx, str, strlen(str));
  CCHmacFinal(&ctx, mac);
  p = hexmac;
  for (int i = 0; i < CC_MD5_DIGEST_LENGTH;i++) {
      snprintf(p,3,"%02x", mac[ i ]);
      p += 2;
  }
  return [NSString stringWithCString:hexmac encoding:encode];
}

你可能感兴趣的:(iOS使用GBK编码的hmacMD5算法)