MD5/SHA1/Hmac_SHA1
2024-10-07 19:59:34
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;
}
最新文章
- Linux下安装Tomcat服务器和部署Web应用
- Myeclipse运行报错:an out of memory error has occurred的解决方法
- java程序员保持天天快乐的6个习惯
- SqlServer--模糊查询-通配符
- 一些有意思的VR设备介绍
- JQuery_DOM 节点操作之包裹节点
- 在html中关于如果function的函数名和input的name一样会发生怎样的现象
- JavaScript学习笔记-用于模式匹配的String方法
- BZOJ 1001 题解
- 2016C#模拟谷歌Google登陆Gmail&;Youtube小案例
- Inno Setup 教程
- android MotionEvent中getX()和getRawX()的区别
- linux VM命令下查找
- Equivalent Strings (字符串相等?)
- springmvc+mongodb+maven 项目搭建配置
- 四、oracle 用户管理(Profile)
- 内置函数--global() 和 local()
- Spring MVC 使用介绍(五)—— 注解式控制器(一):基本介绍
- url 中文及特殊字符转码
- Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡
热门文章
- linux进阶之路(三):vi/vim编辑器
- [参考]C# JSON字符串序列化与反序列化
- git分布式版本控制系统权威指南学习笔记(六):git reset、get stash、git checkout总结
- 1、Locust压力测试环境搭建
- MarkDown 快速开始 基础教学
- Kali Linux 2018 更新源配置
- C#内嵌Python架构实现
- 根据不同运行环境配置和组织node.js应用
- layui 封装自定义模块
- creat-react-app搭建的项目中按需引入antd以及配置Less和如何修改antd的主题色