Create a data vault for encrypted storage of application data.
DataVault* dataVault = nil;
@try
{
// If the dataVault already exists, call getVault and unlock it
// If not, create the vault with necessary password
// The password is chosen to make sure it satisfies password policy criteria given below
if ( [DataVault vaultExists:@"SampleVault"] ) {
dataVault = [DataVault getVault:@"SampleVault"];
[dataVault unlock:@"password!1A" Salt:@"saltD#ddg#k05%gnd[!1A"];
}
else {
dataVault = [DataVault createVault:@"SampleVault" Password:@"password!1A" Salt:@"saltD#ddg#k05%gnd[!1A"];
}
// Supply various criteria for password policy
DVPasswordPolicy *pwdPolicy = [[[DVPasswordPolicy alloc] init] autorelease];
pwdPolicy.defaultPasswordAllowed = YES;
pwdPolicy.minLength = 4;
pwdPolicy.hasDigits = YES;
pwdPolicy.hasUpper = YES;
pwdPolicy.hasLower = YES;
pwdPolicy.hasSpecial = YES;
pwdPolicy.expirationDays = 20;
pwdPolicy.minUniqueChars = 3;
pwdPolicy.lockTimeout = 1600;
pwdPolicy.retryLimit = 20;
// setPasswordPolicy will lock the vault to ensure old password conforms to new password policy settings
[dataVault setPasswordPolicy:pwdPolicy];
// You must unlock the vault after setting the password policy
[dataVault unlock:@"password!1A" Salt:@"saltD#ddg#k05%gnd[!1A"];
// Use getPasswordPolicy to get the current policy set in the vault
pwdPolicy = [dataVault getPasswordPolicy];
NSLog(@" pwdPolicy %@ ",pwdPolicy.description);
// Call setString by giving it a name:value pair to encrypt and persist
// a string data type within your dataVault.
[dataVault setString:@"stringName" withValue:@"stringValue"];
// Call getString to retrieve the string we just stored in our data vault!
NSString *storedStringValue = [dataVault getString:@"stringName"];
NSLog(@" storedStringValue %@ ",storedStringValue.description);
// Call setValue by giving it a name:value pair to encrypt and persist
// a binary data type within your dataVault unsigned char acBinData[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
[dataVault setValue:@"binaryName" withValue:[NSData dataWithBytes:acBinData length:7]];
// Call getValue to retrieve the binary we just stored in our data vault!
NSData *storedBinaryValue = [dataVault getValue:@"binaryName"];
NSLog(@" storedBinaryValue %@ ",storedBinaryValue );
// Call getDataNames to retrieve all stored element names from our data vault
// NSArray * dataNames = [dataVault getDataNames];
NSArray * dataNames = [dataVault getDataNames];
if ( dataNames != nil ) {
DVDataName *dataName;
// for ( NSInteger iIdx = 0; iIdx < [dataNames count]; iIdx++ ) {
for ( NSInteger iIdx = 0; iIdx < [dataNames size]; iIdx ++) {
dataName = [dataNames objectAtIndex:iIdx];
if ( dataName.type == DVDataTypeString ) {
// Stored value is of string type
NSString *thisStringValue = [dataVault getString:dataName.name];
NSLog(@" thisStringValue %@ ",thisStringValue );
}
else if ( dataName.type == DVDataTypeBinary ) {
// Stored value is of binary type
NSData *thisBinaryValue = [dataVault getValue:dataName.name];
NSLog(@" thisBinaryValue %@ ",thisBinaryValue );
}
else {
// Unknown type. Possibly stored using previous version of dataVault
// Try as string first and then as binary
NSString *thisStringValue = [dataVault getString:dataName.name];
if ( thisStringValue == nil ) {
NSData *thisBinaryValue = [dataVault getValue:dataName.name];
NSLog(@" thisBinaryValue %@ ",thisBinaryValue );
}
}
}
}
[dataVault changePassword:@"password!2A" Salt:@"saltD#ddg#k05%gnd[!2A"];
// Because this is a test example, we will delete our vault at the end.
// This means we will forever lose all data we persisted in our data vault.
[DataVault deleteVault: @"SampleVault"];
}
@catch (DataVaultException *exception)
{
NSLog(@"Datavault exception. Reason: %@", [exception reason]);
}