For up-to-date product documentation, see the IBM MobileFirst Foundation Developer Center.

JSONStore security utilities iOS examples

Learn about JSONStore security utilities iOS examples.

Encryption and decryption

// User provided password, hardcoded only for simplicity. NSString* password = @"HelloPassword"; // Random salt with recommended length. NSString* salt = [WLSecurityUtils generateRandomStringWithBytes:32]; // Recomended number of iterations. int iterations = 10000; // Populated with an error if one occurs. NSError* error = nil; // Call that generates the key. NSString* key = [WLSecurityUtils generateKeyWithPassword:password andSalt:salt andIterations:iterations error:&error]; // Text that is encrypted. NSString* originalString = @"My secret text"; NSDictionary* dict = [WLSecurityUtils encryptText:originalString withKey:key error:&error]; // Should return: 'My secret text'. NSString* decryptedString = [WLSecurityUtils decryptWithKey:key andDictionary:dict error:&error];

Encode and decode base64

// Input string. NSString* originalString = @"Hello world!"; // Encode to base64. NSData* originalStringData = [originalString dataUsingEncoding:NSUTF8StringEncoding]; NSString* encodedString = [WLSecurityUtils base64StringFromData:originalStringData length:originalString.length]; // Should return: 'Hello world!'. NSString* decodedString = [[NSString alloc] initWithData:[WLSecurityUtils base64DataFromString:encodedString] encoding:NSUTF8StringEncoding];

Get remote random

[WLSecurityUtils getRandomStringFromServerWithBytes:32 timeout:1000 completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { // You might want to see the response and the connection error before moving forward. // Get the secure random string. NSString* secureRandom = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; }];

Get local random

NSString* secureRandom = [WLSecurityUtils generateRandomStringWithBytes:32];

Parent topic: JSONStore security utilities examples