|
13 | 13 | @implementation VPUPSHAUtil |
14 | 14 |
|
15 | 15 | + (NSString *)sha1HashString:(NSString *)string { |
16 | | - unsigned char *hashStr = (unsigned char *)malloc(sizeof(unsigned char) * (CC_SHA1_DIGEST_LENGTH * 2 + 1)); |
17 | | - vpup_sha1_encryption((char *)[string UTF8String], hashStr); |
18 | 16 |
|
19 | | - NSString *hashString = [NSString stringWithUTF8String:(char *)hashStr]; |
| 17 | + if (!string) { |
| 18 | + return nil; |
| 19 | + } |
20 | 20 |
|
21 | | - free(hashStr); |
| 21 | + NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; |
22 | 22 |
|
23 | | - return hashString; |
| 23 | + uint8_t digest[CC_SHA1_DIGEST_LENGTH]; |
| 24 | + |
| 25 | + CC_SHA1(data.bytes, (unsigned int)data.length, digest); |
| 26 | + |
| 27 | + NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2]; |
| 28 | + |
| 29 | + for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) { |
| 30 | + [output appendFormat:@"%02x", digest[i]]; |
| 31 | + } |
| 32 | + |
| 33 | + return output; |
24 | 34 | } |
25 | 35 |
|
26 | 36 | + (NSString *)sha256HashString:(NSString *)string { |
27 | | - unsigned char *hashStr = (unsigned char *)malloc(sizeof(unsigned char) * (CC_SHA256_DIGEST_LENGTH * 2 + 1)); |
28 | | - vpup_sha256_encryption((char *)[string UTF8String], hashStr); |
29 | | - NSString *hashString = [NSString stringWithUTF8String:(char *)hashStr]; |
30 | 37 |
|
31 | | - free(hashStr); |
| 38 | + if (!string) { |
| 39 | + return nil; |
| 40 | + } |
| 41 | + |
| 42 | + NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; |
| 43 | + |
| 44 | + uint8_t digest[CC_SHA256_DIGEST_LENGTH]; |
| 45 | + |
| 46 | + CC_SHA256(data.bytes, (unsigned int)data.length, digest); |
32 | 47 |
|
33 | | - return hashString; |
| 48 | + NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2]; |
| 49 | + |
| 50 | + for(int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) { |
| 51 | + [output appendFormat:@"%02x", digest[i]]; |
| 52 | + } |
| 53 | + |
| 54 | + return output; |
34 | 55 | } |
35 | 56 |
|
36 | 57 | + (NSString *)hmac_sha1HashString:(NSString *)string key:(NSString *)hmacKey { |
37 | | - unsigned char *hashStr = (unsigned char *)malloc(sizeof(unsigned char) * (CC_SHA1_DIGEST_LENGTH * 2 + 1)); |
38 | 58 |
|
39 | | - vpup_hmac_sha1_hex_encryption((char *)[hmacKey UTF8String], (char *)[string UTF8String], hashStr); |
| 59 | + if (!string || !hmacKey) { |
| 60 | + return nil; |
| 61 | + } |
| 62 | + |
| 63 | + const char *cData = [string cStringUsingEncoding:NSUTF8StringEncoding]; |
| 64 | + |
| 65 | + const char *cKey = [hmacKey cStringUsingEncoding:NSUTF8StringEncoding]; |
| 66 | + |
| 67 | + uint8_t cHMAC[CC_SHA1_DIGEST_LENGTH]; |
| 68 | + |
| 69 | + CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC); |
40 | 70 |
|
41 | | - NSString *hashString = [NSString stringWithUTF8String:(char *)hashStr]; |
| 71 | + NSMutableString * output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2]; |
42 | 72 |
|
43 | | - free(hashStr); |
| 73 | + for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) { |
| 74 | + [output appendFormat:@"%02x", cHMAC[i]]; |
| 75 | + } |
44 | 76 |
|
45 | | - return hashString; |
| 77 | + return output; |
46 | 78 | } |
47 | 79 |
|
48 | 80 | @end |
0 commit comments