The core infrastructure backend (API, database, Docker, etc).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

33 lines
1.1 KiB

using Bit.Core;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Bit.Infrastructure.EntityFramework.Converters;
public class DataProtectionConverter : ValueConverter<string, string>
{
public DataProtectionConverter(IDataProtector dataProtector) :
base(s => Protect(dataProtector, s), s => Unprotect(dataProtector, s))
{ }
private static string Protect(IDataProtector dataProtector, string value)
{
if (value?.StartsWith(Constants.DatabaseFieldProtectedPrefix) ?? true)
{
return value;
}
return string.Concat(
Constants.DatabaseFieldProtectedPrefix, dataProtector.Protect(value));
}
private static string Unprotect(IDataProtector dataProtector, string value)
{
if (!value?.StartsWith(Constants.DatabaseFieldProtectedPrefix) ?? true)
{
return value;
}
return dataProtector.Unprotect(
value.Substring(Constants.DatabaseFieldProtectedPrefix.Length));
}
}