2017-01-17 1 views
-1

Ich frage mich, wenn Sie für eine Keychain item vorbereiten, wann würden Sie konvertieren NSString zu NSData?iOS Schlüsselbund: NSDate vs NSString

Zum Beispiel: Im Code von diesem Tutorial zur Verfügung gestellt http://hayageek.com/ios-keychain-tutorial/

Es besagt Folgendes:

[dict setObject:encodedKey forKey:(__bridge id)kSecAttrAccount]; 

jedoch in dem Buch "iOS Application Security" von David Thiel verwendet wie folgt zusammen:

[dict setObject:@"dthiel" forKey:(__bridge id)kSecAttrAccount]; 

Also, bin ich ganz verwirrt, wann muss ichkonvertieren 210 bis NSData und wie kann ich sagen?

Vielen Dank.

Antwort

0

Sie müssen den Wert als NSData codieren.

Zum Beispiel:

#define KeychainIdentifier @"keychain.access.identifier" 

- (void)setKeyValue:(NSString *)key value:(NSString *)value { 
    //The keychain identifier must be encoded as `NSData`. 
    NSData *keychainItemID = [KeychainIdentifier dataUsingEncoding:NSUTF8StringEncoding]; 


    //Build the query. We need to QUERY the keychain and check if the item exists. 
    //If it does, we will NOT be adding the item in the keychain. Note: You can "overwrite" the data if you want but for this example, I'm going to keep it simple and NOT do that. 
    //For this example, the item stored is a "GenericPassword". 
    //We will query for the existence of "one" item. 
    //This query will only return attributes because we are not FETCHING from the keychain. Just "checking/querying". 
    //Finally, the item is accessible when the device is unlocked. 
    NSMutableDictionary *query = [@{ 
            (id)kSecClass    : (id)kSecClassGenericPassword, 
            (id)kSecAttrGeneric  : keychainItemID, 
            (id)kSecMatchLimit  : (id)kSecMatchLimitOne, 
            (id)kSecReturnAttributes : (id)kCFBooleanTrue, 
            (id)kSecAttrAccessible : (id)kSecAttrAccessibleWhenUnlocked, 
            (id)kSecAttrAccount  : key 
            } mutableCopy]; 


    //Query the keychain and get all the item's attributes. 
    CFMutableDictionaryRef result = nil; 
    OSStatus error = SecItemCopyMatching((__bridge CFMutableDictionaryRef)query, (CFTypeRef *)&result); 

    if (error == errSecItemNotFound) { 
     //Item does not exist, add it to the keychain. 
     //To do that, we turn our query into an "INSERT". 
     //That means we need to remove the "return" key because we are no longer fetching/querying and returning attributes. We also have to remove the match limit. 
     //We also remove the match limit. 
     [query removeObjectForKey:(id)kSecMatchLimit]; 
     [query removeObjectForKey:(id)kSecReturnAttributes]; 

     //Now we encode the data to be stored in the keychain and then we submit our "INSERT" to the keychain. This will add the item in the keychain. 
     [query setObject:[value dataUsingEncoding:NSUTF8StringEncoding] forKey:(id)kSecValueData]; 

     error = SecItemAdd((__bridge CFMutableDictionaryRef)query, nil); 

     if (error == noErr) { 
      //Success. 
     } 
     else { 
      //Something went wrong. 
     } 
    } 
    else { 
     //Item already exists. 
    } 
} 
+0

Vielen Dank für Ihre Probe. Nur wundernd, ist es notwendig, ** kSecAttrGeneric ** zu haben? Weil ich mich erinnere, dass ** kSecClassGenericPassword ** nur ** kSecClass **, ** kSecAttrAccount ** und ** kSecAttrServer ** zur Identifizierung benötigt, ist das korrekt? –

+0

@ KwokPingLau; Es ist nicht erforderlich. Es ist optional. Wie Sie hier sehen können: http://stackoverflow.com/questions/11614047/what-makes-a-keychain-item-unique-in-ios Der Primärschlüssel für '' kSecClassGenericPassword'' ist 'kSecAttrAccount und kSecAttrService' – Brandon