Code Sample

Create a data vault for encrypted storage of application data.

public void testFunctionality()   
{
   try
   {
      DataVault oDataVault = null;
      
      // 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.bDefaultPasswordAllowed   = true;
      oPasswordPolicy.iMinLength                = 4;
      oPasswordPolicy.bHasDigits                = true;
      oPasswordPolicy.bHasUpper                 = true;
      oPasswordPolicy.bHasLower                 = true;
      oPasswordPolicy.bHasSpecial               = true;
      oPasswordPolicy.iExpirationDays           = 20;
      oPasswordPolicy.iMinUniqueChars           = 3;
      oPasswordPolicy.iLockTimeout              = 1600;
      oPasswordPolicy.iRetryLimit               = 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].iType == DataVault.DV_DATA_TYPE_STRING )
         {
            String thisStringValue = oDataVault.getString( dataNameArray[i].sName );
         }
         else
         {
            byte[] thisBinaryValue = oDataVault.getValue( dataNameArray[i].sName );
         }
      }
      
      // 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( Exception exception )
   {
      
   }
   finally
   {
      // 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" );
   }
}