20 md 5暗号化
2230 ワード
ヘッダファイルh
実装ファイルm
// 16 32 8
#import
@interface MD5 : NSObject
{
}
// 32 md5
+ (NSString*)md532BitLower:(NSString *)inPutText;
// 32 md5
+ (NSString*)md532BitUpper:(NSString*)inPutText;
+ (NSString *)md5HexDigest:(NSString*)input;
@end
実装ファイルm
#import "MD5.h"
#import
@implementation MD5
/**
* 32 md5
*
* @param inPutText
*
* @return 32 md5
*/
+ (NSString*)md532BitLower:(NSString *)inPutText
{
const char *cStr = [inPutText UTF8String];
unsigned char result[16];
CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
return [[NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
] lowercaseString];
}
/**
* 32 md5
*
* @param inPutText
*
* @return 32 md5
*/
+ (NSString*)md532BitUpper:(NSString*)inPutText
{
const char *cStr = [inPutText UTF8String];
unsigned char result[16];
CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
return [[NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
] uppercaseString];
}
+ (NSString *)md5HexDigest:(NSString*)input
{
const char* str = [input UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(str, (CC_LONG)strlen(str), result);
NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH];
for(int i = 0; i