-
Notifications
You must be signed in to change notification settings - Fork 3
/
DataProtectionAPIProtectProvider.cs
55 lines (47 loc) · 2.11 KB
/
DataProtectionAPIProtectProvider.cs
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using Microsoft.AspNetCore.DataProtection;
using System;
namespace Fededim.Extensions.Configuration.Protected.DataProtectionAPI
{
/// <summary>
/// The standard Microsoft DataProtectionAPI protect provider for Fededim.Extensions.Configuration.Protected, implementing the <see cref="IProtectProvider"/> interface.
/// </summary>
public class DataProtectionAPIProtectProvider : IProtectProvider
{
public IDataProtector DataProtector { get; }
/// <summary>
/// The main constructor
/// </summary>
/// <param name="dataProtector">the <see cref="IDataProtector"/> interface obtained from Data Protection API</param>
public DataProtectionAPIProtectProvider(IDataProtector dataProtector)
{
DataProtector = dataProtector;
}
/// <summary>
/// This methods create a new <see cref="IProtectProvider"/> for supporting per configuration value encryption subkey (e.g. "subpurposes")
/// </summary>
/// <param name="subkey">the per configuration value encryption subkey</param>
/// <returns>a derived <see cref="IProtectProvider"/> based on the <see cref="subkey"/> parameter</returns>
public IProtectProvider CreateNewProviderFromSubkey(String subkey)
{
return new DataProtectionAPIProtectProvider(DataProtector.CreateProtector(subkey));
}
/// <summary>
/// This method decrypts an encrypted string
/// </summary>
/// <param name="encryptedValue">the encrypted string to be decrypted</param>
/// <returns>the decrypted string</returns>
public String Decrypt(String encryptedValue)
{
return DataProtector.Unprotect(encryptedValue);
}
/// <summary>
/// This method encrypts a plain-text string
/// </summary>
/// <param name="plainTextValue">the plain-text string to be encrypted</param>
/// <returns>the encrypted string</returns>
public String Encrypt(String plainTextValue)
{
return DataProtector.Protect(plainTextValue);
}
}
}