-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeychainTool.m
124 lines (109 loc) · 4.44 KB
/
KeychainTool.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
//
// KeychainTool.m
// RuntimeTest
//
// Created by user on 2020/4/15.
// Copyright © 2020 zilong. All rights reserved.
//
#import "KeychainTool.h"
@implementation KeychainTool
+ (KeychainTool *)sharedKeychain {
static KeychainTool *shareKeychainInstance = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
shareKeychainInstance = [[self alloc] init];
});
return shareKeychainInstance;
}
- (BOOL)addKeychainWithKey:(NSString *)key serviceKey:(NSString *)serviceKey value:(NSData *)value {
NSDictionary *add = @{BRIDGE(id, kSecAttrAccessible) : BRIDGE(id, kSecAttrAccessibleWhenUnlocked),
BRIDGE(id, kSecClass) : BRIDGE(id, kSecClassGenericPassword),
BRIDGE(id, kSecValueData) : value,
BRIDGE(id, kSecAttrAccount) : key,
BRIDGE(id, kSecAttrService) : serviceKey
};
return [self addKeychainWithKeyDictionary:add dataValue:value];
}
- (KeychainToolStatus *)queryKeychainWithKey:(NSString *)key serviceKey:(NSString *)serviceKey {
NSDictionary *quary = @{BRIDGE(id, kSecClass) : BRIDGE(id, kSecClassGenericPassword),
BRIDGE(id, kSecReturnData) : @YES,
BRIDGE(id, kSecMatchLimit) : BRIDGE(id, kSecMatchLimitOne),
BRIDGE(id, kSecAttrAccount) : key,
BRIDGE(id, kSecAttrService) : serviceKey
};
return [self queryKeychainWithKeyDictionary:quary];
}
- (BOOL)updateKeychainWithKey:(NSString *)key serviceKey:(NSString *)serviceKey changeValue:(NSData *)value {
NSDictionary *basics = @{BRIDGE(id, kSecClass) : BRIDGE(id, kSecClassGenericPassword),
BRIDGE(id, kSecAttrAccount) : key,
BRIDGE(id, kSecAttrService) : serviceKey
};
return [self updateKeychainWithKeyDictionary:basics updateData:value];
}
- (BOOL)deleteKeychainWithKey:(NSString *)key serviceKey:(NSString *)serviceKey {
NSDictionary *delete = @{BRIDGE(id, kSecClass) : BRIDGE(id, kSecClassGenericPassword),
BRIDGE(id, kSecAttrAccount) : key,
BRIDGE(id, kSecAttrService) : serviceKey
};
return [self deleteKeychainWithKeyDictionary:delete];
}
/// 添加
/// @param cfDictionary 自定义Dictionary 包含类型和标识等
/// @param dataValue 插入的数据
- (BOOL)addKeychainWithKeyDictionary:(NSDictionary *)cfDictionary dataValue:(NSData *)dataValue {
CFTypeRef result;
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)cfDictionary, &result);
if (status == errSecSuccess) {
NSLog(@"添加成功");
return YES;
} else {
NSLog(@"添加失败");
return NO;
}
}
/// 查询
/// @param cfDictionary 自定义Dictionary 包含类型和标识等
- (KeychainToolStatus *)queryKeychainWithKeyDictionary:(NSDictionary *)cfDictionary {
KeychainToolStatus *toolStatus = [[KeychainToolStatus alloc] init];
CFTypeRef dataTypeRef = NULL;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)cfDictionary, &dataTypeRef);
if (status == errSecSuccess) {
NSData *data = (__bridge NSData *)dataTypeRef;
toolStatus.executeStatus = KeyChainExecuteStatusSuccess;
toolStatus.executeData = data;
NSLog(@"查询成功");
} else {
toolStatus.executeStatus = KeyChainExecuteStatusFailure;
NSLog(@"查询失败");
}
return toolStatus;
}
/// 更新
/// @param cfDictionary 自定义Dictionary 包含类型和标识等
/// @param updateData 更新的数据
- (BOOL)updateKeychainWithKeyDictionary:(NSDictionary *)cfDictionary updateData:(NSData *)updateData {
NSDictionary *update = @{(__bridge id)kSecValueData : updateData};
OSStatus status = SecItemUpdate((__bridge CFDictionaryRef)cfDictionary, (__bridge CFDictionaryRef)update);
if (status == errSecSuccess) {
NSLog(@"更新成功");
return YES;
} else {
NSLog(@"更新失败");
return NO;
}
}
/// 删除
/// @param cfDictionary 自定义Dictionary 包含类型和标识等
- (BOOL)deleteKeychainWithKeyDictionary:(NSDictionary *)cfDictionary {
OSStatus status = SecItemDelete((__bridge CFDictionaryRef)cfDictionary);
if (status == errSecSuccess) {
NSLog(@"删除成功");
return YES;
} else {
NSLog(@"删除失败");
return NO;
}
}
@end
@implementation KeychainToolStatus
@end