60 lines
1.9 KiB
Plaintext
60 lines
1.9 KiB
Plaintext
|
|
#import <Foundation/Foundation.h>
|
|
#import "WXUtil.h"
|
|
/*
|
|
加密实现MD5和SHA1
|
|
*/
|
|
@implementation WXUtil
|
|
|
|
//md5 encode
|
|
+(NSString *) md5:(NSString *)str
|
|
{
|
|
const char *cStr = [str UTF8String];
|
|
unsigned char digest[CC_MD5_DIGEST_LENGTH];
|
|
CC_MD5( cStr, (unsigned int)strlen(cStr), digest );
|
|
|
|
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;
|
|
}
|
|
//sha1 encode
|
|
+(NSString*) sha1:(NSString *)str
|
|
{
|
|
const char *cstr = [str cStringUsingEncoding:NSUTF8StringEncoding];
|
|
NSData *data = [NSData dataWithBytes:cstr length:str.length];
|
|
|
|
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
|
|
|
|
CC_SHA1(data.bytes, (unsigned int)data.length, digest);
|
|
|
|
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
|
|
|
|
for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
|
|
[output appendFormat:@"%02x", digest[i]];
|
|
|
|
return output;
|
|
}
|
|
//http 请求
|
|
+(NSData *) httpSend:(NSString *)url method:(NSString *)method data:(NSString *)data
|
|
{
|
|
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5];
|
|
//设置提交方式
|
|
[request setHTTPMethod:method];
|
|
//设置数据类型
|
|
[request addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
|
|
//设置编码
|
|
[request setValue:@"UTF-8" forHTTPHeaderField:@"charset"];
|
|
//如果是POST
|
|
[request setHTTPBody:[data dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
|
NSError *error;
|
|
//将请求的url数据放到NSData对象中
|
|
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
|
|
return response;
|
|
//return [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
|
|
}
|
|
|
|
@end |