Getting MD5 and SHA-1

£可爱£侵袭症+ 提交于 2020-01-22 07:33:10

问题


I am looking for some help in getting MD5 and SHA-1 in my iPhone app. Can anybody give me an idea on how to get these?


回答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;

}

also have a look at my blog post here - http://www.makebetterthings.com/blogs/iphone/how-to-get-md5-and-sha1-in-objective-c-ios-sdk/



来源:https://stackoverflow.com/questions/6006743/getting-md5-and-sha-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!