목표 C의 MD5 알고리즘
Objective-C에서 MD5를 계산하는 방법
md5는 iPhone에서 사용할 수 있으며, ie의 추가 기능으로 추가할 수 있습니다.NSString
그리고.NSData
아래와 같이.
MyAdditions.h
@interface NSString (MyAdditions)
- (NSString *)md5;
@end
@interface NSData (MyAdditions)
- (NSString*)md5;
@end
MyAdditions.m
#import "MyAdditions.h"
#import <CommonCrypto/CommonDigest.h> // Need to import for CC_MD5 access
@implementation NSString (MyAdditions)
- (NSString *)md5
{
const char *cStr = [self UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, (int)strlen(cStr), result ); // This is the md5 call
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]
];
}
@end
@implementation NSData (MyAdditions)
- (NSString*)md5
{
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( self.bytes, (int)self.length, result ); // This is the md5 call
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]
];
}
@end
편집
NSData md5를 추가한 이유는 제가 필요로 했기 때문입니다.그리고 이 작은 조각을 저장하기에 좋은 장소라고 생각했기 때문입니다.
이러한 방법은 http://www.nsrl.nist.gov/testdata/의 NIST MD5 테스트 벡터를 사용하여 검증됩니다.
내장된 Common Crypto 라이브러리를 사용하면 됩니다.Import를 잊지 마십시오.
#import <CommonCrypto/CommonDigest.h>
그 후:
- (NSString *) md5:(NSString *) input
{
const char *cStr = [input UTF8String];
unsigned char digest[CC_MD5_DIGEST_LENGTH];
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;
}
성능이 중요한 경우 이 최적화된 버전을 사용할 수 있습니다.이 속도는 다음보다 약 5배 빠릅니다.stringWithFormat
또는NSMutableString
.
이것은 NSString의 카테고리입니다.
- (NSString *)md5
{
const char* cStr = [self UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(cStr, strlen(cStr), result);
static const char HexEncodeChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
char *resultData = malloc(CC_MD5_DIGEST_LENGTH * 2 + 1);
for (uint index = 0; index < CC_MD5_DIGEST_LENGTH; index++) {
resultData[index * 2] = HexEncodeChars[(result[index] >> 4)];
resultData[index * 2 + 1] = HexEncodeChars[(result[index] % 0x10)];
}
resultData[CC_MD5_DIGEST_LENGTH * 2] = 0;
NSString *resultString = [NSString stringWithCString:resultData encoding:NSASCIIStringEncoding];
free(resultData);
return resultString;
}
Apple 실장을 사용하지 않는 이유:https://developer.apple.com/library/mac/documentation/Security/Conceptual/cryptoservices/GeneralPurposeCrypto/GeneralPurposeCrypto.html #//apple_ref/doc/uid/TP40011172-CH9-SW1
Apple 개발자 사이트에서 암호화 서비스 가이드를 검색합니다.
사람들이 파일 스트림 버전을 원하니까MD5, SHA1 및 SHA512와 연동되는 Joel Lopes Da Silva의 멋진 단편 일부를 수정하여 스트림을 사용하고 있습니다.iOS용으로 만들어졌지만 OSX에서도 최소한의 변경만으로 작동합니다(ALAsetRepresentation 메서드 삭제).파일 경로 또는 ALAssets(ALAsetRepresentation 사용)가 지정된 파일의 체크섬을 만들 수 있습니다.데이터를 작은 패키지로 정리하여 파일 크기나 자산 크기에 관계없이 메모리에 미치는 영향을 최소화합니다.
현재 github에 있습니다.https://github.com/leetal/FileHash
언급URL : https://stackoverflow.com/questions/1524604/md5-algorithm-in-objective-c
'programing' 카테고리의 다른 글
Bash에서 길이가 0이 아닌 문자열 테스트: [ -n "$var" ]또는 [$var"] (0) | 2023.04.09 |
---|---|
Xcode 빌드 옵션의 영향 "비트 코드 활성화" 예/아니오 (0) | 2023.04.09 |
루프 vba에서 다음 반복으로 건너뜁니다. (0) | 2023.04.09 |
git 분기를 오리진 버전으로 재설정해야 합니다. (0) | 2023.04.09 |
git 저장 해제 (0) | 2023.04.09 |