mirror of
https://github.com/bitwarden/android.git
synced 2024-10-31 15:15:34 +03:00
async vault fetch and filter (search) tasks. Resolved singleton crypto issue around reuse of PaddedBufferedBlockCipher
This commit is contained in:
parent
f9fd53c733
commit
f0455aad74
2 changed files with 46 additions and 34 deletions
|
@ -48,6 +48,7 @@ namespace Bit.App.Pages
|
|||
public ListView ListView { get; set; }
|
||||
public IEnumerable<VaultListPageModel.Site> Sites { get; set; } = new List<VaultListPageModel.Site>();
|
||||
public IEnumerable<VaultListPageModel.Folder> Folders { get; set; } = new List<VaultListPageModel.Folder>();
|
||||
public SearchBar Search { get; set; }
|
||||
|
||||
private void Init()
|
||||
{
|
||||
|
@ -77,18 +78,18 @@ namespace Bit.App.Pages
|
|||
|
||||
ListView.ItemSelected += SiteSelected;
|
||||
|
||||
var searchBar = new SearchBar
|
||||
Search = new SearchBar
|
||||
{
|
||||
Placeholder = "Search vault...",
|
||||
BackgroundColor = Color.FromHex("efeff4")
|
||||
};
|
||||
searchBar.TextChanged += SearchBar_TextChanged;
|
||||
searchBar.SearchButtonPressed += SearchBar_SearchButtonPressed;
|
||||
Search.TextChanged += SearchBar_TextChanged;
|
||||
Search.SearchButtonPressed += SearchBar_SearchButtonPressed;
|
||||
|
||||
Title = _favorites ? AppResources.Favorites : AppResources.MyVault;
|
||||
Content = new StackLayout
|
||||
{
|
||||
Children = { searchBar, ListView },
|
||||
Children = { Search, ListView },
|
||||
Spacing = 0
|
||||
};
|
||||
}
|
||||
|
@ -112,16 +113,25 @@ namespace Bit.App.Pages
|
|||
|
||||
private void FilterResults(string searchFilter)
|
||||
{
|
||||
if(string.IsNullOrWhiteSpace(searchFilter))
|
||||
Task.Run(async () =>
|
||||
{
|
||||
LoadFolders(Sites);
|
||||
}
|
||||
else
|
||||
{
|
||||
searchFilter = searchFilter.ToLower();
|
||||
var filteredSites = Sites.Where(s => s.Name.ToLower().Contains(searchFilter) || s.Username.ToLower().Contains(searchFilter));
|
||||
LoadFolders(filteredSites);
|
||||
}
|
||||
await Task.Delay(300);
|
||||
if(searchFilter != Search.Text)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if(string.IsNullOrWhiteSpace(searchFilter))
|
||||
{
|
||||
LoadFolders(Sites);
|
||||
}
|
||||
else
|
||||
{
|
||||
searchFilter = searchFilter.ToLower();
|
||||
var filteredSites = Sites.Where(s => s.Name.ToLower().Contains(searchFilter) || s.Username.ToLower().Contains(searchFilter));
|
||||
LoadFolders(filteredSites);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected async override void OnAppearing()
|
||||
|
@ -151,17 +161,20 @@ namespace Bit.App.Pages
|
|||
|
||||
private async Task FetchAndLoadVaultAsync()
|
||||
{
|
||||
var foldersTask = _folderService.GetAllAsync();
|
||||
var sitesTask = _favorites ? _siteService.GetAllAsync(true) : _siteService.GetAllAsync();
|
||||
await Task.WhenAll(foldersTask, sitesTask);
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
var foldersTask = _folderService.GetAllAsync();
|
||||
var sitesTask = _favorites ? _siteService.GetAllAsync(true) : _siteService.GetAllAsync();
|
||||
await Task.WhenAll(foldersTask, sitesTask);
|
||||
|
||||
var folders = await foldersTask;
|
||||
var sites = await sitesTask;
|
||||
var folders = await foldersTask;
|
||||
var sites = await sitesTask;
|
||||
|
||||
Folders = folders.Select(f => new VaultListPageModel.Folder(f));
|
||||
Sites = sites.Select(s => new VaultListPageModel.Site(s));
|
||||
Folders = folders.Select(f => new VaultListPageModel.Folder(f));
|
||||
Sites = sites.Select(s => new VaultListPageModel.Site(s));
|
||||
|
||||
LoadFolders(Sites);
|
||||
LoadFolders(Sites);
|
||||
});
|
||||
}
|
||||
|
||||
private void LoadFolders(IEnumerable<VaultListPageModel.Site> sites)
|
||||
|
|
|
@ -21,16 +21,13 @@ namespace Bit.App.Services
|
|||
private const int Iterations = 5000;
|
||||
|
||||
private readonly Random _random = new Random();
|
||||
private readonly PaddedBufferedBlockCipher _cipher;
|
||||
private readonly ISecureStorageService _secureStorage;
|
||||
private readonly CbcBlockCipher _aesBlockCipher;
|
||||
private KeyParameter _keyParameter;
|
||||
|
||||
public CryptoService(ISecureStorageService secureStorage)
|
||||
{
|
||||
var engine = new AesEngine();
|
||||
var blockCipher = new CbcBlockCipher(engine);
|
||||
_cipher = new PaddedBufferedBlockCipher(blockCipher);
|
||||
|
||||
_aesBlockCipher = new CbcBlockCipher(new AesEngine());
|
||||
_secureStorage = secureStorage;
|
||||
}
|
||||
|
||||
|
@ -96,10 +93,11 @@ namespace Bit.App.Services
|
|||
var iv = GenerateRandomInitializationVector();
|
||||
var keyParamWithIV = new ParametersWithIV(_keyParameter, iv, 0, InitializationVectorSize);
|
||||
|
||||
_cipher.Init(true, keyParamWithIV);
|
||||
var encryptedBytes = new byte[_cipher.GetOutputSize(plaintextBytes.Length)];
|
||||
var length = _cipher.ProcessBytes(plaintextBytes, encryptedBytes, 0);
|
||||
_cipher.DoFinal(encryptedBytes, length);
|
||||
var cipher = new PaddedBufferedBlockCipher(_aesBlockCipher);
|
||||
cipher.Init(true, keyParamWithIV);
|
||||
var encryptedBytes = new byte[cipher.GetOutputSize(plaintextBytes.Length)];
|
||||
var length = cipher.ProcessBytes(plaintextBytes, encryptedBytes, 0);
|
||||
cipher.DoFinal(encryptedBytes, length);
|
||||
|
||||
return new CipherString(Convert.ToBase64String(iv), Convert.ToBase64String(encryptedBytes));
|
||||
}
|
||||
|
@ -119,10 +117,11 @@ namespace Bit.App.Services
|
|||
try
|
||||
{
|
||||
var keyParamWithIV = new ParametersWithIV(_keyParameter, encyptedValue.InitializationVectorBytes, 0, InitializationVectorSize);
|
||||
_cipher.Init(false, keyParamWithIV);
|
||||
byte[] comparisonBytes = new byte[_cipher.GetOutputSize(encyptedValue.CipherTextBytes.Length)];
|
||||
var length = _cipher.ProcessBytes(encyptedValue.CipherTextBytes, comparisonBytes, 0);
|
||||
_cipher.DoFinal(comparisonBytes, length);
|
||||
var cipher = new PaddedBufferedBlockCipher(_aesBlockCipher);
|
||||
cipher.Init(false, keyParamWithIV);
|
||||
byte[] comparisonBytes = new byte[cipher.GetOutputSize(encyptedValue.CipherTextBytes.Length)];
|
||||
var length = cipher.ProcessBytes(encyptedValue.CipherTextBytes, comparisonBytes, 0);
|
||||
cipher.DoFinal(comparisonBytes, length);
|
||||
return Encoding.UTF8.GetString(comparisonBytes, 0, comparisonBytes.Length).TrimEnd('\0');
|
||||
}
|
||||
catch(Exception e)
|
||||
|
|
Loading…
Reference in a new issue