bitwarden-android/src/App/Models/CipherString.cs

54 lines
1.7 KiB
C#
Raw Normal View History

2016-05-02 09:52:09 +03:00
using System;
using Bit.App.Abstractions;
using XLabs.Ioc;
namespace Bit.App.Models
{
public class CipherString
{
2016-05-04 02:49:49 +03:00
private string _decryptedValue;
2016-05-02 09:52:09 +03:00
public CipherString(string encryptedString)
{
if(string.IsNullOrWhiteSpace(encryptedString) || !encryptedString.Contains("|"))
{
throw new ArgumentException(nameof(encryptedString));
}
EncryptedString = encryptedString;
}
public CipherString(string initializationVector, string cipherText)
{
if(string.IsNullOrWhiteSpace(initializationVector))
{
throw new ArgumentNullException(nameof(initializationVector));
}
if(string.IsNullOrWhiteSpace(cipherText))
{
throw new ArgumentNullException(nameof(cipherText));
}
EncryptedString = string.Format("{0}|{1}", initializationVector, cipherText);
}
public string EncryptedString { get; private set; }
public string InitializationVector { get { return EncryptedString?.Split('|')[0]; } }
public string CipherText { get { return EncryptedString?.Split('|')[1]; } }
public byte[] InitializationVectorBytes { get { return Convert.FromBase64String(InitializationVector); } }
public byte[] CipherTextBytes { get { return Convert.FromBase64String(CipherText); } }
public string Decrypt()
{
2016-05-04 02:49:49 +03:00
if(_decryptedValue == null)
{
var cryptoService = Resolver.Resolve<ICryptoService>();
_decryptedValue = cryptoService.Decrypt(this);
}
return _decryptedValue;
2016-05-02 09:52:09 +03:00
}
}
}