ios – 获取MD5和SHA-1

前端之家收集整理的这篇文章主要介绍了ios – 获取MD5和SHA-1前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找一些帮助,在我的iPhone应用程序中获取MD5和SHA-1.
有人可以给我一个如何获得这些的想法吗?

解决方法

#include <CommonCrypto/CommonDigest.h>

-(NSString*) sha1:(NSString*)input
{

 NSData *data = [input dataUsingEncoding: NSUTF8StringEncoding]; 

 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;

}

- (NSString *) md5:(NSString *) input
{
 const char *cStr = [input UTF8String];
 unsigned char digest[CC_MD5_DIGEST_LENGTH];
 CC_MD5( cStr,(CC_LONG)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;

}

也看看我的博文在这里 –
http://www.makebetterthings.com/blogs/iphone/how-to-get-md5-and-sha1-in-objective-c-ios-sdk/

原文链接:https://www.f2er.com/iOS/330471.html

猜你在找的iOS相关文章