-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnityIOSKeyChain.mm
93 lines (72 loc) · 3.42 KB
/
UnityIOSKeyChain.mm
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
#import <UIKit/UIKit.h>
#import <Security/Security.h>
#define SERVICE_NAME @"ServiceName"
extern "C" {
char* _Get(const char *cdataType);
int _Add(const char *dataType, const char *value);
int _Delete(const char *dataType);
}
//When searching, "kSecAttrService, kSecAttrAccount" will be used as a unique value.
char* _Get(const char *dataType)
{
NSMutableDictionary* query = [NSMutableDictionary dictionary];
[query setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[query setObject:(id)[NSString stringWithCString:dataType encoding:NSUTF8StringEncoding] forKey:(id)kSecAttrAccount];
[query setObject:SERVICE_NAME forKey:(id)kSecAttrService];
[query setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
CFDataRef cfresult = NULL;
OSStatus err = SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef*)&cfresult);
if (err == noErr) {
NSData* passwordData = (__bridge_transfer NSData *)cfresult;
const char* value = [[[NSString alloc] initWithData:passwordData encoding:NSUTF8StringEncoding] UTF8String];
char *str = strdup(value);
return str;
NSLog(@"create uuid");
} else {
return NULL;
}
}
int _Add(const char *dataType, const char *value)
{
NSMutableDictionary* attributes = nil;
NSMutableDictionary* query = [NSMutableDictionary dictionary];
NSData* sata = [[NSString stringWithCString:value encoding:NSUTF8StringEncoding] dataUsingEncoding:NSUTF8StringEncoding];
[query setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[query setObject:(id)[NSString stringWithCString:dataType encoding:NSUTF8StringEncoding] forKey:(id)kSecAttrAccount];
[query setObject:SERVICE_NAME forKey:(id)kSecAttrService];
OSStatus err = SecItemCopyMatching((CFDictionaryRef)query, NULL);
//if noErr, only update dateTime
if (err == noErr) {
// update item
attributes = [NSMutableDictionary dictionary];
[attributes setObject:sata forKey:(id)kSecValueData];
[attributes setObject:[NSDate date] forKey:(id)kSecAttrModificationDate];
err = SecItemUpdate((CFDictionaryRef)query, (CFDictionaryRef)attributes);
return (int)err;
//if err = errSecItemNotFound, item is not registered. make new item
} else if (err == errSecItemNotFound) {
attributes = [NSMutableDictionary dictionary];
[attributes setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[attributes setObject:(id)[NSString stringWithCString:dataType encoding:NSUTF8StringEncoding] forKey:(id)kSecAttrAccount];
[attributes setObject:sata forKey:(id)kSecValueData];
[attributes setObject:SERVICE_NAME forKey:(id)kSecAttrService];
[attributes setObject:[NSDate date] forKey:(id)kSecAttrCreationDate];
[attributes setObject:[NSDate date] forKey:(id)kSecAttrModificationDate];
err = SecItemAdd((CFDictionaryRef)attributes, NULL);
return (int)err;
} else {
return (int)err;
}
}
int _Delete(const char *dataType)
{
NSMutableDictionary* query = [NSMutableDictionary dictionary];
[query setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[query setObject:(id)[NSString stringWithCString:dataType encoding:NSUTF8StringEncoding] forKey:(id)kSecAttrAccount];
OSStatus err = SecItemDelete((CFDictionaryRef)query);
if (err == noErr) {
return 0;
} else {
return (int)err
}
}