mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-02 22:42:45 +08:00
49 lines
1.4 KiB
Objective-C
49 lines
1.4 KiB
Objective-C
//
|
|
// NSString+MD5.m
|
|
// RestKit
|
|
//
|
|
// Created by Jeff Arena on 4/4/11.
|
|
// Copyright 2011 Two Toasters
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//
|
|
|
|
#import <CommonCrypto/CommonDigest.h>
|
|
#import "NSString+MD5.h"
|
|
#import "../Support/RKFixCategoryBug.h"
|
|
|
|
RK_FIX_CATEGORY_BUG(NSString_MD5)
|
|
|
|
@implementation NSString (MD5)
|
|
|
|
- (NSString*)MD5 {
|
|
// Create pointer to the string as UTF8
|
|
const char* ptr = [self UTF8String];
|
|
|
|
// Create byte array of unsigned chars
|
|
unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
|
|
|
|
// Create 16 byte MD5 hash value, store in buffer
|
|
CC_MD5(ptr, strlen(ptr), md5Buffer);
|
|
|
|
// Convert MD5 value in the buffer to NSString of hex values
|
|
NSMutableString* output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
|
|
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
|
|
[output appendFormat:@"%02x",md5Buffer[i]];
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
@end
|