Code Sample

Create a data vault for encrypted storage of application data.

public void testFunctionality(Context oContext)    
  {
    try
    {
       DataVault oDataVault = null;

       DataVault.init( oContext );

       // If this dataVault already exists, then get it by calling getVault()
       // Else create this new dataVault by calling createVault()
       if ( DataVault.vaultExists( "DataVaultExample" ) )
         oDataVault = DataVault.getVault( "DataVaultExample" );
       else
         oDataVault = DataVault.createVault( "DataVaultExample", "password!1A", "saltD#ddg#k05%gnd[!1A" );

       // Call setLockTimeout(). This allows you to set the timeout of the vault in seconds
       oDataVault.setLockTimeout( 1500 );
       int iTimeout = oDataVault.getLockTimeout();

       // Call setRetryLimit(). This allows you to set the number of retries before the vault is destroyed
       oDataVault.setRetryLimit( 10 );
       int iRetryLimit = oDataVault.getRetryLimit();

       // Call setPasswordPolicy(). The passwordPolicy also includes the retryLimit and LockTimeout that we set above.
       DataVault.DVPasswordPolicy oPasswordPolicy = new DataVault.DVPasswordPolicy();
       oPasswordPolicy.setIsDefaultPasswordAllowed (true);
       oPasswordPolicy.setMinLength( 4 );
       oPasswordPolicy.setHasDigits( true );
       oPasswordPolicy.setHasUpper( true );
       oPasswordPolicy.setHasLower( true );
       oPasswordPolicy.setHasSpecial( true );
       oPasswordPolicy.setExpirationDays( 20 );
       oPasswordPolicy.setMinUniqueChars( 3 );
       oPasswordPolicy.setLockTimeout( 1600 );
       oPasswordPolicy.setRetryLimit( 20 );

       // SetPasswordPolicy() will always lock the vault to ensure the old password
       // conforms to the new password policy settings.
       oDataVault.setPasswordPolicy( oPasswordPolicy );  

       // We are now locked and need to unlock before we can access the vault.
       oDataVault.unlock( "password!1A", "saltD#ddg#k05%gnd[!1A" );

       // Call getPasswordPolicy() to return the current password policy settings.
       DataVault.DVPasswordPolicy oCurrentPolicy = oDataVault.getPasswordPolicy();

       // Call setString() by giving it a name:value pair to encrypt and persist 
       // a string data type within your dataVault.
       oDataVault.setString( "stringName", "stringValue" );

       // Call getString to retrieve the string we just stored in our data vault!
       String storedStringValue = oDataVault.getString( "stringName" );

       // Call setValue() by giving it a name:value pair to encrypt and persist 
       // a binary data type within your dataVault.
       byte[] binaryValue = { 1, 2, 3, 4, 5, 6, 7  };
       oDataVault.setValue( "binaryName", binaryValue );

       // Call getValue to retrieve the binary we just stored in our data vault!
       byte[] storedBinaryValue = oDataVault.getValue( "binaryName" );

       // Call getDataNames to retrieve all stored element names from our data vault.
       DataVault.DVDataName[] dataNameArray = oDataVault.getDataNames();
       for ( int i = 0; i < dataNameArray.length; i++ )
       {
          if ( dataNameArray[i].getType() == DataVault.DV_DATA_TYPE_STRING )
          {
             String thisStringValue = oDataVault.getString( dataNameArray[i].getName() );
          }
          else
          {
             byte[] thisBinaryValue = oDataVault.getValue( dataNameArray[i].getName() );
          }
       }

       // Call changePassword with 2 parameters. Vault must be unlocked.
       // If you pass null parameters as your new password or your new salt,
       // it will generate a default password or default salt, respectively.
       oDataVault.changePassword( null, null );

       // Call isDefaultPasswordused() to see if we are using an automatically
       // generated password (which we are).
       boolean isDefaultPasswordUsed = oDataVault.isDefaultPasswordUsed();

       // Lock the vault.
       oDataVault.lock();

       // Call changePassword with 4 parameters even if the vault is locked.
      // Here, we pass null for oldSalt and oldPassword because defaults were used.
       oDataVault.changePassword( null, null, "password!1A", "saltD#ddg#k05%gnd[!1A" );

       // Call isDefaultPasswordused() and we will see that the default password is NOT used anymore.
       isDefaultPasswordUsed = oDataVault.isDefaultPasswordUsed();
    }
    catch( Throwable exception )
    {
      exception.printStackTrace();
    }
    finally
    {
      try
       {
         // 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.
         if ( DataVault.vaultExists( "DataVaultExample" ) )
           DataVault.deleteVault( "DataVaultExample" );
       }
       catch(Throwable t)
       {
         t.printStackTrace();   
       }
    }
  }