1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
import "YKEncryption.h"
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonHMAC.h>
@implementation YKEncryption
+ (NSString *)md5:(NSString *)source{
if (0 == source.length) {
return nil;
}
NSData *data = [source dataUsingEncoding:NSUTF8StringEncoding];
unsigned char result[CC_MD5_DIGEST_LENGTH];
memset(result, 0, CC_MD5_DIGEST_LENGTH * sizeof(unsigned char));
CC_MD5(data.bytes, (unsigned int)data.length, result);
NSMutableString *dest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];//
for(NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
[dest appendFormat:@"%02x",result[i]];
}
return [dest lowercaseString];
}
+ (NSString *)SHA:(NSString *)source shaType:(YKSHAType)shaType {
if (0 == source.length || shaType < YKSHATypeSha1 || shaType > YKSHATypeSha512) {
return nil;
}
NSData *data = [source dataUsingEncoding:NSUTF8StringEncoding];
//CC_SHA512_DIGEST_LENGTH > CC_SHA384_DIGEST_LENGTH> CC_SHA256_DIGEST_LENGTH > CC_SHA224_DIGEST_LENGTH > CC_SHA1_DIGEST_LENGTH
//resultData = calloc(length, sizeof(uint8_t)); need free(resultData) 使用定长数组,减少内存碎片。
uint8_t resultData[CC_SHA512_DIGEST_LENGTH];
memset(resultData, 0, CC_SHA512_DIGEST_LENGTH * sizeof(uint8_t));
NSInteger resultDataLength = 0;
switch (shaType) {
case YKSHATypeSha1:{
resultDataLength = CC_SHA1_DIGEST_LENGTH;
CC_SHA1(data.bytes, (CC_LONG)data.length, resultData);
}
break;
case YKSHATypeSha224:{
resultDataLength = CC_SHA224_DIGEST_LENGTH;
CC_SHA224(data.bytes, (CC_LONG)data.length, resultData);
}
break;
case YKSHATypeSha256:{
resultDataLength = CC_SHA256_DIGEST_LENGTH;
CC_SHA256(data.bytes, (CC_LONG)data.length, resultData);
}
break;
case YKSHATypeSha384:{
resultDataLength = CC_SHA384_DIGEST_LENGTH;
CC_SHA384(data.bytes, (CC_LONG)data.length, resultData);
}
break;
case YKSHATypeSha512:{
resultDataLength = CC_SHA512_DIGEST_LENGTH;
CC_SHA512(data.bytes, (CC_LONG)data.length, resultData);
}
break;
default:
break;
}
NSMutableString* result = [NSMutableString stringWithCapacity:resultDataLength * 2];
for(NSInteger i = 0; i < resultDataLength; i++) {
[result appendFormat:@"%02x", resultData[i]];
}
return [result lowercaseString];
}
+ (NSString *)hmac:(NSString *)source withKey:(NSString *)key {
if (0 == source.length ) {
return nil;
}
NSData *sourceData = [source dataUsingEncoding:NSUTF8StringEncoding];
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
unsigned char hmac[CC_SHA256_DIGEST_LENGTH];
memset(hmac, 0, CC_SHA256_DIGEST_LENGTH * sizeof(unsigned char));
CCHmac(kCCHmacAlgSHA256, keyData.bytes, keyData.length, sourceData.bytes, sourceData.length, hmac);
NSData *hmacData = [NSData dataWithBytes:hmac length:sizeof(hmac)];
const unsigned char *buffer = (const unsigned char *)[hmacData bytes];
NSMutableString *resHMAC = [NSMutableString stringWithCapacity:hmacData.length * 2];
for (int i = 0; i < hmacData.length; ++i){
[resHMAC appendFormat:@"%02x", buffer[i]];
}
return [resHMAC lowercaseString];
}
|