-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNSString+EncodingSHA1.m
More file actions
34 lines (26 loc) · 1011 Bytes
/
NSString+EncodingSHA1.m
File metadata and controls
34 lines (26 loc) · 1011 Bytes
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
#import <CommonCrypto/CommonDigest.h>
#import "NSString+EncodingSHA1.h"
@implementation NSString (EncodingSHA1)
//----------------------------------------------------------------------------//
#pragma mark -- APIs --
//----------------------------------------------------------------------------//
- (NSData *)dataByEncodingSHA1
{
unsigned char digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1((const unsigned char *)[self UTF8String], [self length], digest);
return [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
}
- (NSString *)stringByEncodingSHA1Hex
{
const unsigned char *source = [[self dataByEncodingSHA1] bytes];
char finaldigest[2*CC_SHA1_DIGEST_LENGTH+1];
for (NSInteger count = 0; count < CC_SHA1_DIGEST_LENGTH; count++) {
sprintf(finaldigest + count * 2, "%02x", source[count]);
}
finaldigest[2*CC_SHA1_DIGEST_LENGTH] = '\0';
return [NSString
stringWithCString:finaldigest
encoding:NSASCIIStringEncoding
];
}
@end