@@ -68,17 +68,83 @@ WSCKeychain* emptyKeychain = [ [ WSCKeychainManager defaultManager ]
68
68
// emptyKeychain will be released automatically.
69
69
```
70
70
71
- ** Find the following Internet passphrase: **
71
+ ** Find the following Internet passphrase then print its Account Name, Passphrase and Comment **
72
72
73
73
<img src =" http://i.imgbox.com/eeiU5Ymr.png " title =" IMDb Passphrase " border =" 0 " height =" 498 " width =" 501 " />
74
74
75
- * using pure C API of * Keychain Services* :
75
+ * using pure C API of * Keychain Services* (OMG! Give me a break!😲🔫) :
76
76
77
77
``` objective-c
78
- OMG! Give me a break !
78
+ OSStatus resultCode = errSecSuccess;
79
+
80
+ // Attributes that will be used for constructing search criteria
81
+ char * label = " secure.imdb.com" ;
82
+ SecProtocolType* ptrProtocolType = malloc( sizeof ( SecProtocolType ) );
83
+ *ptrProtocolType = kSecProtocolTypeHTTPS ;
84
+
85
+ SecKeychainAttribute attrs[] = { { kSecLabelItemAttr, ( UInt32 )strlen( label ), ( void* )label }
86
+ , { kSecProtocolItemAttr, ( UInt32 )sizeof( SecProtocolType ), ( void* )ptrProtocolType }
87
+ };
88
+
89
+ SecKeychainAttributeList attrsList = { sizeof( attrs ) / sizeof( attrs[ 0 ] ), attrs };
90
+
91
+ // Creates a search object matching the given list of search criteria.
92
+ SecKeychainSearchRef searchObject = NULL;
93
+ if ( ( resultCode = SecKeychainSearchCreateFromAttributes( NULL
94
+ , kSecInternetPasswordItemClass
95
+ , &attrsList
96
+ , &searchObject
97
+ ) ) == errSecSuccess )
98
+ {
99
+ SecKeychainItemRef matchedItem = NULL;
100
+
101
+ // Finds the next keychain item matching the given search criteria.
102
+ while ( ( resultCode = SecKeychainSearchCopyNext( searchObject, &matchedItem ) ) != errSecItemNotFound )
103
+ {
104
+ SecKeychainAttribute theAttributes[] = { { kSecAccountItemAttr, 0, NULL }
105
+ , { kSecCommentItemAttr, 0, NULL }
106
+ };
107
+
108
+ SecKeychainAttributeList theAttrList = { sizeof( theAttributes ) / sizeof( theAttributes[ 0 ] ), theAttributes };
109
+ UInt32 lengthOfPassphrase = 0;
110
+ char* passphraseBuffer = NULL;
111
+ if ( ( resultCode = SecKeychainItemCopyContent( matchedItem
112
+ , NULL
113
+ , &theAttrList
114
+ , &lengthOfPassphrase
115
+ , ( void** )&passphraseBuffer
116
+ ) ) == errSecSuccess )
117
+ {
118
+ NSLog( @"\n==============================\n" );
119
+ NSLog( @"Passphrase: %@", [ [ [ NSString alloc ] initWithBytes: passphraseBuffer length: lengthOfPassphrase encoding: NSUTF8StringEncoding ] autorelease ] );
120
+
121
+ for ( int _Index = 0; _Index < theAttrList.count; _Index++ )
122
+ {
123
+ SecKeychainAttribute attrStruct = theAttrList.attr[ _Index ];
124
+ NSString* attributeValue = [ [ [ NSString alloc ] initWithBytes: attrStruct.data length: attrStruct.length encoding: NSUTF8StringEncoding ] autorelease ];
125
+
126
+ if ( attrStruct.tag == kSecAccountItemAttr )
127
+ NSLog( @"IMDb User Name: %@", attributeValue );
128
+ else if ( attrStruct.tag == kSecCommentItemAttr )
129
+ NSLog( @"Comment: %@", attributeValue );
130
+ }
131
+
132
+ NSLog( @"\n==============================\n" );
133
+ }
134
+
135
+ SecKeychainItemFreeContent ( &theAttrList, passphraseBuffer );
136
+ CFRelease( matchedItem );
137
+ }
138
+ }
139
+
140
+ if ( ptrProtocolType )
141
+ free( ptrProtocolType );
142
+
143
+ if ( searchObject )
144
+ CFRelease( searchObject );
79
145
```
80
146
81
- * using * WaxSealCore* :
147
+ * using *WaxSealCore* (Just a few lines of Objective-C code) :
82
148
83
149
```objective-c
84
150
NSError* error = nil;
@@ -91,20 +157,36 @@ WSCPassphraseItem* IMDbLoginPassphrase = ( WSCPassphraseItem* )[ [ WSCKeychain l
91
157
itemClass: WSCKeychainItemClassInternetPassphraseItem
92
158
error: &error ];
93
159
94
- // WaxSealCore supports ** Unicode-based** search, so you can use Emoji or Chinese in your search criteria.
160
+ // WaxSealCore supports Unicode-based search, so you can use Emoji or Chinese in your search criteria.
95
161
// One step. So easy, is not it?
96
-
162
+ ```
163
+
164
+ Print its Account Name, Passphrase and Comment:
165
+
166
+ ``` objective-c
97
167
if ( IMDbLoginPassphrase )
98
168
{
99
- NSLog( @"Huh, found it!" );
169
+ NSLog( @"==============================" );
170
+ // Use the `account` property
100
171
NSLog( @"IMDb User Name: %@", IMDbLoginPassphrase.account );
172
+
173
+ // Use the `passphrase` property
174
+ NSLog( @"Passphrase: %@", [ [ [ NSString alloc ] initWithData: IMDbLoginPassphrase.passphrase encoding: NSUTF8StringEncoding ] autorelease ] );
175
+
176
+ // Use the `comment` property
101
177
NSLog( @"Comment: %@", IMDbLoginPassphrase.comment );
178
+ NSLog( @"==============================" );
102
179
180
+ // -setComment:
103
181
IMDbLoginPassphrase.comment = @"👿👿👿👿👿👿";
104
182
}
105
183
else
106
184
NSLog( @"I'm so sorry!" );
185
+ ```
186
+
187
+ Batch Search:
107
188
189
+ ```objective-c
108
190
// Find all the Internet passphrases that met the given search criteria
109
191
NSArray* passphrases = [ [ WSCKeychain login ]
110
192
// Batch search
@@ -118,9 +200,11 @@ if ( passphrases.count != 0 )
118
200
{
119
201
for ( WSCPassphraseItem* _Passphrase in passphrases )
120
202
{
121
- NSLog( @"Huh, got one!" );
122
- NSLog( @"IMDb User Name: %@", _ Passphrase.account );
123
- NSLog( @"Comment: %@", _ Passphrase.comment );
203
+ NSLog( @"==============================" );
204
+ NSLog( @"IMDb User Name: %@", IMDbLoginPassphrase.account );
205
+ NSLog( @"Passphrase: %@", [ [ [ NSString alloc ] initWithData: IMDbLoginPassphrase.passphrase encoding: NSUTF8StringEncoding ] autorelease ] );
206
+ NSLog( @"Comment: %@", IMDbLoginPassphrase.comment );
207
+ NSLog( @"==============================" );
124
208
125
209
_Passphrase.comment = @"👺👹👺👹";
126
210
}
0 commit comments