@@ -32,15 +32,43 @@ @interface OTPListViewController ()<UITableViewDataSource, UITableViewDelegate>
3232
3333@implementation OTPListViewController
3434
35+ + (NSData *)passwordDataForService : (NSString *)serviceName account : (NSString *)account {
36+ if (serviceName.length <= 0 || account.length <= 0 ) {
37+ return nil ;
38+ }
39+ SSKeychainQuery *query = [[SSKeychainQuery alloc ] init ];
40+ query.service = serviceName;
41+ query.account = account;
42+ [query fetch: nil ];
43+ return query.passwordData ;
44+ }
45+
46+ + (NSMutableArray *)loadKeychainAuthURLs {
47+ NSArray *otpAccountList = [SSKeychain accountsForService: kOTPService ];
48+ NSMutableArray *authURLs = [NSMutableArray arrayWithCapacity: otpAccountList.count];
49+ for (NSDictionary *otpAccount in otpAccountList) {
50+ NSData *passwordData = [self passwordDataForService: kOTPService account: otpAccount[(__bridge id )kSecAttrAccount ]];
51+ if (passwordData) {
52+ NSMutableDictionary *tempDict = [otpAccount mutableCopy ];
53+ tempDict[(__bridge id )kSecValueData ] = passwordData;
54+ OTPAuthURL *authURL = [OTPAuthURL ease_authURLWithKeychainDictionary: tempDict];
55+ if (authURL) {
56+ [authURLs addObject: authURL];
57+ }
58+ }
59+ }
60+ return authURLs;
61+ }
62+
3563+(NSString *)otpCodeWithGK : (NSString *)global_key {
3664 NSString *otpCode = nil ;
3765 if (global_key.length > 0 ) {
38- NSArray *otpAccountDictList = [SSKeychain accountsForService: kOTPService ];
39- for (NSDictionary *obj in otpAccountDictList ) {
40- NSString *name = obj[(__bridge id ) kSecAttrAccount ] ;
41- name = [[name componentsSeparatedByString: @" @" ] firstObject ];
42- if ([name isEqualToString: global_key]) {
43- OTPAuthURL *authURL = [OTPAuthURL ease_authURLWithKeychainDictionary: obj];
66+ NSMutableArray *authURLs = [self loadKeychainAuthURLs ];
67+ for (OTPAuthURL *authURL in authURLs ) {
68+ NSString *cur_issure = authURL. issuer ;
69+ NSString *cur_global_key = [[authURL. name componentsSeparatedByString: @" @" ] firstObject ];
70+ if ([cur_issure isEqualToString: @" Coding " ] &&
71+ [cur_global_key isEqualToString: global_key]) {
4472 otpCode = authURL.otpCode ;
4573 break ;
4674 }
@@ -52,18 +80,7 @@ +(NSString *)otpCodeWithGK:(NSString *)global_key{
5280- (void )viewDidLoad {
5381 [super viewDidLoad ];
5482 self.title = @" 身份验证器" ;
55- [self loadKeychainArray ];
56- }
57-
58- - (void )loadKeychainArray {
59- NSArray *otpAccountDictList = [SSKeychain accountsForService: kOTPService ];
60- self.authURLs = [NSMutableArray arrayWithCapacity: [otpAccountDictList count ]];
61- for (NSDictionary *otpAccountDict in otpAccountDictList) {
62- OTPAuthURL *authURL = [OTPAuthURL ease_authURLWithKeychainDictionary: otpAccountDict];
63- if (authURL) {
64- [self .authURLs addObject: authURL];
65- }
66- }
83+ self.authURLs = [[self class ] loadKeychainAuthURLs ];
6784}
6885
6986- (void )viewWillAppear : (BOOL )animated {
0 commit comments