-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathDataProtectionConverter.cs
More file actions
37 lines (30 loc) · 1.14 KB
/
DataProtectionConverter.cs
File metadata and controls
37 lines (30 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// FIXME: Update this file to be null safe and then delete the line below
#nullable disable
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));
}
}