溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iOS SDK計算SHA1和MD5

發(fā)布時間:2020-07-10 11:02:08 來源:網(wǎng)絡(luò) 閱讀:337 作者:wcrane 欄目:移動開發(fā)
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問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI