-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtectedTagHelper.cs
More file actions
66 lines (58 loc) · 2.33 KB
/
ProtectedTagHelper.cs
File metadata and controls
66 lines (58 loc) · 2.33 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System.Text;
using System.Text.Encodings.Web;
namespace TestApp.TagHelpers
{
/// <summary>
/// convert "<protected class="protected-str">My sensitive data</protected>"
/// to "<a href="#" data-protected="195$8ebae3b0a6adb0aab7aab5a6e3a7a2b7a2" class="protected-str">[Protected]</a>"
/// </summary>
[HtmlTargetElement("protected", TagStructure = TagStructure.NormalOrSelfClosing)]
public class ProtectedTagHelper : TagHelper
{
/// <summary>
/// Default value for placeholder in source code before decoding by JS
/// </summary>
[HtmlAttributeName("placeholder")]
public string Placeholder { get; set; } = "[Protected]";
/// <summary>
/// Page to open on click on Tag before decoding by JS
/// </summary>
[HtmlAttributeName("href")]
protected virtual string LinkTarget { get; } = "#";
/// <summary>
/// formate Tag and encode content
/// </summary>
/// <param name="context"></param>
/// <param name="output"></param>
/// <returns></returns>
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
var content = (await output.GetChildContentAsync()).GetContent();
var encoded = EncodeString(content, Random.Shared.Next(1, 256));
output.TagName = "a";
output.Attributes.RemoveAll("placeholder");
output.Attributes.SetAttribute("data-protected", encoded);
output.Content.SetContent(Placeholder);
}
/// <summary>
/// Encode the string with the key with XOR
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns>formated encrypted string</returns>
protected static string EncodeString(string str, int key)
{
var encrypted = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
var charCode = (int)str[i];
var encryptedChar = charCode ^ key;
encrypted.Append(encryptedChar.ToString("x2"));
}
return key + "*" + encrypted.ToString();
}
}
}