MD5使用接口
NSString+Hash.h
//
// NSString+Hash.h
//
// Created by Tom Corwine on 5/30/12..
//
#import <Foundation/Foundation.h>
@interface NSString (Hash)
@property (readonly) NSString *md5String;
@property (readonly) NSString *sha1String;
@property (readonly) NSString *sha256String;
@property (readonly) NSString *sha512String;
- (NSString *)hmacSHA1StringWithKey:(NSString *)key;
- (NSString *)hmacSHA256StringWithKey:(NSString *)key;
- (NSString *)hmacSHA512StringWithKey:(NSString *)key;
@end
MD5使用
ViewController.m
#import "ViewController.h"
#import "NSString+Hash.h"
#define Salt @"fsdhjkfhjksdhjkfjhkd546783765"
@implementation ViewController
- (void)viewDidLoad{
[super viewDidLoad];
[self digest4:@"123"];
[self digest4:@"abc"];
[self digest4:@"456"];
}
/**
* 直接用MD5加密
*/
- (NSString *)digest:(NSString *)str{
NSString *anwen = [str md5String];
NSLog(@"%@ - %@", str, anwen);
return anwen;
}
/**
* 加盐
*/
- (NSString *)digest2:(NSString *)str{
str = [str stringByAppendingString:Salt];
NSString *anwen = [str md5String];
NSLog(@"%@ - %@", str, anwen);
return anwen;
}
/**
* 多次MD5
*/
- (NSString *)digest3:(NSString *)str{
NSString *anwen = [str md5String];
anwen = [anwen md5String];
NSLog(@"%@ - %@", str, anwen);
return anwen;
}
/**
* 先加密, 后乱序
*/
- (NSString *)digest4:(NSString *)str{
NSString *anwen = [str md5String];
NSString *header = [anwen substringToIndex:2];
NSString *footer = [anwen substringFromIndex:2];
anwen = [footer stringByAppendingString:header];
NSLog(@"%@ - %@", str, anwen);
return anwen;
}
@end