How to get md5 and SHA1 in objective c (iOS sdk)
2015-08-18 11:21
477 查看
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
Step 2 – Here is the real code for calculating SHA1 and MD5 hash -
SHA1 -
MD5 -
Step 1 – The very first thing you need to do is import CommonCrypto’s CommonDigest.h
#import <CommonCrypto/CommonDigest.h>
Step 2 – Here is the real code for calculating SHA1 and MD5 hash -
SHA1 -
-(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 -
- (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; }
相关文章推荐
- 使用Xcode HeaderDoc和Doxygen文档化你的Objective-C和Swift代码
- Component Object Model
- 深入理解Objective-C:方法缓存
- Objective-C属性
- 我的模拟OC测试
- Objective-C Associated Objects 的实现原理
- var, object, dynamic的区别以及使用(转载)
- 串口之WaitForSingleObjec、WaitForMultipleObjects函数详解
- 串口之WaitForSingleObjec、WaitForMultipleObjects函数详解
- Objective-C的属性和成员变量用法及关系浅析
- 2015 Objective-C 三大新特性
- JSONObject对象的解析
- objective-c中如何从UIImage中获取像素信息
- objective-c中类的申明、实现、访问
- Kotlin教程学习-dataclass,objectclass,use函数,类扩展,socket
- Objective-C( 三、方法的声明与实现)
- Objective-C Runtime 运行时之二:成员变量与属性
- Objective-C Runtime 运行时之三:方法与消息
- objective-c类目Category属性的实现方法
- underscore objects