博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to get md5 and SHA1 in objective c (iOS sdk)
阅读量:4596 次
发布时间:2019-06-09

本文共 1247 字,大约阅读时间需要 4 分钟。

Calculating the md5 and sha1 hash in iOS sdk is pretty simple -

Step 1 – The very first thing you need to do is import CommonCrypto’s CommonDigest.h

1
#import <CommonCrypto/CommonDigest.h>

Step 2 – Here is the real code for calculating SHA1 and MD5 hash -

SHA1 -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-(
NSString
*) sha1:(
NSString
*)input
{
 
const
char
*cstr = [input cStringUsingEncoding:
NSUTF8StringEncoding
];
 
NSData
*data = [
NSData
dataWithBytes:cstr length:input.length];
 
 
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
 
 
CC_SHA1(data.bytes, data.length, digest);
 
 
NSMutableString
* output = [
NSMutableString
stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
 
 
for
(
int
i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
 
[output appendFormat:
@"%02x"
, digest[i]];
 
 
return
output;
 
}

MD5 -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (
NSString
*) md5:(
NSString
*) input
{
 
const
char
*cStr = [input UTF8String];
 
unsigned
char
digest[16];
 
CC_MD5( cStr, strlen(cStr), digest );
// This is the md5 call
 
 
NSMutableString
*output = [
NSMutableString
stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
 
 
for
(
int
i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
 
[output appendFormat:
@"%02x"
, digest[i]];
 
 
return 
output;
 
}

Hope it will help someone!

转载于:https://www.cnblogs.com/hwei/p/3634492.html

你可能感兴趣的文章
linux基础6-bash shell编程
查看>>
php 语法
查看>>
回顾MySpace架构的坎坷之路
查看>>
ubuntu系统无eth0网卡解决办法
查看>>
六.计算机网络互联基础
查看>>
JS兼容各个浏览器的本地图片上传即时预览效果
查看>>
JS编写日历控件(支持单日历 双日历 甚至多日历等)
查看>>
### 学习《C++ Primer》- 6
查看>>
IOS中实现单例
查看>>
Math 对象
查看>>
[luoguP1877] [HAOI2012]音量调节(DP)
查看>>
重磅 | 2017年深度学习优化算法研究亮点最新综述火热出炉
查看>>
clipboard.js 介绍
查看>>
(二)程序中的内存&&栈
查看>>
一个实例来见证LINGO的强大
查看>>
C# — WinForm TCP连接之服务器端
查看>>
HTML8
查看>>
asp.net 导出excel 以及插入图片
查看>>
揭密Google Map的工作原理(转)
查看>>
掌握这几种微服务模式助你成为更出色的工程师
查看>>