mirror of
https://github.com/bitwarden/mobile
synced 2025-12-13 23:03:23 +00:00
start writing crypto service
This commit is contained in:
109
src/Core/Models/Domain/CipherString.cs
Normal file
109
src/Core/Models/Domain/CipherString.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using Bit.Core.Enums;
|
||||
using System;
|
||||
|
||||
namespace Bit.Core.Models.Domain
|
||||
{
|
||||
public class CipherString
|
||||
{
|
||||
private string _decryptedValue;
|
||||
|
||||
public CipherString(EncryptionType encryptionType, string data, string iv, string mac)
|
||||
{
|
||||
if(string.IsNullOrWhiteSpace(data))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
}
|
||||
|
||||
if(!string.IsNullOrWhiteSpace(iv))
|
||||
{
|
||||
EncryptedString = string.Format("{0}.{1}|{2}", (byte)encryptionType, iv, data);
|
||||
}
|
||||
else
|
||||
{
|
||||
EncryptedString = string.Format("{0}.{1}", (byte)encryptionType, data);
|
||||
}
|
||||
|
||||
if(!string.IsNullOrWhiteSpace(mac))
|
||||
{
|
||||
EncryptedString = string.Format("{0}|{1}", EncryptedString, mac);
|
||||
}
|
||||
|
||||
EncryptionType = encryptionType;
|
||||
Data = data;
|
||||
Iv = iv;
|
||||
Mac = mac;
|
||||
}
|
||||
|
||||
public CipherString(string encryptedString)
|
||||
{
|
||||
if(string.IsNullOrWhiteSpace(encryptedString))
|
||||
{
|
||||
throw new ArgumentException(nameof(encryptedString));
|
||||
}
|
||||
|
||||
EncryptedString = encryptedString;
|
||||
var headerPieces = EncryptedString.Split('.');
|
||||
string[] encPieces;
|
||||
|
||||
if(headerPieces.Length == 2 && Enum.TryParse(headerPieces[0], out EncryptionType encType))
|
||||
{
|
||||
EncryptionType = encType;
|
||||
encPieces = headerPieces[1].Split('|');
|
||||
}
|
||||
else
|
||||
{
|
||||
encPieces = EncryptedString.Split('|');
|
||||
EncryptionType = encPieces.Length == 3 ? EncryptionType.AesCbc128_HmacSha256_B64 :
|
||||
EncryptionType.AesCbc256_B64;
|
||||
}
|
||||
|
||||
switch(EncryptionType)
|
||||
{
|
||||
case EncryptionType.AesCbc128_HmacSha256_B64:
|
||||
case EncryptionType.AesCbc256_HmacSha256_B64:
|
||||
if(encPieces.Length != 3)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Iv = encPieces[0];
|
||||
Data = encPieces[1];
|
||||
Mac = encPieces[2];
|
||||
break;
|
||||
case EncryptionType.AesCbc256_B64:
|
||||
if(encPieces.Length != 2)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Iv = encPieces[0];
|
||||
Data = encPieces[1];
|
||||
break;
|
||||
case EncryptionType.Rsa2048_OaepSha256_B64:
|
||||
case EncryptionType.Rsa2048_OaepSha1_B64:
|
||||
if(encPieces.Length != 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Data = encPieces[0];
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public EncryptionType EncryptionType { get; private set; }
|
||||
public string EncryptedString { get; private set; }
|
||||
public string Iv { get; private set; }
|
||||
public string Data { get; private set; }
|
||||
public string Mac { get; private set; }
|
||||
|
||||
public string Decrypt(string orgId = null)
|
||||
{
|
||||
if(_decryptedValue == null)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
return _decryptedValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user