-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathXCKeyBuilder.m
More file actions
executable file
·71 lines (58 loc) · 2.24 KB
/
XCKeyBuilder.m
File metadata and controls
executable file
·71 lines (58 loc) · 2.24 KB
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
////////////////////////////////////////////////////////////////////////////////
//
// JASPER BLUES
// Copyright 2012 - 2013 Jasper Blues
// All Rights Reserved.
//
// NOTICE: Jasper Blues permits you to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
#import "XCKeyBuilder.h"
#import <CommonCrypto/CommonDigest.h>
#if MD5_DIGEST_LENGTH != CC_MD5_DIGEST_LENGTH
#error Digest length in XCKeyBuilder.h (MD5_DIGEST_LENGTH) disagress with CommonCrypto value (CC_MD5_DIGEST_LENGTH)
#endif
@implementation XCKeyBuilder
//-------------------------------------------------------------------------------------------
#pragma mark - Class Methods
//-------------------------------------------------------------------------------------------
+ (XCKeyBuilder*)forItemNamed:(NSString*)name
{
return [self createUnique];
}
+ (XCKeyBuilder*)createUnique
{
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFUUIDBytes bytes = CFUUIDGetUUIDBytes(theUUID);
CFRelease(theUUID);
return [[XCKeyBuilder alloc] initHashValueMD5HashWithBytes:&bytes length:sizeof(bytes)];
}
//-------------------------------------------------------------------------------------------
#pragma mark - Initialization & Destruction
//-------------------------------------------------------------------------------------------
- (id)initHashValueMD5HashWithBytes:(const void*)bytes length:(NSUInteger)length
{
self = [super init];
if (self != nil)
{
// CC_MD5(bytes, (int) length, _value);
CC_SHA256(bytes, ( CC_LONG )length, _value);
}
return self;
}
//-------------------------------------------------------------------------------------------
#pragma mark - Interface Methods
//-------------------------------------------------------------------------------------------
- (NSString*)build
{
NSInteger byteLength = sizeof(HashValueMD5Hash);
NSMutableString* stringValue = [NSMutableString stringWithCapacity:byteLength * 2];
NSInteger i;
for (i = 0; i < byteLength; i++)
{
[stringValue appendFormat:@"%02x", _value[i]];
}
return [[stringValue substringToIndex:24] uppercaseString];
}
@end