mirror of
https://github.com/bitwarden/mobile
synced 2025-12-16 00:03:22 +00:00
store previous key and userid so we can determine if stored crypto is usable before a sync
This commit is contained in:
@@ -4,18 +4,21 @@ using System.Text;
|
||||
using Bit.App.Abstractions;
|
||||
using Bit.App.Models;
|
||||
using PCLCrypto;
|
||||
using System.Linq;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class CryptoService : ICryptoService
|
||||
{
|
||||
private const string KeyKey = "key";
|
||||
private const string PreviousKeyKey = "previousKey";
|
||||
private const int InitializationVectorSize = 16;
|
||||
|
||||
private readonly Random _random = new Random();
|
||||
private readonly ISecureStorageService _secureStorage;
|
||||
private readonly IKeyDerivationService _keyDerivationService;
|
||||
private byte[] _key;
|
||||
private byte[] _previousKey;
|
||||
|
||||
public CryptoService(
|
||||
ISecureStorageService secureStorage,
|
||||
@@ -44,6 +47,7 @@ namespace Bit.App.Services
|
||||
}
|
||||
else
|
||||
{
|
||||
PreviousKey = _key;
|
||||
_secureStorage.Delete(KeyKey);
|
||||
_key = null;
|
||||
}
|
||||
@@ -63,6 +67,29 @@ namespace Bit.App.Services
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] PreviousKey
|
||||
{
|
||||
get
|
||||
{
|
||||
if(_previousKey == null)
|
||||
{
|
||||
_previousKey = _secureStorage.Retrieve(PreviousKeyKey);
|
||||
}
|
||||
|
||||
return _previousKey;
|
||||
}
|
||||
private set
|
||||
{
|
||||
if(value != null)
|
||||
{
|
||||
_secureStorage.Store(PreviousKeyKey, value);
|
||||
_previousKey = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool KeyChanged => !PreviousKey?.SequenceEqual(Key) ?? Key == null ? false : true;
|
||||
|
||||
public CipherString Encrypt(string plaintextValue)
|
||||
{
|
||||
if(Key == null)
|
||||
|
||||
Reference in New Issue
Block a user