MD5

---------------------------------图片稀释类-----------------------------------

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>


@interface ReduceImageRatio : NSObject

+ (UIImage *)imageWithMaxSide:(CGFloat)length sourceImage:(UIImage *)image;

@end



#import "ReduceImageRatio.h"


@implementation ReduceImageRatio

+ (UIImage *)imageWithMaxSide:(CGFloat)length sourceImage:(UIImage *)image{

    CGFloat scale = [[UIScreen mainScreen] scale];

    CGSize imgSize = GGSSizeReduce(image.size, length);

    UIImage *img = nil;

    

    UIGraphicsBeginImageContextWithOptions(imgSize, YES, scale);  // 创建一个 bitmap context

    

    [image drawInRect:CGRectMake(0, 0, imgSize.width, imgSize.height)

            blendMode:kCGBlendModeNormal alpha:1.0];              // 将图片绘制到当前的 context

    

    img = UIGraphicsGetImageFromCurrentImageContext();            // 从当前 context 中获取刚绘制的图片

    UIGraphicsEndImageContext();

    

    return img;

}


static CGSize GGSSizeReduce(CGSize size, CGFloat limit)   // 按比例减少尺寸

{

    CGFloat max = MAX(size.width, size.height);

    if (max < limit) {

        return size;

    }

    

    CGSize imgSize;

    CGFloat ratio = size.height / size.width;

    

    if (size.width > size.height) {

        imgSize = CGSizeMake(limit, limit*ratio);

    } else {

        imgSize = CGSizeMake(limit/ratio, limit);

    }

    

    return imgSize;

}

@end






---------------------------------MD5类-----------------------------------

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>


@interface Md5 : NSObject

+ (NSString *) md5:(NSString *)str;

@end



#import "Md5.h"

#import <CommonCrypto/CommonDigest.h>

@implementation Md5



+ (NSString *) md5:(NSString *)str

{

    const char *cStr = [str UTF8String];

    unsigned char result[16];

    CC_MD5( cStr, (uint32_t)strlen(cStr), result );

    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




#import <Foundation/Foundation.h>

#import <CommonCrypto/CommonDigest.h>

#import <UIKit/UIKit.h>

@interface CreateMD5WithFile : NSObject

+(NSString *)createMD5WithFile:(NSString *)string;

+(NSString*)fileMD5:(NSData *)data;

@end



#import "CreateMD5WithFile.h"

#import "Md5.h"

@implementation CreateMD5WithFile

+(NSString *)createMD5WithFile:(NSData *)imageData

{

    NSString * string = [[NSString allocinitWithData:imageData encoding:NSUTF8StringEncoding];

    return [Md5 md5:string];

}


+(NSString*)fileMD5:(NSData *)data

{

    NSString * dataPath = NSTemporaryDirectory();

    

    //将图片归档

    [data writeToFile:[dataPath stringByAppendingPathComponent:@"dataForImage.tmp"atomically:YES];

    

    //

    NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:[dataPath stringByAppendingPathComponent:@"dataForImage.tmp"]];

    if( handle== nil ) return @"ERROR GETTING FILE MD5"// file didn't exist

    

    CC_MD5_CTX md5;

    

    CC_MD5_Init(&md5);

    

    BOOL done = NO;

    while(!done)

    {

        NSData* fileData = [handle readDataOfLength1000 ];

        CC_MD5_Update(&md5, [fileData bytes], [fileData length]);

        if( [fileData length] == 0 ) done = YES;

    }

    unsigned char digest[CC_MD5_DIGEST_LENGTH];

    CC_MD5_Final(digest, &md5);

    NSString* s = [NSString stringWithFormat@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",

                   digest[0], digest[1],

                   digest[2], digest[3],

                   digest[4], digest[5],

                   digest[6], digest[7],

                   digest[8], digest[9],

                   digest[10], digest[11],

                   digest[12], digest[13],

                   digest[14], digest[15]];

    return s;

}

@end






---------------------------------使用-----------------------------------

//

//  ViewController.m

//  FXMD5

//

//  Created by Floating_SH on 16/1/6.

//  Copyright © 2016 SH. All rights reserved.

//


#import "ViewController.h"

#import "Md5.h"

#import "CreateMD5WithFile.h"

#import "ReduceImageRatio.h"

@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    // 1.根据一个字符串生成一个md5,

    NSString *string1 = @"http://baidu.com";

    NSLog(@"%@",[Md5 md5:string1]);

    // 23901b9dc3d84176eb001d386a96be9e 系统MD5

    // 23901B9DC3D84176EB001D386A96BE9E 生成MD5

    

    // 2.对一个文件求md5

    UIImage *image = [UIImage imageNamed:@"1"];

    

    // 将图片变小

    UIImage *image_xs = [ReduceImageRatio imageWithMaxSide:200 sourceImage:image];

    

    // 将图片写入沙盒

    // (1)归档

    NSData *data = UIImagePNGRepresentation(image_xs);

    // (2)写入沙盒

    NSString *DocumentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

    NSString *filePath = [DocumentPath stringByAppendingPathComponent:@"1.png"];

    NSLog(@"%@",filePath);

    

    [data writeToFile:filePath atomically:YES];

    

    // 对图片求md5

    NSLog(@"%@",[CreateMD5WithFile fileMD5:data]);

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值