1、MD5

#import <CommonCrypto/CommonDigest.h>
+ (NSString *) md5:(NSString *) input {
const char *cStr = [input UTF8String];
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, (unsigned int)strlen(cStr), digest ); // This is the md5 call
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * ];
for(int i = ; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]];
return output;
}

2、SHA1

+ (NSString *) sha1:(NSString *)input{
NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding]; uint8_t digest[CC_SHA1_DIGEST_LENGTH]; CC_SHA1(data.bytes, (unsigned int)data.length, digest); NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * ]; for(int i=; i<CC_SHA1_DIGEST_LENGTH; i++) {
[output appendFormat:@"%02x", digest[i]];
} return output;
}

3、HmacSHA1

//额外添加了base64转码
+(NSString *)Base_HmacSha1:(NSString *)key data:(NSString *)data{
  const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding];
  const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];
  //Sha256:
  // unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
  //CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);

  //sha1
  unsigned char cHMAC[CC_SHA1_DIGEST_LENGTH];
  CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);

  NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];

  //将加密结果进行一次BASE64编码。
  NSString *hash = [HMAC base64EncodedStringWithOptions:0];
  return hash;
}

最新文章

  1. Linux下安装Tomcat服务器和部署Web应用
  2. Myeclipse运行报错:an out of memory error has occurred的解决方法
  3. java程序员保持天天快乐的6个习惯
  4. SqlServer--模糊查询-通配符
  5. 一些有意思的VR设备介绍
  6. JQuery_DOM 节点操作之包裹节点
  7. 在html中关于如果function的函数名和input的name一样会发生怎样的现象
  8. JavaScript学习笔记-用于模式匹配的String方法
  9. BZOJ 1001 题解
  10. 2016C#模拟谷歌Google登陆Gmail&amp;Youtube小案例
  11. Inno Setup 教程
  12. android MotionEvent中getX()和getRawX()的区别
  13. linux VM命令下查找
  14. Equivalent Strings (字符串相等?)
  15. springmvc+mongodb+maven 项目搭建配置
  16. 四、oracle 用户管理(Profile)
  17. 内置函数--global() 和 local()
  18. Spring MVC 使用介绍(五)—— 注解式控制器(一):基本介绍
  19. url 中文及特殊字符转码
  20. Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡

热门文章

  1. linux进阶之路(三):vi/vim编辑器
  2. [参考]C# JSON字符串序列化与反序列化
  3. git分布式版本控制系统权威指南学习笔记(六):git reset、get stash、git checkout总结
  4. 1、Locust压力测试环境搭建
  5. MarkDown 快速开始 基础教学
  6. Kali Linux 2018 更新源配置
  7. C#内嵌Python架构实现
  8. 根据不同运行环境配置和组织node.js应用
  9. layui 封装自定义模块
  10. creat-react-app搭建的项目中按需引入antd以及配置Less和如何修改antd的主题色