6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

文字列をMD5変換する

Posted at

MD5変換するメソッドをNSStringにカテゴリで追加します。ウェブサービスのAPIとデータの受け渡しをするときなどに、たまに使うかもしれません。

//  NSString+MD5.h

# import <Foundation/Foundation.h>

@interface NSString (md5String)
- (NSString *)md5String;
@end

//  NSString+MD5.m

# import "NSString+MD5.h"
# import <CommonCrypto/CommonDigest.h>

@implementation NSString (md5String)
- (NSString *)md5String {
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, strlen(cStr), result );
    return [NSString stringWithFormat:
			@"xxxxxxxxxxxxxxxx",
			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

参考:MD5 algorithm in Objective C
http://stackoverflow.com/questions/1524604/md5-algorithm-in-objective-c

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?