-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSString+HMAC.m
231 lines (197 loc) · 6.1 KB
/
NSString+HMAC.m
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
// NSString+HMAC.m
// HMACDemo
//
// Created by MOMO的小棉袄 on 2017/5/8.
// Copyright © 2017年 peony. All rights reserved.
//
#import "NSString+HMAC.h"
@implementation NSString (HMAC)
@dynamic MD5String;
@dynamic SHA1String;
@dynamic SHA256String;
@dynamic SHA512String;
@dynamic encrypt;
/**
传入加密方式和加密的Key,如果key为空则直接
*/
- (NSString* (^)(ENCRYPT_TYPE,NSString*))encrpty{
return ^NSString*(ENCRYPT_TYPE type,NSString* key){
NSString * str = nil;
if (key == nil) {
switch (type) {
case 1:
str = [self MD5String];
break;
case 2:
str = [self SHA1String];
break;
case 3:
str = [self SHA256String];
break;
case 4:
str = [self SHA512String];
break;
}
}else{
switch (type) {
case 1:
str = [self HMACMD5StringWithKey:key];
break;
case 2:
str = [self HMACSHA512StringWithKey:key];
break;
case 3:
str = [self HMACSHA256StringWithKey:key];
break;
case 4:
str = [self HMACSHA512StringWithKey:key];
break;
}
}
return str;
};
}
/**
获得字符的MD5加密后的字符串
@return 加密后的字符串
*/
- (NSString *)MD5String{
return [self encryptWithLength:CC_MD5_DIGEST_LENGTH andWithWay:kCCHmacAlgMD5];
}
/**
获得字符的SHA1加密后的字符串
@return 加密后的字符串
*/
- (NSString *)SHA1String{
return [self encryptWithLength:CC_SHA1_DIGEST_LENGTH andWithWay:kCCHmacAlgSHA1];
}
/**
获得字符的SHA256加密后的字符串
@return 加密后的字符串
*/
- (NSString *)SHA256String{
return [self encryptWithLength:CC_SHA256_DIGEST_LENGTH andWithWay:kCCHmacAlgSHA256];
}
/**
获得字符的SHA512加密后的字符串
@return 加密后的字符串
*/
- (NSString *)SHA512String{
return [self encryptWithLength:CC_SHA512_DIGEST_LENGTH andWithWay:kCCHmacAlgSHA512];
}
/**
类方法实现HMAC加密
@param data 传入的字符串
@param key 传入的key
@param digestLength 编码方式返回的字符长度
@param way 加密方式
@return 加密过后的字符串
*/
+ (NSString *)HMAC:(NSString *)data key:(NSString *)key andLenth:(CC_LONG )digestLength way:(int)way{
return [data HMACWithkey:key andLenth:digestLength way:way];
}
/**
根据加密规范给传入的字符进行HMAC加密
@param key 加密的提供的key
@param digestLength 传入的字符编码长度
@param way 传入的字符编码方式
@return 编码过后的字符串
*/
- (NSString *)HMACWithkey:(NSString *)key andLenth:(CC_LONG )digestLength way:(int)way{
const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding];
const char *cData = [self cStringUsingEncoding:NSUTF8StringEncoding];
uint8_t cHMAC[digestLength];
CCHmac(way, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
//NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:CC_SHA1_DIGEST_LENGTH];
NSString *hash;
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
for(int i = 0; i < digestLength; i++)
[output appendFormat:@"%02x", cHMAC[i]];
hash = output;
return hash;
}
/**
根据加密规范给传入的字符进行哈希加密
@param digestLength 传入的字符编码长度
@param way 传入的字符编码方式
@return 编码过后的字符串
*/
- (NSString *)encryptWithLength:(CC_LONG )digestLength andWithWay:(int)way{
const char *cData = [self cStringUsingEncoding:NSUTF8StringEncoding];
uint8_t cHMAC[digestLength];
CC_MD5(cData, (int)strlen(cData), cHMAC);
NSMutableString *output = [NSMutableString stringWithCapacity:digestLength * 2];
for(int i = 0; i < digestLength; i++)
[output appendFormat:@"%02x", cHMAC[i]];
return output;
}
/**
HMAC+MD5加密
@param data 传入的字符串
@param key 传入的KEY
@return 编码过后的字符串
*/
+ (NSString *)HMACMD5:(NSString *)data key:(NSString *)key {
return [self HMAC:data key:key andLenth:CC_MD5_DIGEST_LENGTH way:kCCHmacAlgMD5];
}
/**
HMAC+SHA1加密
@param data 传入的字符串
@param key 传入的KEY
@return 编码过后的字符串
*/
+ (NSString *)HMACSHA1:(NSString *)data key:(NSString *)key {
return [self HMAC:data key:key andLenth:CC_SHA1_DIGEST_LENGTH way:kCCHmacAlgSHA1];
}
/**
HMAC+SHA256加密
@param data 传入的字符串
@param key 传入的KEY
@return 编码过后的字符串
*/
+ (NSString *)HMACSHA256:(NSString *)data key:(NSString *)key {
return [self HMAC:data key:key andLenth:CC_SHA256_DIGEST_LENGTH way:kCCHmacAlgSHA256];
}
/**
HMAC+SHA512加密
@param data 传入的字符串
@param key 传入的KEY
@return 编码过后的字符串
*/
+ (NSString *)HMACSHA512:(NSString *)data key:(NSString *)key {
return [self HMAC:data key:key andLenth:CC_SHA512_DIGEST_LENGTH way:kCCHmacAlgSHA512];
}
/**
HMAC+MD5加密
@param key 传入的KEY
@return 编码过后的字符串
*/
- (NSString *)HMACMD5StringWithKey:(NSString *)key{
return [self HMACWithkey:key andLenth:CC_MD5_DIGEST_LENGTH way:kCCHmacAlgMD5];
}
/**
HMAC+SHA1加密
@param key 传入的KEY
@return 编码过后的字符串
*/
- (NSString *)HMACHSA1StringWithKey:(NSString *)key{
return [self HMACWithkey:key andLenth:CC_SHA1_DIGEST_LENGTH way:kCCHmacAlgSHA1];
}
/**
HMAC+SHA256加密
@param key 传入的KEY
@return 编码过后的字符串
*/
- (NSString *)HMACSHA256StringWithKey:(NSString *)key{
return [self HMACWithkey:key andLenth:CC_SHA256_DIGEST_LENGTH way:kCCHmacAlgSHA256];
}
/**
HMAC+SHA512加密
@param key 传入的KEY
@return 编码过后的字符串
*/
- (NSString *)HMACSHA512StringWithKey:(NSString *)key{
return [self HMACWithkey:key andLenth:CC_SHA512_DIGEST_LENGTH way:kCCHmacAlgSHA512];
}
@end