아이폰용
#import <CommonCrypto/CommonDigest.h>
+ (NSString *)uniqueIDFromString:(NSString *)source
{
const char *src = [[source lowercaseString] UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(src, (CC_LONG)strlen(src), result);
NSString *ret = [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]
];
return ret;
}
안드로이드용
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public static String getMD5Hash(String s) {
MessageDigest m = null;
String hash = null;
try {
m = MessageDigest.getInstance("MD5");
m.update(s.getBytes(),0,s.length());
hash = new BigInteger(1, m.digest()).toString(16);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return hash;
}
사용법
String src = "123qwe";
String enc = getMD5Hash(src);
'아이폰' 카테고리의 다른 글
iOS 버전 체크 매크로 (1) | 2013.02.01 |
---|---|
NSString의 유용한 메서드 몇가지 (0) | 2013.01.15 |
[IOS] 이미지를 서버로 업로드하기 (2) | 2012.02.28 |
[Objective C] Tabbar Custom 하기 (0) | 2012.02.22 |
앱 내에서 카카오톡 호출하기 (0) | 2012.02.11 |