温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS SDK计算SHA1和MD5

发布时间:2020-07-10 11:02:08 来源:网络 阅读:331 作者:wcrane 栏目:移动开发
How to get md5 and SHA1 in objective c (iOS sdk)

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 -


?

戴维营教育代码
01
02
03
04
05
06
07
08
09
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 -

?

戴维营教育代码
01
02
03
04
05
06
07
08
09
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!!

via:
http://www.makebetterthings.com/ ... bjective-c-ios-sdk/

iOS SDK计算SHA1和MD5收藏0


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI