/**
  * Encrypt database with new password
  *
  * @param oldPassword the old password
  * @param newPassword the new password
  * @param encrypt
  * @return re-encrypted database
  */
 boolean changeMasterPassword(String oldPassword, String newPassword, boolean encrypt) {
   if (!setMasterPassword(oldPassword)) {
     return false;
   }
   byte[] oldKey = key.get();
   byte[] newKey = EncryptionUtil.genPasswordKey(newPassword);
   ByteArrayWrapper testKey =
       new ByteArrayWrapper(
           EncryptionUtil.dbKey(oldKey, MasterKeyPasswordSafe.class, testKey(oldPassword)));
   HashMap<ByteArrayWrapper, byte[]> oldDb = new HashMap<ByteArrayWrapper, byte[]>();
   database.copyTo(oldDb);
   HashMap<ByteArrayWrapper, byte[]> newDb = new HashMap<ByteArrayWrapper, byte[]>();
   for (Map.Entry<ByteArrayWrapper, byte[]> e : oldDb.entrySet()) {
     if (testKey.equals(e.getKey())) {
       continue;
     }
     byte[] decryptedKey = EncryptionUtil.decryptKey(oldKey, e.getKey().unwrap());
     String decryptedText = EncryptionUtil.decryptText(oldKey, e.getValue());
     newDb.put(
         new ByteArrayWrapper(EncryptionUtil.encryptKey(newKey, decryptedKey)),
         EncryptionUtil.encryptText(newKey, decryptedText));
   }
   synchronized (database.getDbLock()) {
     resetMasterPassword(newPassword, encrypt);
     database.putAll(newDb);
   }
   return true;
 }